I. Basic conditions of judgment
1) Logical operators
-A expr1-a expr2 logic and
-O expr1-o expr2 logic or
! !EXPR1 Logical Non-
2) Numerical judgment
-eq num1-eq num2 is equal
-ne Num1-ne num2 is not equal
-GT NUM1-GT num2 is greater than
-ge Num1-ge num2 is greater than or equal to
-lt Num1-lt num2 is less than
-le Num1-le num2 is less than or equal to
3) string judgment
= STR1 = str2 strings are equal
! = str1! = STR2 strings are unequal
=~ str1 =~ str2 str1 contains str2, note that the entire condition is placed between "[[]]"
-n-n str1 string length is not equal to 0
-z-z str2 whether the string length equals 0
4) file judgment
-r-r filename file exists and is readable
-w-w filename file exists and is writable
-s-s filename file exists and is not 0 in length
-f-f filename file exists and is a normal file
-d-d filename file exists and is a directory
Second, if Judgment statement basic format:
1) If Judgment statement basic format 1:
If [judging condition]
Then
Commands
Else
Fi
Example:
#数值判断:
Read-p "Enter a number (0/1):" num
If [$num-eq 1]
Then
echo "true"
Else
echo "false"
Fi
#字符串判断:
Str1= "This is a string"
if [["$str 1" =~ "This"]
Then
echo "true"
Else
echo "false"
Fi
#文件判断:
If [-F./test1.sh]
Then
echo "true"
Else
echo "false"
Fi
2) If Judgment statement basic Format 2:
If [judging condition]; Then
Commands
elif [judging conditions]; Then
Commands
...
Else
Commands
Fi
#举例:
Read-p "Enter the score:" Score
If ["$score"-lt 0]; Then
echo "Enter a score in 0~100"
elif ["$score"-GT 100]; Then
echo "Enter a score in 0~100"
elif ["$score"-LT 60]; Then
echo "Fail Score"
elif ["$score"-LT 70]; Then
echo "Pass Score"
elif ["$score"-lt 80]; Then
echo "Fair score"
elif ["$score"-LT 90]; Then
echo "Good score"
Else
echo "Excellent score"
Fi
#注意:
1. There must be a space between if and []
2. There must be a space before and after the judging condition
3, then if and if in the same line to use;
4, the judgment condition contains the variable, must use the double quotation mark to cause
Three, while loop statement basic format
1) While loop statement basic format:
#while [Judging condition]
Min=1
max=10
While [$min-le $max]
Do
Echo $min
min= ' expr $min + 1 '
Done
#注意:
1. There must be a space between the while and []
2, before and after the conditions to determine the space
# while (judging condition))
I=1
while (($i <=12))
Do
if (($i%4==0))
Then
Echo $i
Fi
i=$ (($i + 1))
Done
#注意:
1. The internal structure of this type is similar to C language
2, Attention Assignment calculation: i=$ (($i + 1))
Four, for Loop statement basic format
1) For Loop statement basic format:
#数字段形式
For i in {1..10}
Do
Echo $i
Done
#详细列出 (not many items)
For i in ' a ' B ' C ' d '
Do
Echo $i
Done
#seq形式 starting from 1
For i in ' seq 10 '
Do
Echo $i
Done
#语法循环--kind of like C syntax.
for ((I=1; i<+10; i++));
Do
Echo $i
Done
#对存在的文件进行循环
For shname in ' LS *.sh '
Do
echo "$shname" | Awk-f. ' {print '} '
Done
Description of the IF, while, for statement for the shell of Linux