>I'm trying to write a dinky sh script which contains the following
>while loop:
>while test "$1" != ""
>do
> case "$1" in
> -i) input=$2; shift ;;
> [+\-][0-9]*) version=$1 ;;
> -v) month=$2; day=$3; shift; shift ;;
> -d) del="yes" ; echo "XX" ;;
> -*) echo "get: unknown argument $1" 1>&2; exit 1 ;;
> *) file=$1 ;;
> esac
> shift
>done
>The echo "XX" statement is only for debugging.
>Anyway, when $1 is "-d", the test statement exits false, and the -d)
>line in the case statement is not executed. Any arguments after this
>one are ignored by the while statement. When I change the line in the
>case statemet to -b) , the script works as expected.
>What gives?
>k
It looks as though you're getting some interaction with test. I
suspect that both -d and -b are being passed as options to test.
Why test doesn't barf on -b, I don't know; it looks to me like
neither should work.
In any case, when you're processing every word on the command line,
it's better to test the count of arguments being greater than 0:
while [ $# -gt 0 ] ; do
case $1 in
...
esac
shift
done
Also, if you're going to be processing options that have their own
arguments, it's better to bite the bullet and use getopts(1) if you
have it, or getopt(1) if you don't. Wierd cases come up when a
command requires that an option's argument take the form of a
suffix. SCCS commands do that. So what getopts (or getopt) pulls
apart, you sometimes have to put back together:
while getopts abo: c
do
case $c in
a | b) opts="$opts -$c" ;;
o) opts="$opts -${c}${OPTARG}" ;;
\?) echo $usage
esac
done
shift `expr $OPTIND - 1`
The "o" case sticks the option and argument back together.
-r