Shell conditional test operator and if statement
1. Conditional test operation
(1) test command
- Test whether a specific expression is established, when the condition is established, the return value of the test statement is 0, otherwise it is other numeric
(利用$?查看上一条命令是否执行成功)
format 1: test Conditional expression
format 2: [Conditional expression](至少有一个空格最好开头末尾都加一个空格)
(2) File testing
Format: [Operator file or directory]
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 (Weite)
- -x: Test whether the current user has permission to execute (eXcute)
(r w x 和chmod的给的权限是一样的)
[root@localhost ~]# [ -d /media/cdrom ]
[root@localhost ~]# echo $?
0
`返回0表示条件成立`
[root@localhost ~]# [ -d /media/cdrom/Server ]
[root@localhost ~]# echo $?
1
`返回1表示条件不成立`
[root@localhost ~]# [ -d /media/cdrom ] && echo "YES
YES
`条件成立时返回YES,&&逻辑与,“而且”的意思`
(3) Comparison of integer values
Format: [Integer 1 Operator Integer 2]
Commonly used test operators
- -eq: equal to (Equal)
- -ne: Not Equal
- -gt: Greater Than
- -lt: Less than (Lesser Than)
- le: Less than or equal to (Lesser or Equal)
- -ge: greater than or equal to (Greater or Equal)
[root@localhost ~]# who | wc -l
7
————用户数是否 > 5
[root@localhost ~]# [ $(who | wc -l) -gt 5 ] && echo "aaa"
aaa `(是否大于5,大于的话输出aaa)`
————用户数是否 >= 10
[root@localhost ~]# [ $(who | wc -l) -ge 10 ] && echo "bbb"
`(用户大于10,则输出bbb,用户不大于10,则什么都不输出)`
[root@localhost ~]# aaa=$(free -m | grep -i "mem" | awk '{print $4}')
`(可以一步一步执行命令去查看,$(free -m | grep -i "mem" | awk '{print $4}'),grep -i 不区分大小写)`
————空闲内存是否 < 1024MB
[root@localhost ~]# [ $aaa -lt 1024 ] && echo "空闲内存为 ${aaa}MB"
空闲内存为 379MB
`(空闲内存小于1024则输出空闲内存为aaa的变量值MB)`
(4) String comparison
Format 1:
[String 1 = String 2] (The 内容相同于
condition is satisfied when string 1 is string 2)
[String 1 != String 2] (The 内容不同于
condition is satisfied when string 1 is string 2)
Format 2:
[-z String 1] (The 内容为空
condition is satisfied when the string is 1 )
Commonly used test operators
- =: The string content is the same
- !=: The string content is different
(!表示相反的意思)
- -z: The string content is empty
[root@localhost ~]# echo $LANG
zh_CN.UTF-8
[root@localhost ~]# [ $LANG != "en.US" ] && echo "Not en.US"
Not en.US `(内容是否是en.US如果不是就输出 Not en.US)`
[root@localhost ~]# read -p “是否覆盖现有文件(yes/no)?" ACK
是否覆盖现有文件(yes/no)?yes `(交互式变量,ACK的变量值是yes)`
[root@localhost ~]# [ $ACK = "yes" ] && echo "覆盖"
覆盖 `(ACK的变量值是否为yes ,是的话输出 覆盖)`
(5) Logic test
Format 1: [Expression 1] Operator [Expression 2] …
Format 2: Command 1 Operator command 2…
Commonly used test operators
- -a or &&: logical and, meaning "and"
- -o or ||: logical or, meaning "or"
- ! : Logical no
(1), view the current user
[root@localhost ~]# echo $USER
Root `($USER是系统表量,显示当前用户)`
[root@localhost ~]# [ $USER != "teacher" ] && echo "Not teacher"
Not teacher `(当$USER不等于 teacher时 输出Not teacher)`
(2), test whether the directory exists
————如何测试/opt/backup目录是否存在?
[root@localhost ~]# [ -d /media/cdrom ] && echo "YES" || echo "NO”
`(检查/media/cdrom是否为目录,是的话输出YES,不是的话输出NO,条件成立时||的前一个不成立是后一个)`
3. View task progress
————查询当前的活动进程数,并测试是否小于100?
[root@localhost ~]# [ $(ps aux | wc -l) -lt 100 ] && echo "YES" || echo "NO"
YES `(小于100的话则是YES,大于100的话则是NO,ps aux 查看静态进程,wc -l统计行数)`
(4), test whether to install the software package
————如何测试是否已安装sysstat软件包?
[root@localhost ~]# rpm -q sysstatpackage
未安装软件包 sysstatpackage
[root@localhost ~]# [ $? -eq 0 ] && echo "Installed." || echo "Not installed."
Not installed. `($? 上条命令的结果,0为成立,除外都为不成立,这里判定的是未安装所以算不成立)`
`(如果上条命令结果成立,则输出lnstalled,如果不成立则输出Not installed)`
(5) Use conditional testing to write scripts to test whether the service is installed
————编辑脚本rpm_install.sh
***用来判断软件是否安装,如果已安装输出“此软件已安装”,如果未安装则进行安装
[root@localhost ~]# vim rpm_install.sh
#!/bin/bash
***查看rpm软件是否已安装
rpm -q $1 &> /dev/null
***已安装输出结果,未安装则进行安装
[ $? -eq 0 ] && echo "$1 软件已安装" || yum -y install $1 &> /dev/null
`(/dev/null相当于一个回收站)`
[root@aaa ~]# chmod +x rpm_install.sh ***(给可执行权限)
[root@aaa ~]# ./rpm_install.sh httpd ***(验证脚本)
[root@aaa ~]# rpm -q httpd ***(发现已经安装)
httpd-2.4.6-93.e17.centos.x86_64
The structure of the if statement
1. Single branch structure
if condition test operation; then command sequence
fi,
for example:
if disk has occupied space> 80%; then alarm
fi
(;可以在当前行直接写命令序列,不加;的话需要空到下一行去写)
[if begins, fi ends, then writes the command sequence, if it does not meet the if conditions, then nothing will be done]
Single branch if statement
————判断挂载点目录,若不存在则自动创建
[root@localhost ~]# cat 1.sh
#!/bin/bash
A="/media/cdrom/"
if [ ! -d $A ]
then
`(当目录不存在时执行mkdir -p $A,存在的话不会做任何操作)`
mkdir -p $A
fi
[root@localhost ~]# chmod +x 1.sh
[root@localhost ~]# ./1.sh
Two, double branch structure
if condition test operation; then command sequence 1
else command sequence 2
fi
For example:
if port 80 is listening; then website service is running
else start httpd service
fi
[if starts, fi ends, if condition test is established, then execute the then command Sequence, otherwise execute the command sequence of else]
Double branch if statement
判断目标主机是否存活,显示检测结果
[root@localhost ~]# cat 2.sh
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
`(/dev/null是“黑洞”相当于一个回收站,放进去的数据都会自动删除)`
if [ $? -eq 0 ]
`(判断ping命令的检测结果,分别给出不同提示,利用$1位置变量)`
then
echo "主机 $1 可以进行通信."
else
echo "主机 $1 无法进行通信."
fi
[root@localhost ~]# ./2.sh 192.168.4.11
主机 192.168.4.11 可以进行通信.
[root@localhost ~]# ./2.sh 192.168.4.13
主机 192.168.4.13 无法进行通信.
`ping命令的-c、-i、-W 选项分别指定测试包个数、发送间隔、超时时间`
Three, multi-branch structure
if condition test operation 1; then command sequence 1
elif condition test operation 2; then command sequence 2
else; command sequence 3
fi
For example:
if score is between 85 and 100; then judged as excellent
elif score is between 70 and 84; Then judged as qualified
esle; judged as unqualified
fi
[if starts, fi ends, the command sequence of then1 is executed when the conditional test operation 1 is established, and the command sequence of then2 is executed when the conditional test operation 2 is established, and so on elif理论是可以无限写的
, but if If the judgment result is not consistent with all the conditional test operations, the else command sequence is executed]
Multi-branch if statement
————判断分数范围,分出优秀、合格、不合格三档
[root@localhost ~]# cat 3.sh
#!/bin/bash
read -p "请输入您的分数(0-100):" A
if [ $A -ge 85 ] && [ $A -le 100 ] ; then
`(如果符合这个条件执行这个)`
`(判断分数所在区间,给出不同的分档结果)`
echo "$A 分!优秀"
elif [ $A -ge 70 ] && [ $A -le 84 ] ; then
`(如果符合这个条件执行这个)`
echo "$A 分,合格"
else `(否则执行这个)`
echo "$A 分?不合格”
fi
[root@localhost ~]#chmod a+x 3.sh
[root@localhost ~]# ./3.sh
请输入您的分数(0-100):89
89 分!优秀
[root@localhost ~]# ./3.sh
请输入您的分数(0-100):75
75 分,合格
[root@localhost ~]# ./3.sh
请输入您的分数(0-100):60
60 分?不合格