Bash shell provides command-line parameters to add data values after commands), command-line options to modify single-character values of command behavior) and directly read keyboard input.
1, command line parameter
The most basic way to pass data to a shell script is to use command line parameters.
1) Read parameters
The variables of input parameters are position parameters, which are represented by standard numbers.
Where $0 is the program name, $ 1 is the first parameter, $2 is the second parameter, and so on until $9 is the ninth parameter.
Shell scripts automatically assign command-line parameters to various positional variables.
When you enter multiple parameters at the same time, you must use spaces to separate them. To include spaces in parameter values, you must use single or double quotes. )
When there are more than 9 parameters, variables must be enclosed in braces in the shell script, such as ${ 10}. Any number of parameters can be used.
2) read the program name
The string passed to the variable $0 is actually the path of the program.
Use the basename command to delete the path prefix and only get the program name.
3) Test scripts
When the script thinks it should contain parameters, but there is actually no data, an error will occur.
A good way is to check the parameters before using them to make sure there is data. You can use the -n parameter to check.
Example: If [–n "$1"], then ... other ... ships will not bear the loading fee.
2. Special parameter variables
Used to track command line parameters.
1) parameter count
Use the special variable $ # to test the number of command line parameters included when executing the script. You can use $ # anywhere in the script.
Example: if [$# -ne 2] can test the number of parameters.
You can use $ {! #} Returns the last command line argument. When there are no parameters, $ # is 0, and $ {! #} is the program name)
2) get all the data
The variable $ * treats all the parameters provided on the command line as one word and multiple parameters as one parameter.
The variable $ @ treats all parameters provided on the command line as multiple words in the same string. Iteration of values is allowed, and for) is generally used to separate different parameters.
Step 3 change
The shift command can change the relative positions of command line parameters. Silently move each parameter variable to the left by a position variable of $0, and discard $ 1. Be careful not to recover! )
This is a good way to iterate parameters when the number of parameters is unknown.
You can provide a parameter for shift to achieve a variety of displacement changes.
4. Processing options
An option is a single letter that starts with a dash and is used to change the behavior of a command.
1, find the option.
1) handling simple options
Options can be treated like command-line parameters, and case statements can be used to determine whether options conform to the format.
2) Separate options from parameters.
When using both options and parameters, you can use-to indicate the end of the list of options. When-is found, the shell will know that it is followed by a normal parameter and stop using the case handling option.
3) Handling options with values
Option followed by parameter value. One way is to use shift and read the last parameter after the corresponding option in the case. The better method is as follows:
2. Use the getopt command.
The getopt command is very convenient when dealing with options and parameters. It reorganizes the parameters for analysis.
1) command format
Getopt can accept any form of list of options and parameters and automatically convert them into an appropriate format.
The command format is: getopt option optstring parameter.
Opstring is used to define valid option letters on the command line and which option letters need parameter values.
2) using getopt in the script
You need to use the set command to replace the existing command line options and parameters with the formatted form generated by the getopt command.
You need to send the original script command line parameters to the getopt command, and then send the output of the getopt command to the set command, as follows: set-`getopts–q ab: cd "$ @" `.
However, the getopt command cannot handle parameter values with spaces well. It parses spaces into parameter delimiters instead of combining two values enclosed in double quotation marks into one parameter. The solution is as follows:
3) More advanced getopts command
The getopts command processes the existing shell parameter variables in sequence, and only processes one of the parameters detected in the command each time it is called. After all parameters are processed, exit with exit status greater than 0.
Very suitable for parsing all command line parameters in a loop.
Format: getopts optstring variable
$optarg contains values for options that require parameter values, while $optind contains the position of getopts in the parameter list when processing stops.
Note: When dealing with getopts, the-before the option will be deleted, so there is no need to dash in the corresponding situation.
Good features:
1) can include spaces in parameter values.
2) There should be no space between the option letter and the parameter value.
3) Bind all undefined options in the command line into an output question mark.
5. Standardization options
Some alphabetic options have standard meanings. It is best to define the meaning of the option according to the standard meaning.
-a–c–d–e–f–h–I–l–n–o–q–r–s–v-x–y
6. Get user input
Use the read command when you need to get input from the script executor during execution.
1) Basic reading
The read command accepts standard input or other file descriptor input. Put the data into the standard variable after reading.
-p allows you to specify a prompt directly on the read command line.
You can specify multiple variables, or you can not specify to put it in the reply environment variable)
2) Timing
Use -t to specify a timer, and read returns a non-zero exit state before the timer expires.
Use -n to specify the number of characters to enter. When the input reaches a predetermined number, it will automatically end the input.
3) Silent reading
Use -s to make the input not displayed on the terminal, such as entering a password)
4) reading files
The most common method is to use the cat command and pass it to the while statement containing read through the pipeline.