1,逻辑判断
2,if流程控制语句
在开始之前,先了解以下逻辑判断符号:
&&与,||或,!非
A && B 必须A,B同时成立才能通过判断
-------->判断流程:先执行A判断其是否成立,若成立,则继续执行B,判断其是否成立
-------->若不成立,则直接结束,不再执行B
A || B A,B有一个成立,则可通过判断
!A 若A不成立,则通过判断
--------------------------------------------------------
-------------------------------------------------------
在条件判断时,那面会比较数值或字符串,这是我们会用到一些参数和符号,如下:
比较数值:
等于------> -eq
不等于------->-ne
小于--------->-lt
大于--------->-gt
小于等于-------->-le
大于等于-------->-ge
一些英文单词:帮助记忆
equal等于,not equal,less than,great than
比较字符串:
= 等于,== 等于,同=,!=不等于,>大于,<小于,-z字符串为空,-n字符串非空null
注意:在[ ]结构中,<和> 需要使用转义符号,如下图
[lu@bogon ~]$ [ $A > $B ] && echo "yes"
yes
[lu@bogon ~]$ [ $A < $B ] && echo "yes"
yes
[lu@bogon ~]$ [ $A > $B ] && echo "yes"
[lu@bogon ~]$ [ $A < $B ] && echo "yes"
yes
实战1:判断当前系统语言环境
[lu@bogon ~]$ echo $LANG
zh_CN.UTF-8
[lu@bogon ~]$ echo $LANG | awk -F . '{print $1}'
zh_CN
vim lang.sh
#!/bin/bash
lang=$(echo $LANG | awk -F . '{print $1}')
if [ $lang == 'en_us' ]
then
echo 'the default language is $lang.'
else
echo 'please set default language as english'
fi
[lu@bogon ~]$ bash -x lang.sh
++ echo zh_CN.UTF-8
++ awk -F . '{print $1}'
+ lang=zh_CN
+ '[' zh_CN == en_us ']'
+ echo 'please set default language as english'
please set default language as english
[lu@bogon ~]$
if流程控制语句
if语句有三种使用方式:
格式如下:
单分支:
if 条件判断语句
then 执行语句
fi
双分支:
if 条件判断语句
then 执行语句
else 执行语句
fi
多分支:
if 条件判断语句
then 执行语句
elif 条件判断语句
then 执行语句
else 执行语句
fi
实战1:if单分支应用
vi if01.sh
#!/bin/bash
if [ ! -d /home/lu/cdrom ]
then
echo "/home/lu/cdrom is not exist"
fi
[lu@bogon ~]$ bash -x if01.sh
+ '[' '!' -d /home/lu/cdrom ']'
+ echo '/home/lu/cdrom is not exist'
/home/lu/cdrom is not exist
实战2:if双分支应用
ping -c 3 -i 0.2 -w 3 10.152.247.1
-c 发送数据包的个数
-i 数据包每次发送间隔时间,默认单位:秒(s)
-w 等待时间,超过返回失败
#!/bin/bash
ping -c 3 -i 0.2 -w 3 $1 > /dev/null
if [ $? -eq 0 ]
then
echo "the host $! is ip"
else
echo "the host $1 is down"
fi
[lu@bogon ~]$ bash if02.sh 127.0.0.1
the host is ip
实战3:if多分支应用
#!/bin/bash
read -p "please input your score (0-100):" num
if [ $num -ge 85 ] && [ $num -le 100 ]
then
echo "great !优秀!"
elif [ $num -ge 60 ] && [ $num -le 84 ]
then
echo "good 良好"
else
echo “不及格”
fi
[lu@bogon ~]$ bash -x if03.sh
+ read -p 'please input your score (0-100):' num
please input your score (0-100):86
+ '[' 86 -ge 85 ']'
+ '[' 86 -le 100 ']'
+ echo 'great !优秀!'
great !优秀!
实战4,if嵌套使用
编写脚本,监控服务运行状态
启动失败后保存日志,并重启服务
再次失败,提示重启主机
#!/bin/bash
systemctl status $1 >> /var/log/ser.log
if [ $? -eq 0 ]
then
echo "the $1 is running."
else
echo "the $1 is dead."
systemctl restart $1 >> /var/log/ser.log
if [ $? -eq 0 ]
then
echo "the $1 reboot finish!"
else
echo "warning you need reboot your system"
fi
fi
[root@bogon lu]# bash -x if04.sh httpd
+ systemctl status httpd
+ '[' 0 -eq 0 ']'
+ echo 'the httpd is running.'
the httpd is running.
实战5:查询内和版本,并输出信息
#!/bin/bash
prime=$(uname -r | awk -F . '{print $1}')
if [ $prime -gt 2 ]
then
echo "the major version of system is $prime "
elif [ $prime -lt 1 ];then
echo "the system is too low "
else
echo "faild"
fi
[lu@bogon ~]$ bash if05.sh
the major version of system is 4