I wrote a shell script to do something. Everything works perfectly! Part of my code is something like this, so that I can pass parameters and arguments to it:
while getopts "a:b:c" opt
do
case $opt in
a) AA=$OPTARG ;;
b) BB=$OPTARG ;;
c) CC=$OPTARG ;;
esac
done
shift $(expr $OPTIND - 1)
But now I want to add a parameter, say d, and I don’t want it to use any argument as shown below, but it ends up with an error, complaining that it option requires an argument error -- dseems like getopts may not do this.
while getopts "a:b:c:d" opt
do
case $opt in
a) AA=$OPTARG ;;
b) BB=$OPTARG ;;
c) CC=$OPTARG ;;
d) DD="ON" ;;
esac
done
shift $(expr $OPTIND - 1)
What should I do then? What is the usual practice, if I need both types of options, can I accept an argument, and the other is not needed?
source
share