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 > How to test a parameter match a regexp rule?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to test a parameter match a regexp rule?

Réponse
 
LinkBack Outils de la discussion
Vieux 07/12/2006, 04h04   #1
Key9
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to test a parameter match a regexp rule?

Hi all

For example:
./test.sh 123456 abcedf

so I want there's a test mechanism of parameter.

for testing if 123456 match rule of input.

if not match ,then echo error and exit.


so I think using regexp is an idea.
and how?




thank you very much
your key9


  Réponse avec citation
Vieux 07/12/2006, 04h50   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test a parameter match a regexp rule?

On 2006-12-07, Key9 wrote:
> Hi all
>
> For example:
> ./test.sh 123456 abcedf
>
> so I want there's a test mechanism of parameter.
>
> for testing if 123456 match rule of input.
>
> if not match ,then echo error and exit.
>
>
> so I think using regexp is an idea.
> and how?


It depends on what you want to match. If it's a string:

if [ "$1" = "teststring" ]; then

If it's a file globbing pattern:

case $1 in
1*6) ;; ## If $1 starts with 1 and ends with 6
*) exit 5 ;; If not, exit with error
esac

If it has to be a regular expression, you can use expr, or in some
shells (e.g., bash3):

if [[ $1 ~ REGEXP ]]; then

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 07/12/2006, 04h57   #3
Key9
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test a parameter match a regexp rule?


>
> if [[ $1 ~ REGEXP ]]; then
>

oh man , I don't know I can using this directly:-)
In my mind , I have to run some app and test it's return.

Thank you very much!


  Réponse avec citation
Vieux 07/12/2006, 09h36   #4
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test a parameter match a regexp rule?

Key9 wrote:
> >
> > if [[ $1 ~ REGEXP ]]; then
> >

> oh man , I don't know I can using this directly:-)


It's as non-portable as the ksh variant...

if [[ $1 = pattern ]]; then

Use 'case' for portability...

case $1 in (pattern) ... ; esac


Janis

> In my mind , I have to run some app and test it's return.
>
> Thank you very much!


  Réponse avec citation
Vieux 07/12/2006, 09h36   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test a parameter match a regexp rule?

2006-12-7, 01:36(-08), Janis:
> Key9 wrote:
>> >
>> > if [[ $1 ~ REGEXP ]]; then
>> >

>> oh man , I don't know I can using this directly:-)

>
> It's as non-portable as the ksh variant...
>
> if [[ $1 = pattern ]]; then
>
> Use 'case' for portability...
>
> case $1 in (pattern) ... ; esac

[...]

case $1 in pattern) ... ; esac

will even be portable to the Bourne shell in case you might need
that level of portability.

--
Stéphane
  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 08h23.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14910 seconds with 13 queries