shell脚本之条件语句

68 阅读4分钟

1.测试(test)

操作符作用
-d测试是否为目录(Directory)
-e测试目录或文件是否存在(Exist)
-a测试目录或文件是否存在(Exist)
-f测试是否为文件(File)
-r测试当前用户是否有权限读取(Read)
-w测试当前用户是否有权限写入(Write)
-x测试当前用户是否有权限执行(eXcute)
-L测试是否为软连接文件
属性测试补充
-s FILE是否存在且非空
-t fdfd 文件描述符是否在某终端已经打开
-N FILE文件自从上一次被读取之后是否被修改过
-O FILE当前有效用户是否为文件属主
-G FILE当前有效用户是否为文件属组

条件测试:判断某需求是否满足,需要由测试机制来实现,专用的测试表达式需要由测试命令辅助完成

若真,则状态码变量 $? 返回0

若假,则状态码变量 $? 返回1

[root@localhost ~]#test -d /etc/sysconfig/  ##测试是否为目录
[root@localhost ~]#echo $?  ##状态码
0                           ##真
[root@localhost ~]#test -f /etc/sysconfig/  ##测试是否为文件
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]# [ -d /etc/sysconfig/ ]  ##测试是否为目录
[root@localhost ~]#echo $?  ##状态码
0                           ##真
[root@localhost ~]# [ -f /etc/sysconfig/ ]
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]#ll /etc/shadow  ##查看shadow文件权限
----------. 1 root root 1195 726 17:23 /etc/shadow
[root@localhost ~]# [ -r /etc/shadow ]  ##测试当前用户是否有权限读取
[root@localhost ~]#echo $?  ##状态码
0                           ##真
[root@localhost ~]# [ -x /etc/shadow ]  ##测试当前用户是否有权限执行
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]# [ ! -e /etc/shadow ]  ##测试目录或文件是否存在
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]# [ ! -a /etc/shadow ]  ##测试目录或文件是否存在
[root@localhost ~]#echo $?  ##状态码
0                           ##真

test.png

2.比较整数数值

操作符
-eq第一个数等于(Equal)第二个数
-ne第一个数不等于(Not Equal)第二个数
-gt第一个数大于(Greater Than)第二个数
-lt第一个数小于(Lesser Than)第二个数
-le第一个数小于或等于(Lesser or Equal)第二个数
-ge第一个数大于或等于(Greater or Equal)第二个数

格式 [ 整数1 操作符 整数2 ]

[root@localhost ~]#a=2  ##定义变量a
[root@localhost ~]#b=3  ##定义变量b
[root@localhost ~]# [ $a -eq $b ]  ##变量a等于变量b
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]# [ $a -le $b ]  ##变量a小于或等于变量b
[root@localhost ~]#echo $?  ##状态码
0                           ##真 

整数.png

3.字符串比较

操作符
=字符串内容相同
!=字符串内容不同,! 号表示相反的意思
-z字符串内容为空
-n字符是否存在

格式:[ 字符串1 操作符 字符串2]

[root@localhost ~]#name1=deng  ##定义name1
[root@localhost ~]#name2=wang  ##定义name2
[root@localhost ~]#[ $name1 = $name2 ]  ##name1=name2
[root@localhost ~]#echo $?  ##状态码
1                           ##假
[root@localhost ~]#[ $USER = root ]&& echo true  ##USER等于root且输出true
true  
tip:&&是且的意思,只有前面命令为真的的时候才会执行&&(且)后面的命令
[root@localhost ~]#[ $USER != root ]&& echo true  ##USER不等于root且输出true
[root@localhost ~]#

字符串测试.png

2.if语句

1.if语句的结构

1.单分支

if单分支.png 单分支结构

if 判断条件;

then   条件为真的分支代码

fi

示例 判断是否为超级管理员

[root@localhost opt]#mkdir ./djq  ##在当前文件创建djq目录
[root@localhost opt]#vim 1.sh  ##编辑一个新的shell脚本

#!/bin/bash   

if [ "$USER" != "root" ]
then
    echo "非管理员用户无权限操作"
else
echo "是管理员用户"
fi
[root@localhost opt]#chmod +x *  ##给新建shell脚本加执行权限,‘*’代表所有
[root@localhost opt]#./1.sh  ##执行shell脚本 
是管理员用户

2.双分支

if双分支.png

双分支结构

if 判断条件; then
 条件为真的分支代码

else
 条件为假的分支代码

fi

示例

判断主机是否联通

[root@localhost opt]#vim 2.sh  ##编辑一个新的shell脚本

#!/bin/bash
##-c 指定发送包的个数
ping -c 3 192.168.132.3
if
  [ $=0 ]
then
   echo "与真机网络畅通"
else
   echo "与真机网络不通"
fi
[root@localhost opt]#chmod +x *   ##给新建shell脚本加执行权限,‘*’代表所有
[root@localhost opt]#./2.sh    ##运行此shell脚本
PING 192.168.132.3 (192.168.132.3) 56(84) bytes of data.
64 bytes from 192.168.132.3: icmp_seq=1 ttl=64 time=0.385 ms
64 bytes from 192.168.132.3: icmp_seq=2 ttl=64 time=0.401 ms
64 bytes from 192.168.132.3: icmp_seq=3 ttl=64 time=0.940 ms

--- 192.168.132.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.385/0.575/0.940/0.258 ms
与真机网络畅通

双重循环1.png

双重循环2.png

3.多分支

if多分支.png

多分支结构

if 判断条件1
then
 条件1为真的分支代码

elif 判断条件2
then
 条件2为真的分支代码

elif 判断条件3;then
 条件3为真的分支代码

...
else
 以上条件都为假的分支代码

fi

示例 依据成绩划分等级

[root@localhost opt]#vim 3.sh   ##编辑一个新的shell脚本
#!/bin/bash

read -p "请输入你的考试成绩:" number

if [ $number -ge 85 ]&& [ $number -le 100 ]
then
   echo "你的成绩为优秀"

elif [ $number -ge 70 ]&& [ $number -le 84 ]
then
   echo "你的成绩良好"
elif [ $number -ge 60 ]&& [ $number -le 69 ]
then
   echo "你的成绩合格"
else
   echo "你的成绩不合格"
fi
[root@localhost opt]#chmod +x *  ##给所有脚本加执行权限
[root@localhost opt]#./3.sh   ##运行此脚本
请输入你的考试成绩:89
你的成绩为优秀
[root@localhost opt]#./3.sh 
请输入你的考试成绩:75
你的成绩良好
[root@localhost opt]#./3.sh 
请输入你的考试成绩:65
你的成绩合格
[root@localhost opt]#./3.sh 
请输入你的考试成绩:50
你的成绩不合格

多重分支1.png

多重分支2.png

2.case语句

case语句格式

case 变量引用 in
模式1)
 分支1
 ;;
模式2)
 分支2
 ;;
...
*)
 默认分支
 ;;

case语句.png

示例 实验要求:用case语句在/etc/init.d/目录中写一个firewalld脚本,并加入到系统服务管理中(#chkconfig: 2345 99 20),使能够使用 service firewalld start|stop|restart|status 来管理firewalld服务;如果命令选项不对,则提示 “用法: $0 {start|stop|status|restart}”。

[root@localhost opt]#vim 4.sh

#!/bin/bash

read -p "输入start|stop|status|restart:来管理防火墙:" firewalld
case $firewalld in
"start")
   systemctl start firewalld
   echo "防火墙已开启"
;;

"stop")
   systemctl stop firewalld
   echo "防火墙已关闭"
;;

"restart")
   systemctl restart firewalld
   echo "防火墙已重启"
;;

"status")
   systemctl status firewalld
   echo "防火墙状态查看"
;;

*)
   echo "用法:$0{start|stop|status|restart}"
;;

esac
[root@localhost opt]#chmod +x *
[root@localhost opt]#./4.sh 
输入start|stop|status|restart:来管理防火墙:start
防火墙已开启
[root@localhost opt]#./4.sh 
输入start|stop|status|restart:来管理防火墙:stop
防火墙已关闭
[root@localhost opt]#./4.sh 
输入start|stop|status|restart:来管理防火墙:status
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 二 2022-08-30 17:26:56 CST; 1min 6s ago
 Docs: man:firewalld(1)
  Process: 765 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 765 (code=exited, status=0/SUCCESS)

7月 28 08:34:59 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
7月 28 08:34:59 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: ICMP type 'beyond-scope' is not supported ...v6.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: beyond-scope: INVALID_ICMPTYPE: No support...me.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: ICMP type 'failed-policy' is not supported...v6.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: failed-policy: INVALID_ICMPTYPE: No suppor...me.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: ICMP type 'reject-route' is not supported ...v6.
7月 28 08:34:59 localhost.localdomain firewalld[765]: WARNING: reject-route: INVALID_ICMPTYPE: No support...me.
8月 30 17:26:55 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
8月 30 17:26:56 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
防火墙状态查看
[root@localhost opt]#./4.sh 
输入start|stop|status|restart:来管理防火墙:restart
防火墙已重启