当前位置:网站首页>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

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 wo Execute 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

  1. 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 .
  2. 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 $RETVAL

If 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

原网站

版权声明
本文为[jackxiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211116155619759e.html

随机推荐