|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
In a ks script I have written, the first arg is an IP address - I decided to check the validity of this using a regular expression: #Check validity of arg1 if [[ "$1" = \b(? ?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b]] then echo "Arg1 is a valid IP address" else echo "Arg1 is invalid" fi The regex may not be the most effiecient, but it is right, anyway, when I run the script, I get: ../scriptabc.ksh: syntax error at line 17 : `(' unexpected What am I doing wrong? (I am relatively new to ksh) Thanks in advance |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 17 Aug 2006 01:55:11 -0700, Kev wrote:
> Hi, > > In a ks script I have written, the first arg is an IP address - I > decided to check the validity of this using a regular expression: > > #Check validity of arg1 > if [[ "$1" = > \b(? ?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b> ]] > then > echo "Arg1 is a valid IP address" > else > echo "Arg1 is invalid" > fi > > The regex may not be the most effiecient, but it is right, anyway, when > I run the script, I get: > > ./scriptabc.ksh: syntax error at line 17 : `(' unexpected > > What am I doing wrong? (I am relatively new to ksh) [...] You're using a perl regular expression where ksh is expecting a ksh globbing pattern. try: if perl -e 'exit(1) unless shift(@ARGV) =~ /your-regexp/' "$1" then ... instead -- Stephane |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Very good, that works!
Could you define a "globbing pattern"? Is it the ksh equivalent of a regexp? Interestingly, I have another regular expression in my script, and this seems to work fine: *[!0-9]* As I say, I am new to ksh! Stephane Chazelas wrote: > On 17 Aug 2006 01:55:11 -0700, Kev wrote: > > Hi, > > > > In a ks script I have written, the first arg is an IP address - I > > decided to check the validity of this using a regular expression: > > > > #Check validity of arg1 > > if [[ "$1" = > > \b(? ?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b> > ]] > > then > > echo "Arg1 is a valid IP address" > > else > > echo "Arg1 is invalid" > > fi > > > > The regex may not be the most effiecient, but it is right, anyway, when > > I run the script, I get: > > > > ./scriptabc.ksh: syntax error at line 17 : `(' unexpected > > > > What am I doing wrong? (I am relatively new to ksh) > [...] > > You're using a perl regular expression where ksh is expecting a > ksh globbing pattern. > > try: > > if perl -e 'exit(1) unless shift(@ARGV) =~ /your-regexp/' "$1" > then > ... > > > instead > > > -- > Stephane |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"Kev" <kreid@cis.strath.ac.uk> writes:
> Very good, that works! > > Could you define a "globbing pattern"? Is it the ksh equivalent of a > regexp? > Shells glob. Utilities like sed, grep and awk use regular expressions. When you use the shell to do echo abc* it "globs" - expanding to all files matching the pattern. In general, the concepts are * = zero or more characters ? = single character . = . [a-z] = matches characters a-z A regular expression is very different but on the surface it seems similar. [a-z] = matches characters a-z - that's the same. But the others are different . = any single character. * = zero or more copies of the character set BEFORE it. so [a-z]* can have two different meanings As a regular expression, it will match zero or more lower case letters. As a glob - it will match any word that starts with a lower case letter. if you used the shell's echo [a-z]* it would print all files starting with a lower case letter. if you did a grep '[a-z]*' <file it would match all lines with zero or more lower case letters. So it would match ALL lines. All have zero or more letters. Regular expressions also has "anchors" such as '^' grep '^[a-z]*' <file will print all lines that START with zero or more lower case letters. grep '^[a-z][a-z]*' <file will print all lines that start with ONE or more lower case letters. Another confusing difference is that file globbing uses '?' to match any single character, and '.' is not special. Yet with regular expressions '.' matches any character. echo *.* will list all files that have a dot in the name. The regular expression to print all lines with a dot would be grep '\.' <file As I said - the '.' is special. You need the backslash to tell the regular expression engine that you want to "escape" the special meaning. If you used grep '.' <file you would print all files that contain one or more characters. For more info, see http://www.grymoire.com/Unix and http://www.grymoire.com/Unix/Regular.html Some of these tutorials are old, as I wrote them 10 years ago. > Interestingly, I have another regular expression in my script, and this > seems to work fine: > *[!0-9]* > > As I say, I am new to ksh! > > > Stephane Chazelas wrote: > >> On 17 Aug 2006 01:55:11 -0700, Kev wrote: >> > Hi, >> > >> > In a ks script I have written, the first arg is an IP address - I >> > decided to check the validity of this using a regular expression: >> > >> > #Check validity of arg1 >> > if [[ "$1" = >> > \b(? ?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b>> > ]] >> > then >> > echo "Arg1 is a valid IP address" >> > else >> > echo "Arg1 is invalid" >> > fi >> > >> > The regex may not be the most effiecient, but it is right, anyway, when >> > I run the script, I get: >> > >> > ./scriptabc.ksh: syntax error at line 17 : `(' unexpected >> > >> > What am I doing wrong? (I am relatively new to ksh) >> [...] >> >> You're using a perl regular expression where ksh is expecting a >> ksh globbing pattern. >> >> try: >> >> if perl -e 'exit(1) unless shift(@ARGV) =~ /your-regexp/' "$1" >> then >> ... >> >> >> instead >> >> >> -- >> Stephane > -- Sending unsolicited commercial e-mail to this account incurs a fee of $500 per message, and acknowledges the legality of this contract. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 17 Aug 2006 03:30:31 -0700, Kev wrote:
> Very good, that works! > > Could you define a "globbing pattern"? Is it the ksh equivalent of a > regexp? > > Interestingly, I have another regular expression in my script, and this > seems to work fine: > *[!0-9]* > > As I say, I am new to ksh! [...] To my mind, it doesn't make sense to use ksh for scripting. Best is to write a script in a standard syntax. *[!0-9]* is a globbing/wildcard/filename expansion/filename generation/fnmatch pattern (however you want to call it). The regexp equivalent is ^.*[^0-9].*$ (or [^0-9]). ksh globbing patterns extend the Bourne or fnmatch patterns (knowing only ?, * and [...]) with additional operators that make it functionnaly (but not syntactically) equivalent to regular expressions (maybe not perl ones though). There is even some way to convert from those patterns to some AT&T regular expressions (yet another variant of regular expressions). $ ksh93 -c 'printf "%R\n" "*[!0-9]*"' [^0-9] $ ksh93 -c 'printf "%R\n" "!(foo)"' ^(foo)!$ (you can see that this (foo)! is not a standard (nor perl) RE operator). ksh also provides the reverse operator (converts from regexp to wildcard): $ ksh93 -c 'printf "%P\n" "([0-9]+\.){3}"' *{3}(+([0-9])\.)* In your case, can you not simply use standard commands: Something like if awk ' BEGIN { if (split(ARGV[1], list, /[.]/) != 4) exit(1) for (i in list) { if (list[i] !~ /^[0-9]+$/ || list[i] ~ /^0./) exit(1) if (list[i] > 255) exit (1) } exit(0) }' "$1" then or: isIP() { ( IFS=. a=$1.0 set -f set -- $a [ "$#" -ne 5 ] && return 1 for i do case $i in *[!0-9]* | "" | 0?*) return 1;; esac [ "$i" -gt 255 ] && return 1 done return 0 ) } if isIP "$1" .... -- Stephane |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Thanks to both of you - interesting stuff!
I will need to learn more about reg. expressions, scripting, awk, etc... you seem to be able to do a lot with them. Kev |
|
![]() |
| Outils de la discussion | |
|
|