getopts Command
Use
Processes command-line arguments and verifies valid options.
Grammar
getopts option string name [parameters ...]
Describe
Getopts is designed to run in a loop, and each time a loop is executed, getopts checks the next command-line argument and determines if it is legitimate. That is, check whether the parameter starts with a--followed by a letter contained in the options. If it is, put the matching option letter in the specified variable variable and return exit status 0; If-the following letter is not included in the options, it is stored in the variable. , and returns exit status 0 if no arguments are already in the command line, or if the next parameter does not start with-the exit status is not 0.
The shell variable Optind. Once you call the Shell, Optind is initialized to 1. The selected item starts with A + and the + is pre-set to the value in the name.
if the character in the option string is followed by a ":" (colon), this option is expected to have parameters. When an option parameter is required for an election,thegetopts command places it in the variable optarg .
When you find that the option string does not contain an option character, or if you find an option that does not have the required option parameter:
- If the option string does not start with :(colon), the name will be set to the? (question mark) character, Optarg. will be set, and the diagnostic message will be written to the standard error.
This is considered to be an error that is detected during the process of passing parameters to the calling application, rather than in the process of processing the getopts command; As described above, a diagnostic message is written, but the exit status becomes 0.
- If the option string starts with a :(colon), the name is set to? (question mark) character, which is for an unknown option, or set to the missing required option: (colon) character, Optarg will be set to the found option character, and no output is written to the standard error.
Example
- The following Getopts command stipulates that a, B, and C are valid options, and options A and C have parameters:
Getopts a:bc:opt
- The following getopts commands specify a, B, and C as valid options, and options A and B have parameters, and when getopts encounters the option defined at the command line, it sets the value of OPT to:
Getopts:a:b:c OPT
- The following script parses and displays its parameters:
aflag= bflag= while getopts ab:name do
Case $name in
a) aflag=1;;
b) bflag=1 bval= "$OPTARG";;
?) printf "Usage:%s: [-A] [-B value] args\n" $ exit 2;;
Esac done if [!-Z "$aflag"]; Then printf "option-a specified\ n" fi if [!-Z "$bflag"]; Then printf ' option-b '%s ' specified\ n ' "$bval" fi Shift $ (($OPTIND-1)) printf "Remaining arguments is:%s \ n "" $* "
The following uses are available in bash:
optstring Optionstring that will match one by one
VarNameoptions for each successful match
Argparameter list, which takes command line argument list when not written
$OPTINDSpecial variables,Option Index, it increments one by one
$OPTARGSpecial variables,option Argument, there are different values in different cases
Rule 1: When optstring begins with ":", getopts distinguishes between invalid option error and miss option argument error.
Invalid option, the varname will be set to, $OPTARG is the problem of option;
Miss option argument, VarName will be set to:, $OPTARG is the problem option.
If Optstring does not start with ":", invalid option error and miss option argument error will make
VarName is set to, $OPTARG is the problem option.
Rule 2: When the letter in the Optstring ":", indicating that the option can be connected to parameters, parameters (argument) placed in the $optarg;
If a parameter is missing and optstring begins with ":", the value of VarName is:, $OPTARG is the option,
Otherwise the value of VarName is?, $OPTARG is the option. (Refer to Rule 1)
Example: gg.sh
[email protected] shel]# cat gg.sh
#gg. Sh
#!/bin/bash
While getopts "Abc:def:ghi" flag
Do
echo "$flag" $OPTIND $optarg # here $optind is an index sequence number, $OPTARG is the value recorded in the option, no value is empty, by default the option is separated by a space
Done
echo "Resetting"
Optind=1 while getopts "Abc:def:ghi" flag
Do
echo "$flag" $OPTIND $OPTARG
Done
[Email protected] shel]#/gg.sh-ab-c foo-f "foo bar"-h-gde
A 1
B 2
C 4 Foo
F 6 Foo Bar
H 7
G 7
D 7
E 8
Resetting
A 1
B 2
C 4 Foo
F 6 Foo Bar
H 7
G 7
D 7
E 8
The above is the display result.
If you adjust the position of the given parameter:
[Email protected] shel]#/gg.sh -abcfoo -F "foo bar"-h–gde a 1
B 1
C 3 Foo
F 5 Foo Bar
H 6
G 6
A S
E 7
Resetting
A 1
B 1
C 3 Foo
F 5 Foo Bar
H 6
G 6
A S
E 7
#!/bin/bash
While getopts h:ms option
Do
Case ' $option ' in
H
echo "option:h, Value $OPTARG"
echo "Next arg index: $OPTIND";;
M
echo "Option:m"
echo "Next arg index: $OPTIND";;
S
echo "Option:s"
echo "Next arg index: $OPTIND";;
\?)
echo "Usage:args [-H n] [-m] [-S]"
echo "-H means hours"
echo "-M means minutes"
echo "-s means seconds"
Exit 1;;
Esac
Done
./args-h 100–ms
Option:h, Value 100
Next arg index:3
Option:m
Next arg index:3
Option:s
Next arg index:4
Do something now * * *
Note:
1.getopts allows options to be stacked together (e.g.-ms)
2. If you want to take parameters, the corresponding options should be added after: (such as h after the need to add parameter h:ms). At this point the options and parameters are separated by at least one white space character, so options cannot be stacked.
3. If a parameter is not found after the option that requires it, it is stored in the given variable. and writes an error message to the standard error. Otherwise, the actual parameters are written to the special variable: optarg
4. Another special variable: Optind, which reflects the next parameter index to be processed, the initial value is 1, and is updated each time the getopts is executed.
Linux command parameters processing shell script function getopts