getopts optstring name [args]
Three environment variables referenced by getopts
optarg: The value of the option parameter that was processed by the getopts built-in command, option argument (hold option parameter), the getopts command puts it in the variable optarg when an option parameter is required for the selected item
optind: The next sequence number of parameters to be processed by the getopts built-in command, option Index, which will be incremented by the initial value of 1 for each invocation of the script.
opterr: If set to 1,bash, getopts error will be displayed. Set to 0, getopts error is not displayed.
The process of getopts
When the script is invoked, Optind is initialized to 1. Each time getopts is called, the next option value is assigned to name, the option index value Optind also points to the next location to process the option, and the option parameter is assigned to Optarg
Getopts 's design goal is to run in a loop, each time it executes,getopts checks the next command-line argument and determines if it is valid. (That is, check whether the parameter starts with -- followed by a letter contained in the opstring).
Valid, the matching option letter exists in the specified variable variable , and returns the exit status 0 (ture);
Invalid ( if - The following letter is not included in the options), it is stored in the variable . , and returns exit status 0If no arguments are already in the command line, or if the next parameter does not start with - the exit status of 0 is returned (false, which can be used to end the while Cycle ).
Getopts after all parameters are processed, a non-0 value (False, Exit loop) is returned, at which point the Optind index value points to the first non-option parameter [args],name?
Getopts Error Handling
When the command is used correctly, name is used to store option, $OPTARG the parameter used to hold option. If the command is entered incorrectly (invalid option, missing parameter),getopts will handle illegal option error and Miss option argument error. The processing result differs from whether the opstring begins with: Opstring: The error message that is used to mask getopts processing (the same effect can be achieved by placing opterr in 0 in the script).
Use the following test case getopts.sh to quickly verify how Getopts handles the error
Opstring value ": Ab:c:" and "AB:C:"
./getopts.sh-a-B ok-c No
./getopts.sh-a-B
./getopts.sh-w
commonly used in scripts ? catch error,Name,optarg treats the value of the error can be used to define the output of the error message itself.
Example
|
Opstring
|
Type of error
|
Name
|
Optarg
|
./getopts.sh-a-B
|
" : ab:c: "
|
Missoption argument
|
:
|
B
|
./getopts.sh-w
|
" : ab:c: "
|
Illegal options
|
?
|
W
|
./getopts.sh-a-B
|
"Ab:c:"
|
Missoption argument
|
?
|
Unsetoptarg
|
./getopts.sh-w
|
"Ab:c:"
|
Illegal options
|
?
|
unset Optarg (cancel variable )
|
Special Instructions
: ? and is not used as a choice character for special purposes.
Getopts allows options to be stacked together (e.g.-ms)
Opstring contains an option string that can be put together, if the option is followed by a parameter that is required in the call of the option, the invocation of such an option is not stacked with other options.
The same shell environment executes getopts multiple times, the optind is not reset, and when the parameter is recalled, the Optind must be reset manually if necessary.
Getopts returns 0 (TRUE) when a defined or undefined option is found; Returns a value other than 0 (false) if the parameter is processed or if an error is encountered