Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 19 Aug 2007 01:08:07 +0200
From:      Christer Hermansson <mail@chdevelopment.se>
To:        freebsd-questions@freebsd.org
Cc:        Derek Ragona <derek@computinginnovations.com>
Subject:   Re: Regular expressions
Message-ID:  <46C77BD7.1080609@chdevelopment.se>
In-Reply-To: <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com>
References:  <46C726A8.9010404@chdevelopment.se> <6.0.0.22.2.20070818130942.02634918@mail.computinginnovations.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Derek Ragona wrote:
> At 12:04 PM 8/18/2007, Christer Hermansson wrote:
>> I also found some basic example at 
>> http://www.grymoire.com/Unix/Sh.html#uh-88 :
>>
>> --------8<--------8<--------8<--------8<--------8<--------
>>
>> #!/bin/sh
>>
>> echo "Type in a number"
>> read ans
>> number=`expr "$ans" : "([0-9]*)"`
>> if [ "$number" != "$ans" ]; then
>> echo "Not a number"
>> elif [ "$number" -eq 0 ]; then
>> echo "Nothing was typed"
>> else
>> echo "$number is a fine number"
>> fi
>>
>> --------8<--------8<--------8<--------8<--------8<--------
>>
>> The above example doesn't work on my freebsd box. Maybe I need to 
>> update my system, sitting with 6.0R which never been updated.
>>
>
> You have a syntax error using expr.  Do a man on expr for more details 
> but if you change that line from:
> number=`expr "$ans" : "([0-9]*)"`
> to:
> number=`expr "$ans" : "\([0-9]*\)"`
>
> You will get the desired results.
>
> Also when debugging scripts remember to add:
> set -x
> to your script on the second line, and see what the script lines are 
> actually doing.
>
>         -Derek
>
Thanks Derek ! Now both the example and my own code works for me. I 
changed my code from "^[A-Za-z0-9_-]+$" to "\([A-Za-z0-9_-]*\)" It seems 
that FreeBSD's expr want some different syntax than the webbased test 
tool at http://regexlib.com/RETester.aspx

-- 

Christer Hermansson





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?46C77BD7.1080609>