1. Description of position variables
$0 Gets the file name of the currently executed script $n Gets the second instance of the currently executing script n Two parameters, n If it is greater than 10, use braces to pass parameters $# Gets the total number of all subsequent parameters in the current script $* Gets the parameters of all parameters passed in the current script $@ Gets the parameters of all parameters passed in the current script
There is no difference between $@ and $@ without double quotation marks. When double quotation marks are added, $"" treats all parameters as a single string, and $"@" treats all strings as different independent strings
2. $0, $#, $n joint presentation
- cat test.sh
if [ $# -ne 2 ] #If the passed in parameter is not 2 then echo "/bin/sh $0 arg1 arg2" #Prompt user for script path and usage exit 1 #Exit and return to 1 fi echo $1 $2 #If two parameters are met, execute the command
- sh test.sh /bin/sh test.sh arg1 arg2. Prompt the user for usage without parameters
- sh test.sh ni wo ni wo correct execution results
2, shell state variable
1. Special state variables
variable | purpose |
---|---|
$? | Get the return value of the execution status of the previous instruction (0 indicates success, non-0 indicates failure, which is very common) |
$$ | Gets the process number (PID) of the currently executing script |
$! | Gets the process number (PID) of the last process working in the background |
$_ | Gets the last parameter of a command or script executed before |
2.$? Detailed use
- Among the execution results of different commands, $? The return values of are different, but the common states are 0 and non-0. 0 indicates success and non-0 indicates failure. You can determine whether the program is executed successfully by obtaining the return value.
- When backing up data, after executing key commands, obtain the return value to determine whether the command is successful and whether the backup data is completed
- In the enterprise scenario, $? The usage of is as follows
- Judge whether the program such as command, script or function is executed successfully
- If you execute "exit number" in the script, this number will be returned to $? variable
- If it is a re function, pass the "return number" to $? S
3.$? Script case explanation
- sed -n '63,73p' /etc/init.d/rpcbind
stop() { echo -n $"Stopping $prog: " killproc $prog RETVAL=$? #Will $? The value of is assigned to the variable RETVAL echo [ $RETVAL -eq 0 ] && { #Judge that when the return value is 0, perform the following operations rm -f /var/lock/subsys/$prog rm -f /var/run/rpcbind* } return $RETVAL
If the return value is not 0, the command is not executed, and the return value is passed to the stop script
3, shell special variables
1. Grammar and function
Variable name | Variable action description |
---|---|
${var:-word} | If the variable is not assigned or empty, replace it with a word value |
${var:=word} | If the variable is not assigned or empty, the word value is assigned and replaced |
${var:+word} | If the variable has been assigned or is not empty, replace it with the word value |
${var:?word}} | If the variable is not assigned or empty, the word value is used as the error output |
Colons are not required. If you omit colons, you can add colons only to unassigned variables to include empty variables
2. Demonstration case
- cat /server/scripts/t2.sh
echo 'echo ${var:-ttt}-->' ${var:-ttt} echo 'echo $var-->' $var echo 'echo ${var:=ttt}-->' ${var:=ttt} echo 'echo $var-->' $var echo 'echo ${var:+MMM}-->' ${var:+MMM} echo 'echo $var-->' $var echo 'unset var and echo ${var:?the error}-->' unset var echo ${var:?the error}
- sh /server/scripts/t2.sh
echo ${var:-ttt}--> ttt echo $var--> #If the variable is not assigned, use ttt instead of output to check that the variable is still not assigned echo ${var:=ttt}--> ttt echo $var--> ttt #If the variable is not assigned, use ttt instead of output to check that the variable has been assigned echo ${var:+MMM}--> MMM echo $var--> ttt #If the variable has been assigned a value, use MMM instead of output to check that the variable is still ttt unset var and echo ${var:?the error}--> /server/scripts/t2.sh: line 9: var: the error #If the variable is not assigned, the defined alarm information is output
3. Business purpose
In enterprises, the processing of directory paths can be done by assigning values if the above variables do not exist, so as to prevent exceptions caused by the absence of directory paths, especially the deletion of variables. This method is very useful. Otherwise, when the deleted variables do not exist, unknown dangers may be caused find ${path:-/tmp} -name "*.tar.gz" -type f -mtime +7|xargs rm -f
With the above command, when the path variable does not exist, use / tmp path instead