Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a reason for using shift $((OPTIND-1)) after getopts?

Tags:

bash

I realise that shift moves the array of cli args n space to the left, and the default of n is 1. This means I can assign the values the array to existing varibles using $1 shift inside a while loop. What I don't quite understand is why it is used in this context below. The input args have been assigned to values already and deleting shift $((OPTIND-1)) doesnt change this fact. Source: http://linux.die.net/man/3/optind

while getopts ":h:a:fc" opt; do
    case $opt in
        h)
            print_help
            exit 0
            ;;
        a)
            aaaa=${OPTARG}
            ;;
        f)
            force=1
            ;;
        c)
            CLEAN=1
            ;;
        \?)
            echoerr "Invalid option -$OPTARG"
            print_help
            exit 1
            ;;
    esac
done

shift $((OPTIND-1))
like image 614
Dave Avatar asked Oct 10 '14 07:10

Dave


People also ask

What is Optind in getopts?

According to man getopts , OPTIND is the index of the next argument to be processed (starting index is 1). Hence, In sh foo.sh -abc CCC Blank arg1 is -abc , so after a we are still parsing arg1 when next is b ( a 1 ).

What is the purpose of getopts command?

The getopts command is a Korn/POSIX Shell built-in command that retrieves options and option-arguments from a list of parameters. An option begins with a + (plus sign) or a - (minus sign) followed by a character. An option that does not begin with either a + or a - ends the OptionString.

What does getopts mean in Bash?

Updated: 02/01/2021 by Computer Hope. On Unix-like operating systems, getopts is a builtin command of the Bash shell. It parses command options and arguments, such as those passed to a shell script.

Should I use getopt or getopts?

The main differences between getopts and getopt are as follows: getopt does not handle empty flag arguments well; getopts does. getopts is included in the Bourne shell and Bash; getopt needs to be installed separately. getopt allows for the parsing of long options ( --help instead of -h ); getopts does not.


1 Answers

The shift removes the parameters processed by the getopts loop from the parameter list, so that the rest of the script can process the remainder of the command line (if any) with $1... in the usual manner, without concern for the number of options processed by getopts.

Consider a hypothetical script with the usage

frobble [-f] [-c] [-a hostname] filename ...

The getopts loop above takes care of parsing the -f,-c and -a, if they are present, but doesn't remove them from the argument list. Which means that to get at your filename argument, you need to find out how many options were processed, and continue processing from there. Conveniently, getopts tells you the index of the first unprocessed argument: the variable OPTIND.

And instead of messing with offsets and stuff, you can just discard the processed options, renumbering the rest of the arguments so your filename is always $1.

That is what shift $((OPTIND-1)) does.

like image 175
Peder Klingenberg Avatar answered Oct 01 '22 09:10

Peder Klingenberg