Option requires an argument error

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?

+4
source share
2 answers

:in the specifier is getoptsnot a separator. From man getopts:

, , , .

, , , . , , , :.

+12

, d "a:b:c:d".

, -d - .

, ? wiki 2 : " . , . ."

0

Source: https://habr.com/ru/post/1531599/


All Articles