In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Command line argument
$0 represents the program name.
$1 to $9 are location parameters.
$# indicates the number of parameters.
$* refer to all parameters as a whole
$@ returns each parameter as a string, which can be traversed using a for loop
$? The exit status of the last executed command. 0 indicates successful execution
The last argument of $_ previous command. Use the shortcut key ESC+. It's the same effect.
Position parameter
There are more than 9 position parameters, and more parameters are also supported. Just use a reference in the form of\ ${10}.
The effect of\ $1 and\ ${1} is the same.
Without curly braces,\ $10 is considered\ $1 and a character 0.
Parameter values with spaces
Each parameter is separated by a space. To include spaces in parameter values, you must use quotation marks (either single or double quotation marks).
When you pass a text string as a parameter, quotation marks are not part of the data. They just indicate where the data starts and ends.
Get the script name
\ $0 represents the script name, but different calling methods return different results. The following script simply prints the value of\ $0:
$cat filename.sh #! / bin/bashecho $0 $. / filename.sh-bash:. /: is a directory $cat filename.sh #! / bin/bashecho $0 $. / filename.sh./filename.sh$ bash filename.sh filename.sh$ bash / root/filename.sh / root/filename.sh$
Use the basename command
If you want to use the script name to determine, you can first filter out the path information with the command basename. The effect of the command is as follows:
$basename / var/log/messages messages$
So the above script can be modified like this:
$cat filename.sh #! / bin/bashecho $(basename $0) $. / filename.shfilename.sh$ bash filename.shfilename.sh$ bash / root/filename.sh filename.sh$ test parameters
When using parameters in a script, make sure that the parameters exist, otherwise you may report an error at run time:
$cat add.sh #! / bin/bashecho $1 + $2 = $[$1 + $2] $. / add.sh 1 21 + 2 = 3 $. / add.sh 1./add.sh: line 2: 1 +: syntax error: expected Operand (error symbol is "+") $
If it is only referenced as a string, it will not report an error. Parameters without passing parameters are empty by default:
$cat hello.sh #! / bin/bashecho Hello $1 $2.$. / hello.sh Tom JerryHello Tom Jerry.$. / hello.sh JerryHello Jerry.$. / hello.shHello. $
Determine whether the parameter exists or not
In shell,-n is used to determine that a string is not empty, while-z is just the opposite, and empty is true. It has been tested above, and the undefined parameters are empty by default:
$cat hello.sh #! / bin/bashif [- n "$1"] then echo Hello $1.else echo Hello Nobody.fi$. / hello.sh TomHello Tom.$. / hello.shHello Nobody.$
The judgment of\ $1 here should be in double quotation marks, otherwise it will be considered as a string. A string is certainly not empty, so the result will always be true.
Judge the number of parameters
The script in the above example can also be implemented by determining whether the number of parameters is greater than 0:
Number of $cat hello.sh #! / bin/bashecho parameters: $# if [$#-gt 0] then echo Hello $1.else echo Hello Nobody.fi$. / hello.sh parameters: 0Hello Nobody.$. / hello.sh Tom parameters: 1Hello Tom.$. / hello.sh Tom Jerry parameters: 2Hello Tom.$
Here-gt compares the two digits (INT), so\ $# is unquoted.
The existence of parameters can also be judged by this method. The two methods have the same effect, and people use them in different books.
Here is an example of addition, which must be passed in two parameters:
$cat add.sh #! / bin/bashif [$#-eq 2] then echo $1 + $2 = $[$1 + $2] else echo requires parameters: 2, actual parameters: $# .fi $. / add.sh 121 + 2 = 3 $. / add.sh 123 required parameters: 2, actual parameters: 3.$. / add.sh 1 required parameters: 2, actual parameters: 1.$
If you want to indicate inequality, it is if [$#-ne 2]
Get the last parameter
This is a trick to use\ $#. Using ${$#} seems to be the last variable of the parameter.
But this is not the case. You can't use\ $in curly braces like this. Here, replace it with an exclamation point:
$cat hello.sh #! / bin/bashif [$#-gt 0] then echo Hello ${! #}. Else echo Hello Nobody.fi$. / hello.sh Tom JerryHello Jerry.$
If you don't have any command-line arguments, you will return\ $0, which is the script name.
The problem with the exclamation mark above is that the effect is to refer to the value of the variable rather than the variable itself. Similar to the value of a pointer. It is more intuitive to replace the # sign with a variable with a name:
$cat parameter.sh #! / bin/bashparamater=keykey=valueecho "${paramater}" echo "${! paramater}" echo "${key}" $. / parameter.sh keyvaluevalue$
Without an exclamation point, you go directly to the value of the variable. Add an exclamation point, which is the value of the variable whose name corresponds to the value of the variable.
Get all parameters
Both\ $* and\ $@ represent all strings, but there is a difference when traversing:
$cat all.sh #! / bin/bashecho'$* effect: 'count=1for I in "$*" do echo $count: $I count=$ [$count + 1] doneecho' $@ effect: 'count=1for I in "$@" do echo $count: $I count=$ [$count + 1] done$. / all.sh Oliver Barry Kara Sara Kane$* effect: 1: Oliver Barry Kara Sara Kane$@ effect: 1: Oliver2: Barry3: Kara4: Sara5: Kane$
The value of\ $* cannot be traversed as a whole. To traverse each variable, use\ $@. The double quotes here are very important.
Without quotation marks, the contents of\ $* and\ $@ (multiple words after variable parsing) are passed to the for loop, so that the effect of the two parameters is the same. It has the same effect as passing an unquoted string directly.
With quotation marks, the contents in quotation marks are a whole. If it is\ $*, all the content in the whole is still the same word and will not be torn apart. If it is\ $@, the whole will be split into multiple words by spaces.
Here is the effect of the demonstration:
$cat all2.sh #! / bin/bashecho'$* unquoted effect: 'count=1for I in $* do echo $count: $I count=$ [$count + 1] doneecho' $@ unquoted effect: 'count=1for I in $@ do echo $count: $I count=$ [$count + 1] doneecho' direct traversal of unquoted characters: 'count=1for i in Oliver Barry Kara Sara Kanedo echo $count: $I count=$ [$count + 1] doneecho' traversal in quotation marks Effect: 'count=1for I in "Oliver Barry Kara Sara Kane" do echo $count: $I count=$ [$count + 1] done$. / all2.sh Oliver Barry Kara Sara Kane$* without quotation marks: 1: Oliver2: Barry3: Kara4: Sara5: Kane$@ without quotation marks: 1: Oliver2: Barry3: Kara4: Sara5: Kane directly traverses unquoted characters: 1: Oliver2: Barry3: Kara4: Sara5: Kane traversal with quotation marks: 1: Oliver Barry Kara Sara Kane$
Emphasize: the special parameter\ $@ must be used in double quotation marks, and the effect is that each parameter is expanded to a separated word. It works when you use a for loop to traverse.
Move the variable shift
The shift command can be used to manipulate command-line arguments. By default, each parameter is moved one position to the left. The removed parameters are discarded and cannot be recovered.
First master the use of this command, which can be used to easily parse command-line parameters.
Use the example
Here is a simple example:
$cat pop.sh #! / bin/bashcount=1while [- n "$1"] # while [$#-ne 0] do echo "$count: $1" count=$ [$count + 1] shiftdone$. / pop.sh Oliver Barry Kara Sara Kane1: Oliver2: Barry3: Kara4: Sara5: Kane$
There are two ways to determine whether there are any parameters, and the effect is the same.
Move multiple location
Execute shift with parameters, indicating how many positions to move:
$cat pop.sh #! / bin/bashcount=1# while [- n "$1"] while [$#-ne 0] do if [- n "$2"] then echo "$count: $1, $2" shift 2 else echo "$count: $1" shift fi count=$ [$count + 1] done$. / pop.sh Oliver Barry Kara Sara Kane1: Oliver, Barry2: Kara, Sara3: Kane$
Simply modify the script above, output 2 parameters at a time, and then move 2 positions.
Processing option
When a shell script requires multiple command-line arguments, all the parameters must be placed in a fixed order when the script is called.
Alternatively, you can use options to specify the value of the parameter.
Case with shift
In this example, there are options with and without values:
$cat format.sh #! / bin/bashprefix= "# prefix base=" test "# default string suffix="# suffix upper=off # whether to uppercase # parse the command line argument while [- n" $1 "] do case" $1 "in-a) suffix=" $2 "shift;;-b) prefix=" $2 "shift -s) base= "$2" shift;;-u) upper=on;; *) echo "$1 is not an option" exit 1 # Discovery unknown parameters Directly exit esac shiftdone# to add prefix and suffix output= "${prefix:+$ {prefix} _} ${base} ${suffix:+_$ {suffix}}" # determine whether to output if [$upper = on] then output=$ {output ^} fi# output result echo "$output" $. / format.sh-an aftertest_after$. / format.sh-s hello-b beforbefor_hello$. / format.sh-s hello-u-an after-b BeforBEFOR_HELLO_AFTER$. / format.sh-s hello-u-an after-b befor-lmurl is not an option$
The case statement processes an option when it finds an option. If you need to provide additional parameters on the command line, you can handle them in the processing section of the general situation. Here, because there is no need to provide any parameters, any incorrect parsing will report an error and exit (exit 1).
Can parse the version of the parameter
This version matches all the parameters to format the output:
$cat format.sh #! / bin/bashprefix= "# prefix base=" test "# default string suffix="# suffix upper=off # whether uppercase # shows that this is an array variable It is not necessary to declare-a names # all original strings that need to be formatted # parse the command line argument while [- n "$1"] do case "$1" in-a) suffix= "$2" shift ;-b) prefix= "$2" shift;;-s) base= "$2" shift;;-u) upper=on;; *) names= ("${names [@]}"$1") Esac shiftdonenames [0] = ${names [0]:-$base} for name in "${names [@]}" do # add prefix and suffix output= "${prefix:+$ {prefix} _} ${name} ${suffix:+_$ {suffix}}" # determine whether to output if [$upper = on] then output=$ {output^} fi # output result echo "$output "done$ $. / format.sh-an after-b befor-u value1 value2 value3BEFOR_VALUE1_AFTERBEFOR_VALUE2_AFTERBEFOR_VALUE3_AFTER$. / format.sh-an after after1-b befor befor1-u value1 value2 value3BEFOR_AFTER1_AFTERBEFOR_BEFOR1_AFTERBEFOR_VALUE1_AFTERBEFOR_VALUE2_AFTERBEFOR_VALUE3_AFTER$. / format.sh-an after after1-b befor befor1-u-v value1 value2 value3BEFOR_AFTER1_AFTERBEFOR_BEFOR1_AFTERBEFOR_-V_AFTERBEFOR_VALUE1_AFTERBEFOR_VALUE2_AFTERBEFOR_VALUE3_AFTER$
Looking at the results of the last two items, there is something wrong with the command line arguments provided, but the program cannot find it.
The penultimate item can be considered that the parameters provided are correct, but the options and parameters appear alternately.
The last item provides an incorrect option, but it is not recognized.
To solve this problem, a more standardized approach is needed to separate parameters from options. The content of the next section.
The problem of array with spaces
There are many ways to add elements to an array, and here is a way to recreate the array:
Array_name= ("${array_name [@]}" value1... ValueN)
You can add more than one element at a time, and if the string contains spaces, add quotation marks.
Like the command line arguments\ $@ and\ $*, all elements of the array have these two similar symbols. The most rigorous approach is to use @ with double quotes with "${names [@]}".
Be careful when adding and taking out elements, otherwise the existence of elements with spaces will break the original element separation of the array.
Add elements here using:
Names= ("${names [@]}"$1")
Not only the elements in the array, but also the elements to be added in double quotation marks, otherwise, if there is a space, multiple elements will be added to the array.
Traversal elements use:
For name in "${names [@]}"
Only when you add it correctly, can you traverse it correctly. Then make sure it is correct when you go through it.
Verify the effect:
$. / format.sh-an after-b befor-u value1 "value2 value3" value4BEFOR_VALUE1_AFTERBEFOR_VALUE2 VALUE3_AFTERBEFOR_VALUE4_AFTER$
Perfect.
Separate parameters and options
The parameters here are the additional parameters in the command line parameters in addition to the options defined. To process both parameters and options, separate the two with special characters (double dash lines). The double dash line indicates the end of the list of options, and the double dash line is followed by parameters. Based on this logic, all you have to do is add a judgment to the case statement.
Make some changes to the script above:
$cat format.sh #! / bin/bashprefix= "# prefix base=" test "# default string suffix="# suffix upper=off # whether uppercase # shows that this is an array variable It is not necessary to declare-a names # all original strings that need to be formatted # parsing options while [- n "$1"] do case "$1" in-a) suffix= "$2" shift ;-b) prefix= "$2" shift;;-s) base= "$2" shift;;-u) upper=on;; -) shift break;; *) echo "$1 is not an option" exit 1 # Discovery unknown parameters Directly exit the esac shiftdone# parsing parameter while [- n "$1"] do names= ("${names [@]}"$1") shiftdonenames [0] = ${names [0]:-$base} for name in "${names [@]}" do # add the prefix and suffix output= "${prefix:+$ {prefix} _} ${name} ${suffix:+_$ {suffix}" Do you want to output all uppercase if [$upper = on] then output=$ {output ^ ^} fi # output result echo "$output" done$
Based on this version, when using it, you need to enter options, then separate them with double dash lines, and then enter parameters. When the script encounters a double dash line, it stops processing options and treats the remaining parameters as parameters:
$. / format.sh-an after-b befor-u value1 value2 value3value1 is not an option$. / format.sh-an after-b befor-u-value1 value2 value3BEFOR_VALUE1_AFTERBEFOR_VALUE2_AFTERBEFOR_VALUE3_AFTER$. / format.sh-an after-b befor-v-u-value1 value2 value3-v is not an option$
There is no double broken line for the first time, so an error is reported.
For the second time, parameters and options are correctly separated by double dashes.
For the third time, undefined options appear in the options section, and errors can also be found.
Summary
This section also lays the groundwork for the following getopt command. Getopt can help us parse the command-line arguments and return a regular list of parameters that separate options and arguments with double dashes.
Also, option merging is not supported here:
$ls-al
All of these problems can be solved with getopt, and long options are also supported.
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.