Conditional statement of shell programming
Condition test
test command
Test whether the expression is established, if it is established, return 0, otherwise return to other numeric values.
Format 1: test conditional expression
[root@server6 ~]# touch a
[root@server6 ~]# test -f a
[root@server6 ~]# echo $?
0
[root@server6 ~]#
Format 2: [Conditional expression]
[root@server6 ~]# [ -f a ]
[root@server6 ~]# echo $?
0
[root@server6 ~]#
#File test
[Operator file or directory]
Commonly used test operator
-d: test whether it is a directory (Directory)
[root@server6 ~]# test -d a
[root@server6 ~]# echo $?
1
[root@server6 ~]#
-e: Test whether the directory or file exists (Exist)
[root@server6 ~]# test -e a
[root@server6 ~]# echo $?
0
[root@server6 ~]#
-f: test whether it is a file (File)
[root@server6 ~]# touch a
[root@server6 ~]# test -f a
[root@server6 ~]# echo $?
0
[root@server6 ~]#
-r: Test whether the current user has permission to read (Read)
[root@server6 ~]# test -r a
[root@server6 ~]# echo $?
0
[root@server6 ~]#
-w: Test whether the current user has permission to write (Write)
[root@server6 ~]# test -w a
[root@server6 ~]# echo $?
0
[root@server6 ~]#
-x: Test whether the current user has permission to execute (eXcute)
[root@server6 ~]# test -x a
[root@server6 ~]# echo $?
1
[root@server6 ~]#
[root@server6 ~]# mkdir /media/cdrom
[root@server6 ~]# [ -d /media/cdrom ]
[root@server6 ~]# echo $?
0--------------------------------------------返回0表示条件成立
[root@server6 ~]# [ -d /media/cdrom/Server ]
[root@server6 ~]# echo $?
1--------------------------------------------返回1表示条件不成立
[root@server6 ~]# [ -d /media/cdrom ] && echo "YES"
YES-----------------------------------------逻辑与 而且的意思
[root@server6 ~]#
Integer value comparison
[Integer 1 Operator Integer 2]
[root@server6 ~]# who
root :0 2020-11-03 14:08 (:0)
root pts/0 2020-11-04 18:18 (192.168.188.1)
[root@server6 ~]# who | wc -l
2
[root@server6 ~]# [ $(who | wc -l) -ge 5 ] && echo "too many"
[root@server6 ~]# [ $(who | wc -l) -le 5 ] && echo "too many"
too many
[root@server6 ~]#
[root@server6 ~]# [ $(free -m | grep Mem: | awk '{print $7}') -ge 1024 ] && echo "内存可用空间:$(free -m | grep Mem: | awk '{print $7}')"
内存可用空间:2661
[root@server6 ~]#
Commonly used test operators
-eq: equal (Equal)
-ne: not equal (Not Equal)
-gt: greater than (Greater Than)
-lt: less than (Lesser Than)
-le: less than or equal to (Lesser or Equal)
-ge : Greater or Equal
The previous conditions need to be met to output
[root@server6 ~]# [ 1 -gt 2 ] && echo "tree"
[root@server6 ~]# [ 1 -lt 2 ] && echo "tree"
tree
[root@server6 ~]#
String comparison
Format 1:
[String 1 = String 2]
[String 1 != String 2]
[root@server6 ~]# [ 'a' = 'b' ]
[root@server6 ~]# echo $?
1
[root@server6 ~]# [ 'a' = 'b' ] && echo "a不等于b"
[root@server6 ~]#
[root@server6 ~]# [ 'a' != 'b' ] && echo "a不等于b"
a不等于b
[root@server6 ~]#
Format 2:
[-z string]
[root@server6 ~]# [ -z '' ] && echo "空格"
空格
[root@server6 ~]#
Commonly used test operators
=: string content is the same
! =: string content is different,! Sign means the opposite-
z: string content is empty
[root@server6 ~]# read -p "是否覆盖现有内容?(yes/no)" ack
是否覆盖现有内容?(yes/no)yes
[root@server6 ~]# [ $ack="yes" ] && echo "覆盖"
覆盖
[root@server6 ~]#
Logic test
Format 1: [Expression 1] Operator [Expression 2]…
Format 2: Command 1 Operator Command 2…
Commonly used test operators
-a or &&: logical and, the meaning of "and"
-o or ||: logical or, the meaning of "or"
!: logical no
The structure of the if statement
Single branch structure
if conditional test operation
then command sequence
fi
[root@server6 ~]# vi mkdir.sh
#!/bin/bash
# 创建目录
dir="/a/b"
if [ ! -d $dir ]
then mkdir -p $dir
fi
[root@server6 ~]# chmod +x mkdir.sh
[root@server6 ~]# ./mkdir.sh
[root@server6 ~]# su sf
[sf@server6 root]$ sudo vi a.sh
#!/bin/bash
# 判断用户是否为root
if [ ! '$USER' = 'root' ]]
then echo "当前用户不是root"
fi
[sf@server6 root]$ sudo chmod +x a.sh
[sf@server6 root]$ sudo ./a.sh
当前用户不是root
[sf@server6 root]$
Double branch structure
Determine whether apache is starting
[root@server6 ~]# vi a1.sh
#!/bin/bash
# 判断apache是否启动中
netstat -anpt | grep 80
if [ $? = 0 ]
then echo "网站服务启动中"
else echo "网站服务停止"
fi
[root@server6 ~]# chmod +x a1.sh
[root@server6 ~]# ./a1.sh
网站服务停止
The double-branch if statement only performs another operation on the basis of a single branch for the "condition is not established", rather than "sit and watch" without performing any operation.
For example, if you want to write a connectivity test script pinghost.sh, provide the self-labeled host address through the position parameter $1, and then give the corresponding prompt based on the ping detection result, you can refer to the following operation process.
ping
[root@server6 ~]# vi ping.sh
#!/bin/bash
# 查看是否能ping通
ping -c 3 $1 &> /dev/null
if [ $? = 0 ]
then echo "ping通了"
else
echo "ping不通"
fi
[root@server6 ~]# chmod +x ping.sh
[root@server6 ~]# ./ping.sh 192.168.188.60
ping通了
[root@server6 ~]#
the weather
[root@server6 ~]# vi tiangi.sh
#!/bin/bash
# 天气
read -p "天气" weather
if [ "$weather" = "天晴" ]
then echo "好天气"
else
echo "今天可能要带伞"
fi
[root@server6 ~]# chmod +x tiangi.sh
[root@server6 ~]# ./tiangi.sh
天气天晴
好天气
[root@server6 ~]#
Multi-branch structure
[root@server6 ~]# vi grade.sh
#!/bin/bash
# 成绩判断
read -p "您的成绩是:" grade
if [ $grade -gt 100 ] || [ $grade -lt 0 ]
then echo "输入错误,请重新输入0~100间数值"
elif [ $grade -ge 85 ] && [ $grade -le 100 ]
then echo "优秀!!"
elif [ $grade -ge 70 ] && [ $grade -le 84 ]
then echo "良好!!"
elif [ $grade -ge 60 ] && [ $grade -le 69 ]
then echo "合格!!!"
else echo "不合格!!"
fi
[root@server6 ~]# chmod +x grade.sh
[root@server6 ~]# ./grade.sh
您的成绩是:95
优秀!!
[root@server6 ~]# ./grade.sh
您的成绩是:101
输入错误,请重新输入0~100间数值
[root@server6 ~]# ./grade.sh
您的成绩是:88
优秀!!
[root@server6 ~]# ./grade.sh
您的成绩是:77
良好!!
[root@server6 ~]# ./grade.sh
您的成绩是:59
不合格!!
[root@server6 ~]#
case multi-branch statement