The Bash shell provides a number of different ways to get data from users, including the following 3 methods:
- Command line arguments (data added in the back of fame)
- command-line Options (single letter with modifiable command behavior) main getopt, getopts command
- Ability to read input directly from the keyboard
13.2-Option Handling
The so-called option is the single letter that follows the dash, which can change the behavior of the command. three ways to handle options in scripting:
- Find options
- getopts command
- getopt command
1. Find Options
1.1 Handling Simple options
The shift command moves each parameter variable one position to the left by default. The value of $ $ will be moved to $1, and the value of $ $ will be removed.
Shift n Indicates the number of bits moved
The above script executes a while loop by testing the length of the first parameter value, and when the first parameter has a length of 0 o'clock, the loop ends, and the first parameter is tested, the shift command moves all the parameter positions one position
1.2 Separating parameters and options
What to do with parameters and options at the same time, Linux uses special characters--separating them, which tells the script when the option ends and when the normal parameters start.
1.3 Handling options with values
Sometimes the option takes an extra parameter value. The script must be detected and handled correctly at this time.
2. getopts command
Because of the flexibility of the shell command line, the complexity is higher when you write code judgments. To this end, C provides functions such as Getopt/getopt_long, C + + boost provides the options library, in the shell, handling this matter is getopts and getopt.
Comparison of the two
- Getopts is a shell built-in command, Getopt is a standalone external tool
- Getopts use syntax is simple, getopt use syntax is more complex
- Getopts does not support long parameters (such as:
--option
), getopt support
- Getopts does not rearrange the order of all parameters, getopt will rearrange the order of the parameters (the difference here is explained below)
- The purpose of getopts is to replace getopt's faster execution parameter analysis work.
Typical use of parameter passing:
- ./test.sh-a-b-c: Short options, no parameters required for each option
- ./TEST.SH-ABC: The short option, like the effect of the previous method, simply writes all the options together.
- ./test.sh-a Args-b-C: Short option, where-a requires parameters, and-b-c does not require parameters.
- ./test.sh--a-long=args--b-long: Long Options
This section first describes the getopts command .
- getopts command built into the bash shell
- getopts command can be used in conjunction with existing shell parameter variables
- getopts command, each time it is called, it processes only one parameter detected on the command line at a time
- getopts command format getopts optstring variable
- getopts command, valid option letters are listed in optstring, if the option letter requires a parameter value to add one: To remove an error message, you can add a opstring before:
- The getopts command uses two environment variables, and if the option needs to be followed by a parameter value, the OPTARG environment variable will save the value.
#-o represents a short option, two colons indicates that the option has an optional parameter, the optional parameter must be close to the option
#如-carg and cannot be-c arg
#--long = Long option
# "[Email protected]" explained above
#-N: Information on Error
#--: An example is better understood:
What will you do #我们要创建一个名字为 the "-F" directory?
# mkdir-f #不成功, because-f is parsed by mkdir as an option, you can use
# mkdir---F so-F will not be used as an option.
Command-line arguments run to add data to the command line when the script is run.
Self-study linux shell13.2-option handling (main getopt, getopts command)