FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Fedora User

 
 
LinkBack Thread Tools
 
Old 02-05-2011, 02:51 PM
Emmett Culley
 
Default Bash regex issue

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:AFF
+ [[ 00:00:00:00:AFF =~ [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:AFF
+ [[ 00:00:00:00:AFF =~ [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?
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 02-05-2011, 03:08 PM
James McKenzie
 
Default Bash regex issue

On 2/5/11 8: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:AFF
> + [[ 00:00:00:00:AFF =~ [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:AFF
> + [[ 00:00:00:00:AFF =~ [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.
>
That is an error as BASH in FC14 is trying to escape the left bracket
and should not be doing so.

Check the FC bugzilla and if there is not a bug report, file one.

James McKenzie

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 02-05-2011, 03:13 PM
Robert Nichols
 
Default Bash regex issue

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:AFF
> + [[ 00:00:00:00:AFF =~ [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:AFF
> + [[ 00:00:00:00:AFF =~ [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.

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 
Old 02-05-2011, 04:39 PM
Emmett Culley
 
Default Bash regex issue

On 02/05/2011 08:13 AM, Robert Nichols wrote:
> 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:AFF
>> + [[ 00:00:00:00:AFF =~ [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:AFF
>> + [[ 00:00:00:00:AFF =~ [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?"
>

Thanks, that was the clue I needed.

The version of bash in CentOS is 3.2 so according to the page referenced above, it should have worked the same as in Fedora (4.1), but the explanation gave me the clue.

I fixed it by first storing the regex expression in a variable.

regex='[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]'
mac=`echo $2 | tr a-z A-Z`
if [[ "$mac" =~ $regex ]]; then
echo 'good'
else
echo 'Invalid MAC address'
fi

Now it works in both CentOS and Fedora, but it is important that you leave $regex unquoted, or you will have the same problem in Fedora. Unquoted it works in both.

--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
 

Thread Tools




All times are GMT. The time now is 04:27 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org