Shell script - detailed explanation of conditional statement if

1. test command

1.2. Format

Tests whether the expression is true, returning 0 if true, otherwise returning other values.

格式1:    test 条件表达式
格式2:    [ 条件表达式 ]

1.2. Example operation

Insert image description here

2. File test

2.1. Format:

操作符 文件名或者目录名 

2.2. Commonly used test operators

-d: Test whether it is a directory (Directory)
-e: Test whether the directory or file exists (Exist)
-f: Test whether it is a file (File)
-r: Test whether the current user has permission to read (Read)
-w: Test Whether the current user has permission to write (Write)
-x: Test whether the current user has permission to execute (eXcute)
-L: Test whether it is a soft link file

2.3. Example operation

Insert image description here
Insert image description here

3. Integer value comparison

3.1. Format

[ 整数1 操作符 整数2 ]

3.2. Commonly used test operators

-eq∶ 等于(Equal)
-ne∶ 不等于(Not Equal)
-gt∶ 大于(Greater Than)
-lt∶ 小于(Lesser Than)
-le∶ 小于或等于(Lesser or Equal)·
-ge∶ 大于或等于(Greater or Equal)

3.3. Example operation

1. Use logical AND to query the number of files in the current directory.
Insert image description here
2. Check whether the system memory is lower than or higher than 1024M. If the conditions are met, a prompt will appear.
Insert image description here
3. Use double brackets plus numbers to express it.
Insert image description here

4. String comparison

4.1. Format

[ 字符串1 = 字符串2 ]             #判断是否相等
[ 字符串1 != 字符串2 ]            #判断是否不等[ -z 字符串 ]                            #是否空值[ -n 字符串 ]                            # 字符集是否存在

4.2. Commonly used test operators

  • =: The string contents are the same
  • !=: The content of the strings is different, and the ! sign means the opposite meaning.
  • z: The string content is empty

4.3. Example operation

Insert image description here
Insert image description here

5. Logic test (ternary operator)

5.1. Format

格式1∶    [ 表达式1 ] 操作符 [ 表达式2 ]…
格式2∶    命令1 操作符 命令2…

5.2. Commonly used test operators

  • -a or &&: logical AND, meaning "and"
  • -oor||: logical or, meaning "or"
  • !: logical no

5.3. Example operation one

Insert image description here

5.4. Example operation two

#!/bin/bash
#Ping test
 
ping -c 3 -i 0.5 -w 2 $1 &> /dev/null && echo "$1 is online!" || echo "$1 is offline!"

Insert image description here

6. if statement

6.1. Single branch structure

If there is only one judgment to be performed, then we can use a single-branch if statement

6.1.1 Format

if [ 条件判断式 ]; then
    当条件判断成立时,可以进行的命令工作内容
fi                  #这里将if反过来写,结束if语句的意思

6.1.2 Schematic diagram:

Insert image description here

6.1.3 Example operations

Insert image description here

6.2. Double branch structure

In the judgment of the same data, if the data needs to be judged in two different ways, we need a double-branch if statement:

6.2.1 Format

if [ 条件判断式 ]; then
    当条件判断成立时,可以进行的命令工作内容
else   
    当条件判断不成立时,可以进行的命令工作内容
fi 

6.2.2 Schematic diagram:

Insert image description here

6.2.3 Example operation one

Insert image description here

6.2.4 Example operation two

#!/bin/bash
#Shut down httpd service
 
netstat -natp | grep :80 &> /dev/null
 
if [ $? -eq 0 ];then
   echo "httpd 服务正常运行!"
else
   echo "httpd 服务未开启,正在开启服务......"
   systemctl start httpd && echo "httpd 服务启动成功"
fi

Insert image description here

6.3. Multi-branch structure

If the data requires multiple different judgments, a multi-branch structure can be used

6.3.1 Format

if [ 条件判断式 ]; then
    当条件判断成立时,可以进行的命令工作内容
elif [ 条件判断式二 ]; then
    当条件判断成立时,可以进行的命令工作内容
else   
    当上面的条件判断都不成立时,可以进行的命令工作内容
fi     

6.3.2 Schematic diagram:

Insert image description here

6.3.3 Example operations

Insert image description here
Insert image description here

7. Case statement structure

7.1. Format

case 变量名称 in
“第一个变量内容”
    程序段
    ;;
“第二个变量内容”
    程序段
    ;;
*)
    不包含第一个变量内容与第二个变量内容的其他程序执行段
    默认程序段
    ;;
esac       

7.2. Example operation one

#!/bin/bash
#Determine grades and scores
 
read -p "请输入你的分数(0-100):" score
 
[ $score -ge 90 -a $score -le 100 ] && a="great"
[ $score -ge 70 -a $score -le 89 ] && a="medium"
[ $score -ge 60 -a $score -le 69 ] && a="pass"
[ $score -lt 60 ] && a="fail"
 
case $a in
great)
   echo "优秀"
;;
medium)
   echo "中等"
;;
pass)
   echo "及格"
;;
fail)
   echo "不及格"
;;
*)
  echo "输入有误"
esac

Insert image description here
Insert image description here

7.3. Example operation two

#!/bin/bash
#Determine Input
 
read -p "请输入一个字符:" string
 
case "$string" in
[a-z]|[A-Z])
    echo "你输入的是字母"
;;
[0-9])
   echo "你输入的是数字"
;;
*)
   echo "你输入的是其他字符"
esac

Insert image description here

8. Summary

1. test command:

two formats

test conditional expression

[Conditional expression]

2. File test

-d -e -f -r -w -L

3. Integer value comparison

-eq -ge -le -gt -lt -ne

4. String comparison

equal =

not equal! =

null-z

Whether -n exists

5. Logic test

Logical AND: -a or &&

Logical OR: -o or ||

Logic No:!

6. if statement

single branch

double branch

multiple branches

When the [ ] condition modifier is used in an if statement, the $flag variable must be quoted.

When the [[]] condition modifier is used in an if statement, the quotes of the $flag variable are optional.

7. case statement

multi-branch structure

Guess you like

Origin blog.csdn.net/weixin_59325762/article/details/128436773