Ksh "getopts" - uninstall option

I use the built-in “getopts” ksh to handle command line options, and I'm looking for a clean / standard way to “undo” the option on the command line. I do not know if this is a technical question about getopts or more a question of style / standards. In any case, I understand that getopts processes its arguments (by default $ *) from left to right, and I hope to use this so that the second option specification “disables” the option.

For parameters that take a parameter argument, this is not a problem. In my case, to handle the getopts register, I just say, for the -x option, for example:

x ) x_arg=$OPTARG
    ;;

and the last instance of "-x" on the command line wins, and that is what I want.

The problem arises with parameters that do not accept the option argument. If this parameter is just a switch / flag, then my case statement will have something like:

x ) x_specified=1
    ;;

With an option like this, all I really can do is add "-x" again on the command line and the exact same thing will happen: "x_specified = 1" is executed again. What is the best / easiest / easiest / standard way to "unset x_specified"? This is my question.

getopts, "+" , "-" , , getopts , "+" "-". , , "+" . , , - :

x ) if [[ -z $x_specified ]] ; then
        x_specified=1
    else
        unset x_specified
    fi
    ;;

"-x" ; , , . , , : " -x", " -x". , . "X", , , , , , "" off -x ", , , , .

, : " -x , ?" . , , . , , , .

+3
1

ksh93, +, optstring "+":

while getopts +abc opt; do
    echo "have option '$opt'"
done

:

$ ./args.ksh -a -b -c +b -b foo 
have option 'a'
have option 'b'
have option 'c'
have option '+b'
have option 'b'
$ ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01

, : , ":" "" optstring.

http://linux.die.net/man/1/ksh93

+1

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


All Articles