Unix Shell - Re: bourne sh scripting: how can I easily test if a given string occurs in one of a se

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > Re: bourne sh scripting: how can I easily test if a given string occurs in one of a se





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: bourne sh scripting: how can I easily test if a given string occurs in one of a se
Michael Paoli

2006-09-21, 1:25 pm

Leonardo wrote:
> 2) how can I easily test if a given string occurs in one of a set of
> strings?
> My current method is:
>
> if [ "$STR" == `echo $STR | egrep '^(one|two|three)$'` ]; then
> echo "$STR is in (one two three)"
> fi
>
> I was wondering if there was a way without using regular expressions, rather
> a list (...), or more simple way.


$ if >>/dev/null 2>&1 fgrep string - << \__EOT__; then
> set
> of
> strings
> __EOT__
> echo matched
> else
> echo not matched
> false
> fi

matched
$ echo $?
0
$ if >>/dev/null 2>&1 fgrep yet_another_string - << \__EOT__; then
> set
> of
> strings
> __EOT__
> echo matched
> else
> echo not matched
> false
> fi

not matched
$ echo $?
1
$

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com