
Is there switch and case in sh script?
In article <59d45h$...@gondor.sdsu.edu>, masc0...@rohan.sdsu.edu
Switch is a Csh concept. The Bourne shell uses the Case statement, as
others have posted that solution to you already. However, for what you
want, you might consider Kornshell and it's getopts function. Getopts is
Kornshell's way of parsing command line flags. You usually see it within a
while loop and case statement. The Getopts function allows you to combine
the flags (-rfo would be legal in your case, and allows you to. It's a
little hard to explain, but here it is:
USAGE="Here's how to use my program...
blah, blah, blah
"
while getopts :rfo: option
do
case $option in
r) rflag=true;;
f) fflag=true;;
+f) plusFFlag=true;;
o) oflag=true
file=$OPTARG
;;
*) print $USAGE
exit 2
;;
esac
done
shift OPTIND - 1
The :rfo: tells getopts what are valid flags. The colon in front tells
getopts not to print an error message if an invalid flag is found. Each
valid flag is listed, and if a flag requires an option, that flag is
follow by a colon.
The flags are "set" in the case statement. Notice I added a "+f" line.
This was NOT part of your requirements, I just put that in to show you how
to check for command line parameters that start with a "+" sign.
Standard Kornshell procedure says you set the flags you want, then check
them further in your code. The idea is to keep the parsing of the flags
clean as possible. It also tends to greatly simplify your code.
Hope this helps.
--
David Weintraub _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Deutsche Bank, NA _/ _/
dw...@dbna.com _/ I AM THE GREAT AND POWERFUL OZ* _/
dav...@cnj.digex.net _/ _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*(Pay no attention to the man behind the curtains)