PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Regular expression issue in korn shell
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Regular expression issue in korn shell

Réponse
 
LinkBack Outils de la discussion
Vieux 17/08/2006, 09h55   #1 (permalink)
Kev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regular expression issue in korn shell

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

  Réponse avec citation
Vieux 17/08/2006, 11h00   #2 (permalink)
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

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
  Réponse avec citation
Vieux 17/08/2006, 11h30   #3 (permalink)
Kev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

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


  Réponse avec citation
Vieux 17/08/2006, 12h29   #4 (permalink)
Bruce Barnett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

"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.
  Réponse avec citation
Vieux 17/08/2006, 13h57   #5 (permalink)
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

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
  Réponse avec citation
Vieux 17/08/2006, 16h19   #6 (permalink)
Kev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h07.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14595 seconds with 14 queries