In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what are the command line parameters of shell script". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the command line parameters of the shell script.
The key to using command-line arguments is that the shell script needs to interact with the person who runs the script.
Bash shell provides data values for command-line parameters to add to commands, single-character values for command-line options to modify command behavior), and direct reading of keyboard input.
1. Command line parameters
The most basic way to pass data to a shell script is to use command-line arguments.
1) read parameters
The variable that reads the input parameter is the position parameter, which is represented by the standard number.
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.
The shell script automatically assigns command-line parameters to each location variable.
When entering multiple parameters at the same time, they must be separated by spaces. If you want to include spaces in the parameter values, you must use single or double quotation marks)
When there are more than nine parameters, you must enclose the variables in curly braces in the shell script, such as ${10}. So that any parameter can be used.
2) read the program name
The string passed to the variable $0 is actually the path of the program that determines whether it is relative or absolute based on the calling method.
Using the basename command, you can remove the path prefix and only get the program name without spaces.
3) Test script
An error occurs when the script thinks it should contain parameters but actually has no data.
A good way is to check the parameters to ensure that the data does exist before using the parameters, and you can use the-n parameter to check.
Example: if [- n "$1"] then.. Else.. Fi
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 ${! #} to return the last command line argument when there is no argument, $# is 0, and ${! #} is the program name)
2) get all data
The variable $* treats all parameters provided on the command line as one word, and it treats multiple parameters as one parameter.
The variable $@ treats all parameters provided on the command line as multiple words in the same string. Allows you to iterate over the values, usually using for), separating the different parameters
3. Shift
The shift command can change the relative position of command-line arguments. By default, move each parameter variable to the left by one location variable $0 unchanged, discard $1, and note that it cannot be restored! )
This is a good way to iterate parameters when the number of parameters is not known.
A parameter can be provided for shift to realize multi-displacement variation.
4. Processing options
An option is a single letter guided by a dash to change the behavior of the command.
1, find the options
1) handle simple options
Options can be processed in the same way as command-line parameters, and case statements are used to determine whether they conform to the option format when extracting.
2) detach options from parameters
When using both options and parameters, you can use-- to indicate the end of the list of options. After discovering-- shell knows that what follows is the normal parameter, and stops using the case processing option.
3) deal with options with values
Options are followed by parameter values. One way is to use shift and read the next parameter after the corresponding option in case. A better way is as follows:
2, use the getopt command
The getopt command is very convenient when working with options and parameters. It reorganizes the parameters for 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 on the command line and which option letters require parameter values.
2) use getopt in scripts
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 output the getopt command to the set command, as follows: set-`script-Q ab:cd "$@" `
However, the getopt command does not handle parameter values with spaces very well, it parses spaces into parameter delimiters rather than merging two values enclosed in double quotes into one parameter. The solution is as follows:
3) more advanced getopts commands
The getopts command processes the existing shell parameter variables sequentially, and each call processes only one of the parameters detected in the command. After all the parameters are processed, exit with an exit status greater than 0.
Ideal for parsing all command line arguments in a loop
Format: getopts optstring variable
$optarg contains the value to be used by options that require parameter values, and $optind contains the position in the parameter list when getopts stops processing.
Note: when getopts processes, the-before the option is removed, so there is no need for dashes in the corresponding case.
Good features:
1) you can include spaces in parameter values
2) there can be no spaces between option letters and parameter values
3) bind all undefined options found on the command line to a single output-question mark
5. Standardization options
There are some letter options that have standard meaning. 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
When you need to get input from the script executor during execution, use the read command
1) basic read
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 not specify that they will be placed in the reply environment variable)
2) timing
Use-t to specify a timer, the full number has not been entered, read returns a non-zero exit status.
Use-n to specify the number of characters entered, and automatically end the input when the input reaches a predetermined number.
3) Silent reading
Use-s so that the input is not displayed on the terminal, for example, enter a password)
4) read the file
The most common method is to use the cat command and pipe it to the while statement that contains read.
Example:
Code example:
The code is as follows:
Cat test | while read line
At this point, I believe you have a deeper understanding of "what are the command line parameters of the shell script?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.