Numerical calculation of ①shell
$[[space] $a [space]operator[space] $b [space]]
The calculation operator and its operands must be enclosed in square brackets [], followed by $ as a leader, and [space] means that spaces are dispensable here.
Eg: ' A=2;b=3;echo $[$a + $b] ' #屏幕输出 5
② interaction with the user when the script is run
' Read X '
When the program runs here, it pauses and prompts the user for a value, and when the user enters and returns, the program assigns the value entered by the user to x,x as any variable name.
Eg: ' Read x '
' Read-p ' Please enter a number: ' Y '
When the program runs here, the screen prints "Please enter a number:" and waits for user input, after the user enters and returns, the program assigns the value entered by the user to Y,y as any variable name
③ parameters of the script run
'./test.sh ferir 24 '
Scripts can be executed to add parameters to the script, the first parameter is Ferir, in the script is obtained, the second parameter is 24, in the script is obtained with the number of arguments, the parameters are infinite, in the script through the $+ parameter bits to get
Note: $ A script name
④if-elif-elif-else-fi
If-fi
' if ($a >); then echo $a; fi ' or ' if [$a-gt];then echo $a; Fi '
If-else-fi
' if ($a =), then echo $a; else echo 30;fi ' or ' if [$a-eq];then echo $a; else echo 30;fi '
If-elif-elif-else-fi
' if ($a >) && (($a =)); then echo $a; elif (($a > 20)) | | ($a =); then echo $[$a +1];else echo ' Sorry '; Fi '
Note: If you enclose the condition in square brackets [], there must be a space between the if and the [,] operand and [,]
> * * * *-gt = * *-EQ >= * * *-ge <= * *-le < * * * * * *-LT
If Judge document properties
' If [-defrwx filename] '- D exists && directory - e exists - f exists && normal file - r readable -w Writable- x executable
⑤case $a in value1) command;; value2) command;; VALUE3) command;; *) command; Esac
' Read-p ' please input a number: ' A;
Case $[$a%2] in
0)
echo "The number is even!";;
1)
echo "The number is odd!";;
*)
echo "This is impossible!";;
Esac '
Syntax knowledge for shell Scripts 2