Listing 1: getopts.sh
# getopts demo
while getopts abz: name
do
case $name in
a) aflag=1
;;
b) bflag=1
;;
z) zflag=$OPTARG
;;
\?) echo "unknown option, terminating "
exit 2
esac
done
shift `expr $OPTIND - 1`
otherargs=${1}
if ! [ -z "$aflag" ]
then # echo if set
echo "aflag set"
fi
if ! [ -z "$bflag" ]
then # echo if set
echo "bflag set"
fi
if ! [ -z "$zflag" ]
then # echo if set
echo "zflag is: "$zflag
fi
if ! [ -z "$otherargs" ]
then # echo if set
echo "otherargs is: "$otherargs
fi
|