On Tue, Feb 06, 2001 at 12:06:09AM +0800, Andrew So Hing-pong 
<[EMAIL PROTECTED]> wrote:
| I would like to ask a question about regular expression & patten
| matching on the script writting.
| 
| test.sh
| ...
| if [ $1 != "v[0-9][0-9].[0-9][0-9].[0-9][0-9]" ] ; then
|    echo "Version must be vXX.XX.XX where X be a digit"
| else
|    echo "Version -> [$1]"
| fi
| 
| I want to ensure the $1 must v00.00.00 (an example), but the result
| 
| #./test.sh v00.01.01
| Version must be vXX.XX.XX where X be a digit
| 
| #./test.sh v[0-9][0-9].[0-9][0-9].[0-9][0-9]
| Version -> [v[0-9][0-9].[0-9][0-9].[0-9][0-9]]

This is because you're doing a string comparison, not a pattern match.
!= tests two strings for equality.

Before going further, just some notation: shell patterns (aka "globs")
are _not_ regular expressions, which has a different syntax. Ed,
vi/vim/nvi, emacs, perl, sek,awk et al use regular expressions. The
shell uses globs, which are much better notation when you're talking
about filenames.

To do your pattern match you want a case statement:

        case "$1" in
                v[0-9][0-9].[0-9][0-9].[0-9][0-9])
                        echo "Version -> [$1]"
                        ;;
                *)      echo "Version must be vXX.XX.XX where X be a digit"
                        ;;
        esac

Cheers,
-- 
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

I recently saw a crash where a yokel in a lwb Landrover had pulled
out of a junction on a fast road in front of a speeding Volvo (one of teh big
estate cars). The volvo had t-boned it amidshipps at about 60mph The landrover
was badly dented. Need a couple of hours with  a hammer to straighten the
panels. The Volvo was urecognisable from the turret forewards.

Brakes? Who cares?
        - Andy Woodward, <[EMAIL PROTECTED]>



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to