
parse command line argument
| I need a script which can process command line argument with
|
| checker -Usa -Pxxx -Syyy
| or
| checker -U sa -P xxx -S yyy
| or
| checker -Usa -P xxx -S yyy
| or
| checker -Usa -Pxxx -Syyy
| basic combinations of all with 0 or more spaces between -{UPS} and actual valu
| -U needs to be sa, otherwise generating error message.
The Bourne shell doesn't have the string processing facilities built
in to break down strings, like extracting xxx from Pxxx, so we rely
on external commands like sed to do this. In this case, I'd make
sed do that in one pass over the entire argument list, so I don't have
to account for all these A B vs. AB variations in the shell logic.
set dummy `echo $* | sed 's/\(-[UPS]\)/\1 /g'`
shift
"sed" inserts a space after -U, -P and -S, and "set ``" installs the
result as the new argument list. "dummy" and "shift" prevent weird
behavior in the case where there are no arguments.
From here it's simple enough to rake in the arguments in a "while"
loop, using "shift" to advance to the next pair:
while :
do
case $# in 0) break;; esac
case $1 in
-U) ...;; -P) ...;; -S) ...;;
*) ... error ;;
esac
shift; shift
done
Donn Cave, University Computing Services, University of Washington
d...@u.washington.edu