string test @(xx|yy|zz) in bash shell ?

string test @(xx|yy|zz) in bash shell ?

Post by Andre van Straat » Fri, 19 Nov 1999 04:00:00



I have this simple script which tests a string:

#! /bin/sh OR APPROPRIATE
s_str=xx

if [[ $s_str = $pattern ]]; then
    echo $s_str
fi

This works (i.e., I see the echo) in the GNU bash shell 1.14.7 for
Windows, and in the MKS Korn shell. In the pdksh of FreeBSD, it works

"if [ $s_str = +(xx|yy|zz) ]; then"
On the Apache server of my ISP, which runs GNU bash 1.14.7 under
i386-linux, I tried various combinations of:
"if [ $s_str = +(xx|yy|zz) ]; then",
but I never saw an echo.
How does it work generally in the bash shell?

And, is there a possibility to match substrings, i.e.,
match "xx" out of "123xx456"??

Thanks,
Andre

Andre van Straaten
Bradford, UK
http://www.vanstraatensoft.com

 
 
 

string test @(xx|yy|zz) in bash shell ?

Post by Christian Weisgerb » Sat, 20 Nov 1999 04:00:00



> #! /bin/sh OR APPROPRIATE
> s_str=xx

> if [[ $s_str = $pattern ]]; then
>     echo $s_str
> fi
> This works (i.e., I see the echo) in the GNU bash shell 1.14.7 for
> Windows,

That would mean the bash 1.14.7 on Windows is substantially different
(incorporating the [[ command and extended globs from bash 2.0)
than the very same version on other platforms. I don't believe
this.

Quote:> and in the MKS Korn shell.

ksh88: no output
ksh93: xx
bash2: xx        (if "shopt -s extglob" is set)

Apparently in ksh88 and pdksh the righthand side of expressions of
the type [[ ... = ... ]] is not treated as a pattern if it results
from variable expansion.

> In the pdksh of FreeBSD, it works

> "if [ $s_str = +(xx|yy|zz) ]; then"

      ^                      ^


different patterns that just happen to be equivalent in this
particular example.

Quote:> On the Apache server of my ISP, which runs GNU bash 1.14.7 under
> i386-linux, I tried various combinations of:
> "if [ $s_str = +(xx|yy|zz) ]; then",
> but I never saw an echo.
> How does it work generally in the bash shell?

The following works in plain Bourne and all compatible shells (ksh,
bash, etc.):

case $s_str in
    xx|yy|zz) echo "$s_str" ;;
esac

Quote:> And, is there a possibility to match substrings, i.e.,
> match "xx" out of "123xx456"??

case $s_str in
    *xx*)  ... ;;
esac

--