Bash regex issue

Robert Nichols rnicholsNOSPAM at comcast.net
Sat Feb 5 16:13:31 UTC 2011


On 02/05/2011 09:51 AM, Emmett Culley wrote:
> I have a bash script I've been using in CentOS that no longer works in Fedora 14. The pertinent lines are:
>
> mac=`echo $2 | tr a-z A-Z`
> if [[ "$mac" =~ '[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]' ]]; then
>    echo 'good'
> else
>    echo 'Invalid MAC address'
> fi
>
> In CentOS or RHEL5 setting parameter 2 = 00:00:00:00:af:df works as expected, but not in Fedora.  By adding -x to the first line of the script the output to the terminal in CentOS looks like this:
>
> ++ echo 00:00:00:00:af:df
> ++ tr a-z A-Z
> + mac=00:00:00:00:AF:DF
> + [[ 00:00:00:00:AF:DF =~ [0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F] ]]
> + echo good
> good
>
> In Fedora it looks like this:
>
> ++ echo 00:00:00:00:af:df
> ++ tr a-z A-Z
> + mac=00:00:00:00:AF:DF
> + [[ 00:00:00:00:AF:DF =~ \[0-9A-F]\[0-9A-F]:\[0-9A-F]\[0-9A-F]:\[0-9A-F]\[0-9A-F]:\[0-9A-F]\[0-9A-F]:\[0-9A-F]\[0-9A-F]:\[0-9A-F]\[0-9A-F] ]]
> + echo 'Invalid MAC address'
> Invalid MAC address
>
> Note the "\" before each "[" in the regular expression.
>
> Any ideas what is going on here?  Is there a better forum to ask this question?

See the bash FAQ at ftp://ftp.cwru.edu/pub/bash/FAQ item E14 "Why does
quoting the pattern argument to the regular expression matching
conditional operator (=~) cause regexp matching to stop working?"

-- 
Bob Nichols     "NOSPAM" is really part of my email address.
                 Do NOT delete it.



More information about the users mailing list