Further to my comment, the following is an example of using getarg in a corporate production environment:
## initialize vars
ouser="${USER:-`whoami`}"
ohost="${HOST:-`hostname`}"
set -- `getopt hHVu:n: ${*:-}`
test $? -ne 0 && usage && exit 9
for i in $*; do
case $i in
-h) usage 0 && exit 9 ;;
-H) usage 1 && exit 9 ;;
-V) echo "$P $rcsid"|cut -d' ' -f1,4-5; exit;;
-u) ouser=$2 ; shift 2 ;;
-n) ohost=$2 ; shift 2 ;;
--) shift ; break ;;
esac
done
My own preference is to bypass getargs and parse the command line directly as shown in the following example (note that parameter strings could just as well be single-character, i.e. "-x", but can be a mix or short or long parameters):
useUID=0
cUID="-99"
cOwner=""
cGID="-99"
cGroup=""
Owner=""
Group=""
VERB=""
SAMPLE=0
FORCE=0
SHOW=1
while [ $# -gt 0 ]
do
case $1 in
--verbose ) VERB="-v" ; shift ;;
--sample ) SAMPLE=1 ; shift ;;
--force ) FORCE=1 ; shift ;;
--nomatch ) SHOW=0 ; shift ;;
--match ) SHOW=1 ; shift ;;
--oUID ) cUID=$2 ; useUID=1 ; shift ; shift ;;
--oUSR ) cOwner=$2 ; useUID=0 ; shift ; shift ;;
--oGID ) cGID=$2 ; useGID=1 ; shift ; shift ;;
--oGRP ) cGroup=$2 ; useGID=0 ; shift ; shift ;;
--nUSR ) Owner=$2 ; shift ; shift ;;
--nGRP ) Group=$2 ; shift ; shift ;;
esac
done
or again as shown here:
ASSIGN=0
REPORT=0
VERB=0
SINGLE=0
USB=0
while [ $# -gt 0 ]
do
case ${1} in
--default ) REPORT=0 ; ASSIGN=0 ; shift ;;
--report ) REPORT=1 ; ASSIGN=0 ; shift ;;
--force ) REPORT=0 ; ASSIGN=1 ; shift ;;
--verbose ) VERB=1 ; shift ;;
--single ) SINGLE=1 ; shift ;;
--usb ) USB=1 ; shift ;;
* ) echo "\n\t Invalid parameter used on the command line. Valid options: [ --default | --report | --force | --single | --usb | --verbose ] \n Bye!\n" ; exit 1 ;;
esac
done
Many times, the variables set from the input parsing are strictly boolean settings for conditional testing before taking actions.