>I'd like an if-test (in bash or ksh) that matches a string >against a regexp. The best I can come up with is > >if { echo $givenstring | grep "regexp" > /dev/null ; } >then et cetera >fi
First, using expr can simplify the expression some:
if expr "$givenstring" : "regexp" then et cetera fi
(Note that expr implictly anchors the regexp on both ends, so you need to add a .* to any end which you don't want anchored.)
Next, sometimes a full-blown regexp isn't really necessary, and the glob-matching of "case" is sufficient.
Finally, bash with "shopt -s extglob" enabled and ksh both support special matching syntax in [[ ]] tests which give you regexp abilitites, albeit with non-standard syntax; for example the ERE "foo.b*a?r$" would be rendered:
if [[ $givenstring == *foo?*(b)?(a)r ]] then et cetera fi
Ken Pizzini
documented on: 1999.09.27 Mon 15:14:50