The code below is an mwe based on a script mwe I've written where I specify flags in what I think is the usual way. But I'm seeing really wierd behavior. If I type mwe -e or mwe -n it thinks there are no arguments and returns no arg. If I type mwe -k or mwe -i it thinks argType is not "-" and returns breaking. If I comment out the four lines that end with a # the code works as expected. That suggests the problem is being caused by the while loop. Could somebody please explain what's happening?
#!/bin/bash
foo=0
argType=`echo "$1" | cut -c 1`
while [ 1 -gt 0 ] ; #
do #
if [ $# -eq 0 ] ; then
echo no arg
exit
elif [ "$argType" != "-" ] ; then
#No more flags
echo breaking
break #
elif [ "$1" = "-n" ] ; then
foo=1
shift
elif [ "$1" = "-e" ] ; then
foo=2
shift
elif [ "$1" = "-i" ] ; then
foo=3
shift
elif [ "$1" = "-k" ] ; then
foo=4
shift
fi
done #
echo This is foo: $foo
-nand-eare likely being consumed as arguments toecho...getoptswill handle most of this for you?getopts, which also has an example of usage.