[clug] sh scripts and =~ in csh

Tony Breeds tony at bakeyournoodle.com
Tue Sep 11 03:59:51 GMT 2007


On Tue, Sep 11, 2007 at 12:32:31PM +1000, Andrew Janke wrote:
> Just "sh-ifying" and old csh script that has been lying about and
> cannot for the life of me remember the equivalent of this in sh...
> 
> set answer = "yes"
> if ("$answer" =~ y*) then
>    dostuff
> endif
> 
> ie: what is the equivalent of =~?

If you're writing /sh/ scripts I don't think there is a nice one.
something like (untested):
answer="yes"
if [ "$(echo "$answer" | sed -n -e '/y.*/ p')" == "$answer" ] ; then
	dostuff
fi

Might work.  However, if you're writing /bash/ sctipts I think you can
do:

answer="yes"
if [[ "$answer" =~ 'y.*' ]] ; then
	dostuff
fi

Yours Tony

  linux.conf.au        http://linux.conf.au/ || http://lca2008.linux.org.au/
  Jan 28 - Feb 02 2008 The Australian Linux Technical Conference!



More information about the linux mailing list