Friday, October 22, 2010

shell case switch


#! /bin/sh
pattern="$1"
shift
echo " Matching against ' $pattern' : "
for string
do
  case $string in
  $pattern) echo " $string: Match. " ; ;
  *) echo "$string: No match. " ; ;
  esac
done

Save this script to a file named pattern, make it executable (chmod a+x pattern), and you 
can use it to perform your own tests:

$ ./pattern ' *' ' hello'
Matching against ' *' :
hello: Match.

$ ./pattern ' hello*' ' hello' ' hello, there' ' well, hello'
Matching against ' hello*' :
hello: Match.
hello, there: Match.
well, hello: No match.
Remember to use single quotes around the arg

No comments:

Blog Archive