> For example I got to run like this:
> do_sth.sh -NORMAL abc
First some general remarks:
- There is no need to us a .sh suffix.
- I personally don't like '_'. '-' is easier to type on "unix"
keyboards :-)
- It is very common to have lowercase options. Much easier to
type :-)
- IMHO started by the GNU guys is is not uncommon to use a
single '-' for one letter options (like '-u') and '--' for
long options.
So IMHO, it should be
do-sth --normal abc
Quote:> I like do_sth.sh to recognize "NORMAL" as an option (and take abc as
> some value), against other potential words, such as "URGENT" --
> instead of only "N" against "U" that getopt may help do.
> Please also let me know if I can do it in either sh, csh or ksh.
For what is /bin/sh on Solaris, the following works with short and log
options, and also recognizes '--' to indicate end-of-options - in case
you have an argument starting with '-'. You can add more consistency
checks to the case statements if you like.
#!/bin/sh
#
# Read options
#
while [ $# -gt 0 ] ; do
case "$1" in
--normal|-n)
if [ $# -lt 2 ] ; then
# some error message and exit
exit 1
fi
shift
normal="$1"
shift
;;
--urgent|-u)
if [ $# -lt 2 ] ; then
# some error message and exit
exit 1
fi
shift
urgent="$1"
shift
;;
--) # end of options
shift
break
;;
-*) # unknown option
# some error message
exit 1
;;
*) # no option, maybe an argument
break
;;
esac
done
#
# Read arguments, shift for every argument
#
# ...
# final check
if [ $# -gt 0 ] ; then
# some error message and exit
exit 1
fi
/Thomas