[NTLUG:Discuss] More shell scripting madness
Chris Cox
cjcox at acm.org
Thu Jul 5 14:11:22 CDT 2007
. Daniel wrote:
...
> But while hacking around, I came up with a question I haven't yet figured
> out.
>
> How can I set up an expression that tests if one string is found within
> another. Let's say, for example, A="ABCDEFGHIJK" and B="CDEF". Obviously
> $B can be found within $A. But how to set up an expression where the
> result is true or false and can run a conditional off of the result?
>
if expr "$A" : ".*$B" >/dev/null; then
echo found
fi
The matched expression is a RE (Regular Expression). You can use this
to parse out matched areas:
e.g.
expr "$A" : ".*\($B\)"
Globbing patterns are portable inside of switch statements...
case "$A" in
*$B*)
echo found
;;
*)
echo "not found"
;;
esac
In case that's more what you were looking for. Just remember that "$B"
can't contain unescaped pattern matching chars.
More information about the Discuss
mailing list