当前位置:网站首页>2、 Shell position variable
2、 Shell position variable
2022-06-24 01:39:00 【jackxiao】
1. Position variable description
$0 Get the file name of the currently executed script $n Get the second... Of the currently executed script n Parameters ,n Greater than 10 Use curly braces , For reference $# Get the total number of all subsequent parameters of the current script $* Get all parameters of the current script [email protected] Get all parameters of the current script
$ and [email protected] There is no difference without double quotation marks , In double quotation marks ,$"" Treat all parameters as a single string ,$"@" Treat all strings as separate strings
2.$0、$#、$n Joint demonstration
- cat test.sh
if [ $# -ne 2 ] # If the passed in parameter is not 2 individual
then
echo "/bin/sh $0 arg1 arg2" # Prompt the user for script path and usage
exit 1 # Exit and return 1
fi
echo $1 $2 # Satisfy 2 Parameters execute the command - sh test.sh
/bin/sh test.sh arg1 arg2.Without parameters , Prompt user usage - sh test.sh ni wo
ni woExecute the results correctly
Two 、shell State variables
1. Special state variables
Variable | purpose |
|---|---|
$? | Get the execution status return value of the previous instruction (0 It means success , Not 0 It means failure , Very often ) |
$$ | Get the process number of the currently executing script (PID) |
$! | Get the process number of the last process working in the background (PID) |
$_ | Get the last parameter of the command or script executed before |
2.$? Detailed use
- Among the execution results of different commands ,$? The return values of are different , But commonly used is 0 He Fei 0 Two kinds of state ,0 It means success , Not 0 It means failure , You can get the return value by , To determine whether the program was successfully executed .
- When backing up data , After executing key commands , Get the return value , To determine if the command was successful , Whether the backup data is completed
- In the enterprise scenario ,$? Can be used as follows
- Judging orders 、 Whether the program such as script or function is successfully executed
- Sit in a script and execute “exit Numbers ”, This number will be returned to $? Variable
- If it's in a reflexive function , Through “return Numbers ” Pass this number to $?S
3.$? Script case explanation
- sed -n '63,73p' /etc/init.d/rpcbind
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$? # take $? Is assigned to a variable RETVAL
echo
[ $RETVAL -eq 0 ] && { # Judge when the return value is 0 yes , Do the following
rm -f /var/lock/subsys/$prog
rm -f /var/run/rpcbind*
}
return $RETVALIf the return value is not 0, Do not execute orders , Pass the return value to stop Script
3、 ... and 、shell Special variables
1. Grammar and function
Variable name | Variable action description |
|---|---|
${var:-word} | If the variable is unassigned or empty , Then use word Value of alternative |
${var:=word} | If the variable is unassigned or empty , Then use word Value assignment and substitution |
${var:+word} | If the variable is assigned or is not empty , Then use word Value of alternative |
${var:?word}} | If the variable is unassigned or empty , Then use word Value is output as an error |
Colons are not required , Omit colon , Only for unassigned variables , Add a colon , Including null variables
2. The demo 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-->
# Variable not assigned , Then use ttt Substitute output , Check that the variable is still unassigned
echo ${var:=ttt}--> ttt
echo $var--> ttt
# Variable not assigned , Then use ttt Substitute output , Check that the variable has been assigned
echo ${var:+MMM}--> MMM
echo $var--> ttt
# Variable assigned , Then use MMM Substitute output , The view 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 will be output 3. Enterprise working purpose
In the enterprise , For the processing of directory path, you can assign values if the above variables do not exist , Prevent exceptions caused by non-existent directory paths , Especially for the deletion of variables , It works , Otherwise, when the deleted variable does not exist , Probably Cause unknown danger find ${path:-/tmp} -name "*.tar.gz" -type f -mtime +7|xargs rm -f
As ordered , When the path variable does not exist , use /tmp Path substitution
边栏推荐
- How to set up AI speech synthesis? What is the function of speech synthesis?
- Can the fortress machine connect to the ECS? What are the reasons why the fortress cannot connect to the ECS?
- Istio practice manual | meeting the new generation of microservice architecture
- Eight common errors in programming
- How do users of Fortress computers add servers? How much does it cost to add servers for fortress users?
- [technology planting grass] talk about the system design and architecture of large-scale shopping platform
- Implementation of automatic triggering of inward delivery order after outward delivery order PGI in SAP inter company sto process
- On November 11, 2021, live broadcast e-commerce opened a new way to play
- S2b2c e-commerce platform in the pharmaceutical and medical industry enables enterprises to grasp differentiated competitive advantages and improve supply chain efficiency
- [flutter] comment utiliser les paquets et plug - ins flutter
猜你喜欢

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

【Flutter】如何使用Flutter包和插件
随机推荐
[technical grass planting] the cloud driver takes you straight to the clouds
Application of "popular science" UAV and video technology in fire scene
S2b2c e-commerce platform in the pharmaceutical and medical industry enables enterprises to grasp differentiated competitive advantages and improve supply chain efficiency
How to use the speech synthesis assistant? Does speech synthesis cost money?
What is function point analysis - FPA
Echo framework: implementing distributed log tracing
Activity Registration - Tencent cloud digital intelligence driven SME transformation and upgrading series salon · Zhuhai station
Textplus - reverse engineering of textplus
How to realize court face recognition / flow statistics based on easycvr technology?
Coordinate system "slang" in GIS world
How to do AI speech synthesis? How to download the voice of AI speech synthesis?
What is the website construction process? What details need to be valued?
If the program exits abnormally, how to catch the fatal error through the go language?
What you don't know about traifik
How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
How to make a fixed asset identification card
How to handle the loading exception in easynvr sharing video playback through iframe?
[technology planting grass] talk about the system design and architecture of large-scale shopping platform
Cloud computing "keeping the promise"
Talk about 11 tips for interface performance optimization