The key to using command-line arguments is that the shell script needs to interact with the person who is running the script.
The bash shell provides command-line arguments to add data values after the command, command line options to modify the command behavior of single character values, and direct read keyboard input.
1, command line parameters
The most basic way to pass data to a shell script is to use command-line arguments.
1) Reading parameters
Variables that read the input parameters are positional parameters, and positional parameters are represented by standard numbers.
Where $ is the program name, the first argument, the second argument, and so on, until the $ $ is the Nineth parameter.
The shell script automatically assigns command line arguments to each position variable.
When you enter multiple parameters that can be numeric or string, you must use space-delimited to include spaces in parameter values, you must use single or double quotes)
When there are more than 9 arguments, you must enclose the variable in the shell script, such as ${10}, using curly braces. So that you can use any of the parameters.
2) Read program name
The string passed to the variable $ is actually the path of the program depending on the calling method to determine whether it is a relative or absolute path.
You can use the basename command to remove the path prefix and get only the program name name in which there is no space.
3) test Script
An error occurs when the script thinks that it should contain parameters but actually has no data.
A good way to do this is to check the parameters to make sure that the data exists before using the parameters, and you can use the-n parameter to check.
Example: If [–n "$"] then. else.. Fi
2, the special parameter variable
Used to track command line arguments
1) Parameter Count
Use special variable $ #测试执行脚本时包含的命令行参数个数. You can use $# anywhere in your script
Example: If [$#–ne 2] You can test the number of parameters
You can use ${!#} to return the last command-line argument when there is no argument, $ #为0, and ${!#} is the program name)
2) get all the data
Variable $* handles all the arguments provided in the command line as a single word, which takes multiple parameters as one argument.
Variable $@ handles all the arguments provided in the command line as multiple words in the same string. Allow the values in them to be iterated for general use for), separating different parameters
3. Shift
The shift command can change the relative position of the command line arguments. Default to move each parameter variable to the left one position variable $ unchanged, discard, pay attention to can not be restored! )
This is a good way to iterate over parameters without knowing the number of parameters.
You can provide a parameter for shift to achieve multiple-shift changes.
4, processing options
An option is a single letter that is booted by a dash to change the behavior of the command.
1, find the option
1) Processing Simple options
You can use the same method processing options as the command-line arguments to use the case statement to determine whether the option format is eligible.
2 separating options from parameters
When you use both options and parameters, you can use--to indicate the end of the list of options. Discover--After the shell knows the generic parameters, stop using the case processing option.
3 processing of options with values
Option followed by the parameter value, one method is to use shift and read the latter parameter after the corresponding option in the case. A better approach is as follows:
2, using the getopt command
The getopt command is handy when working with options and parameters. It organizes the parameters to facilitate parsing
1) command format
Getopt can accept any form of options and parameter lists and automatically convert them to the appropriate format.
The command format is: getopt options optstring Parameters
The option string (opstring) is used to define valid option letters in the command line, and which option letters require parameter values.
2 Use getopt in scripts
You need to use the SET command to replace existing command-line options and parameters with the formatted form generated by the getopt command.
You need to give the original script command-line arguments to the getopt command, and then output the getopt command to the SET command, as follows: set– ' getopts–q ab:cd ' $@ '
However, the getopt command does not handle parameter values with spaces very well, and it resolves the spaces into parameter delimiters instead of merging two values from double quotes into one argument. The solution is as follows:
3 more advanced getopts command
Getopts the order of command to handle existing shell parameter variables, one at a time, processing only the parameters detected in the command. After all parameters have been processed, exit with an exit state greater than 0.
Ideal for parsing all command-line arguments in a loop
The format is: getopts optstring variable
$optarg The value that contains the option to use for the parameter value $optind contains the position in the argument list when getopts stops processing.
Note: When getopts is processed, the previous option is removed, so no dashes are required in the corresponding case.
Good features:
1 can include spaces in parameter values
2 There can be no space between the option letter and the parameter value
3 The undefined options found on the command line are bound to a single output-the question mark
5. Standardized options
There are some letter options that have the standard meaning. It's best to define option meaning by standard meaning
-a–c–d–e–f–h–i–l–n–o–q–r–s–v-x–y
6. Get user input
When you need to obtain input from the execution script during execution, use the Read command
1) Basic Reading
The read command accepts standard input or other file descriptor input. After reading, put the data into a standard variable.
-P allows you to specify a prompt directly on the read command line.
You can specify multiple variables, or you can not specify that they will be placed in the reply environment variable.
2) Timing
Use-T to specify a timer that is full and not entered, and read returns a non-0 exit state.
Use-N to specify the number of characters you enter, and then automatically end the input when you reach a predetermined number
3) Reading silently
Use-s to make input not appear in terminal such as Enter password
4) Read the file
The most common method is to use the Cat command and pass it to the while statement containing read.
Example:
To copy Code code examples:
Copy Code code as follows:
Cat Test | While Read line