This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.
Operator
1. Basic grammar
$((expression)) or $[expression]
Expr +, -, \*, /, %
Note: There must be Spaces between expr operators
2. Case practice
-
You compute 3 plus 2
[root@localhost ~]# expr 2 + 3 5 Copy the code
-
Compute the value of 3 minus 2
[root@localhost ~]# expr 3 - 2 1 Copy the code
-
So let’s compute 2 plus 3 X 4
- Expr completes the calculation in one step
[root@localhost ~]# expr `expr 2 + 3` \* 4 20 Copy the code
-
$[expression] is used
[root@localhost ~]# S=$[(2+3)*4] [root@localhost ~]# echo $S 20 Copy the code
-
Use $((expression))
[root@localhost ~]# R=$(((2+3)*4)) [root@localhost ~]# echo $R 20 Copy the code
Second, conditional judgment
1. Basic grammar
[condition] (notice the space before and after the condition)
Note: The condition is either null or true, [MOE] returns true and [] returns false.
2. Common judgment conditions
-
Compare two integers
= String comparison
-lt is less than (less than) -le is less than or equal to (less equal)
-eq equals (equal) -gt is greater than (greater than)
-ge greater equal -NE Not equal
-
Judge by file permissions
-r Has the read permission (read) -w Has the write permission (write)
-x has the execute permission (execute).
-
Judge by file type
-f file exists and is a regular file
-e file existence -d file existence and directory
3. Case practice
-
Whether 23 is greater than or equal to 22
[root@localhost ~]# [ 23 -ge 22 ] [root@localhost ~]# echo $? 0 Copy the code
-
Helloworld. sh Specifies whether you have write permission
[root@localhost moe]# [ -w helloword.sh ] [root@localhost moe]# echo $? 0 Copy the code
-
Check whether files in the /root/moe/text. TXT directory exist
[root@localhost moe]# [ -e test.txt ] [root@localhost moe]# echo $? 0 Copy the code
-
Condition judgment (&& said previous command execution success, to execute a command, | | said on a command execution failed, to perform the next command)
[root@localhost moe]# [ 1 -eq 1 ] && [ 1 = 1 ] [root@localhost moe]# echo $? 0 [root@localhost moe]# [ 1 -eq 1 ] && [ 1 = 2 ] [root@localhost moe]# echo $? 1 [root@localhost moe]# [ 1 -eq 1 ] || [ 1 = 2 ] [root@localhost moe]# echo $? 0 [root@localhost moe]# [ 1 -eq 2 ] || [ 1 = 1 ] [root@localhost moe]# echo $? 0 Copy the code
3. Process control
1
1.1. Basic Grammar
If [conditional judgment]; Then program FI or if [conditional judgment] then program FICopy the code
Matters needing attention:
(1) there must be a space between the brackets and the conditional statement
(2) If
1.2. Case practice
If it is 1, it prints MOE; if it is 2, it prints Zoe; if it is anything else, it prints nothing.
[root@localhost moe]# vim if.sh
[root@localhost moe]# cat if.sh
#! /bin/bash
if [ $1 -eq 1 ]
then
echo "moe"
elif [ $1 -eq 2 ]
then
echo "zoe"
fi
[root@localhost moe]# bash if.sh 1
moe
[root@localhost moe]# bash if.sh 2
zoe
[root@localhost moe]# bash if.sh 3
[root@localhost moe]#
Copy the code
2. Case statement
2.1. Basic Grammar
Case $variable name in "value 1") if the value of the variable is equal to the value 1, execute procedure 1; "Value 2") if the value of the variable is equal to value 2, program 2 is executed; ... Omit the other branches... *) Execute this program if none of the values of the variables are above; esacCopy the code
Matters needing attention:
1) Case lines must end with the word “in” and each pattern match must end with a close parenthesis of “) “.
2) double semicolon “**;; ** “indicates the end of the command sequence, which is equivalent to break in Java.
3) The last “*)” indicates the default mode, which is equivalent to the default in Java.
2.2. Case practice
Enter a number, MOE if it is 1, ZOE if it is 2, and MZOE if it is anything else.
[root@localhost moe]# vim case.sh
[root@localhost moe]# cat case.sh
#! /bin/bash
case $1 in
1)
echo "moe"
;;
2)
echo "zoe"
;;
*)
echo "mzoe"
;;
esac
[root@localhost moe]# sh case.sh 1
moe
[root@localhost moe]# sh case.sh 2
zoe
[root@localhost moe]# sh case.sh 3
mzoe
[root@localhost moe]#
Copy the code
3. For loop
3.1 basic Grammar 1
For ((initial value; Cyclic control conditions; Variable change)) do program doneCopy the code
3.2. Case Practice 1
We’re going from 1 to 100
[root@localhost moe]# vim for.sh
[root@localhost moe]# cat for.sh
#! /bin/bashS=0 for ((i=0; i<=100; i++)) do S=$[$S+$i] done echo $S [root@localhost moe]# chmod u+x for.sh [root@localhost moe]# ./for.sh 5050Copy the code
3.3 basic Grammar 2
For variable in value 1 value 2 value 3... Do procedures doneCopy the code
3.4. Case Practice 2
-
Prints all input parameters
[root@localhost moe]# vim for2.sh [root@localhost moe]# cat for2.sh #! /bin/bashfor i in $* do echo "moe love $i" done [root@localhost moe]# bash for2.sh zoe1 zoe2 zoe3 moe love zoe1 moe love zoe2 moe love zoe3Copy the code
-
Compare $* with $@
Both $* and $@ indicate that all arguments passed to a function or script, when not enclosed by double quotation marks, are passed as $1 $2... Prints all arguments as $n.
[root@localhost moe]# vim for2.sh [root@localhost moe]# cat for2.sh #! /bin/bashfor i in $* do echo "moe love $i" done for j in $@ do echo "moe love $j" done [root@localhost moe]# sh for2.sh zoe1 zoe2 zoe3 moe love zoe1 moe love zoe2 moe love zoe3 moe love zoe1 moe love zoe2 moe love zoe3 [root@localhost moe]#Copy the code
When they are enclosed by double quotation marks, "$*" takes all the arguments as a whole, ending with "$1 $2... $n "to print all parameters; "$@" will separate the parameters to "$1" "$2"... Print all parameters as $n ".
[root@localhost moe]# vim for3.sh [root@localhost moe]# cat for3.sh #! /bin/bash for i in "$*" do echo "moe love $i" done for j in "$@" do echo "moe love $j" done [root@localhost moe]# bash for3.sh zoe1 zoe2 zoe3 moe love zoe1 zoe2 zoe3 moe love zoe1 moe love zoe2 moe love zoe3 Copy the code
All the arguments in $* are considered a whole, so the for loop will only loop once
Each parameter in $@ is considered independent, so if there are several parameters in "$@", it will loop several times
4. While loop
4.1. Basic Grammar
While [conditional] do the procedure doneCopy the code
4.2. Case practice
We’re going from 1 to 100
[root@localhost moe]# vim while.sh
[root@localhost moe]# cat while.sh
#! /bin/bash
s=0
i=1
while [ $i -le 100 ]
do
s=$[$s+$i]
i=$(($i+1))
done
echo $s
[root@localhost moe]# bash while.sh
5050
Copy the code
Four,
This section introduces operator syntax, conditional syntax, common flow control syntax and case operation in shell, which is the most basic part of shell programming.
Link: Linux shell small white into the first chapter