文章目录
- 四、条件判断
- 1、if 单分支格式
-
- 2、if-else 双分支格式
-
- 3、if-elif-else 多分支结构
-
- 4、( ) 和 { } 复合指令
-
- 5、exit退出程序
- 6、case 多条件判断语句
-
四、条件判断
常见的if
条件语句分为三种:单分支、双分支、多分支;
1、if 单分支格式
1.1 语法格式
if <条件表达式>
then
指令
fi
if <条件表达式>;then
指令
fi
1.2 操作案例
- 判断当前系统剩余内存大小,若低于100M,进行告警。
[root@localhost shell]
freeMem=`free -m | grep -w Mem | tr -s " " | cut -d " " -f 4`
if [ $freeMem -le 100 ]
then
echo "内存已满,请及时清理!"
else
echo "内存充足!"
fi
[root@localhost ~]
1614
- 判断当前脚本执行者,如果不是root用户,提示用户脚本需要root用户来执行。
[root@localhost shell]
if [ $USER == "root" ];then
echo "is root!"
else
echo "not is root!"
fi
2、if-else 双分支格式
2.1 语法格式
if <条件表达式>
then
指令1
else
指令2
fi
2.2 操作案例
- 检查主机CPU是Intel还是AMD。
[root@localhost shell]
vendor=`cat /proc/cpuinfo | grep vendor_id | uniq | tr -s " " | cut -d " " -f 2`
if [[ $vendor=="Genuinelnmtel" ]];then
echo "is intel!"
elif [[ $vendor=="AuthenticAMD" ]];then
echo "is AMD!"
else
echo "is other!"
fi
3、if-elif-else 多分支结构
3.1 语法格式
if <条件表达式1>
then
指令1
elif <条件表达式2>
then
指令2
else
指令3
fi
3.2 操作案例
- 根据用户输入数字,判断其优良中差。
[root@localhost shell]
read -p "Please enter your score(0-100):" grade
if [ $grade -ge 85 ]
then
echo "$grade,优"
elif [ $grade -ge 70 ]
then
echo "$grade,良"
elif [ $grade -ge 60 ]
then
echo "$grade,中"
else
echo "$grade,差"
fi
4、( ) 和 { } 复合指令
复合指令:一串命令的集合,( )
和{ }
都是对一串命令进行执行;
4.1 相同点
4.2 不同点
-
( )
只是对一串命令重新开一个子shell进行执行,{ }
对一串命令在当前shell执行;
-
( )
最后一个命令可以不用 ;
,{ }
最后一个命令要用;
结束;
-
( )
第一个命令和左边括号不需要有空格,{ }
第一个命令必须和左边括号有空格;
5、exit退出程序
exit
语句的基本作用是终止shell程序的执行,此外exit
可以附带一个可选参数,用来指定程序退出时的状态码;
exit [status]
其中,status
表示退出状态,该参数是一个整数值,取值范围为0-255
,;
[root@localhost shell]
vendor=`cat /proc/cpuinfo | grep vendor_id | uniq | tr -s " " | cut -d " " -f 2`
if [[ $vendor=="Genuinelnmtel" ]];then
echo "is intel!"
elif [[ $vendor=="AuthenticAMD" ]];then
echo "is AMD!"
else
echo "is other!"
fi
exit 6
[root@localhost shell]
6
6、case 多条件判断语句
6.1 语句格式
case 变量名 in
值1)
指令1
;;
值2)
指令2
;;
值3)
指令3
;;
*)
默认指令
esac
6.2 操作案例
- ping主机测试
[root@localhost shell]
read -p "Please enter youer ip.address(1-254):" ip_addr
ping -c 2 10.81.20.$ip_addr &> /dev/null
if [ $? -eq 0 ];then
echo "is ok!"
else
echo "is down!"
fi
- 判断一个用户是否存在
[root@localhost shell]
read -p "Please enter your username:" username
grep -w $username /etc/passwd &> /dev/null
if [ $? -eq 0 ];then
echo "$username is live!"
else
echo "$username is none!"
fi
- 判断当前内核主版本是否为3,且次版本是否大于10
[root@localhost shell]
aaa=`uname -r | cut -d "." -f 1`
bbb=`uname -r | cut -d "." -f 2`
if [ $aaa -eq 4 ];then
echo "主版本为4"
else
echo "主版本不为4"
fi
if [ $bbb -ge 10 ];then
echo "次版本大于10"
else
echo "次版本小于10"
fi
- 判断某个软件软件包是否已经安装,如果没有则设置自动安装(需要
yum
源支持)
[root@localhost shell]
read -p "Please enter your check service:" Service
Service_num=`rpm -qa $Service | wc -l`
if [ $Service_num -eq 0 ];then
echo "This Service is not install" && yum install -y $Service
exit 2
else
echo "This Service is install" && rpm -qa $Service | cut -d "-" -f 2
exit 3
fi
- 判断某个服务是否运行,若是没有运行,则自动开启该服务,并进行回显;
[root@localhost shell]
read -p "Please enter your check service:" Service
while true
do
Service_num=`ps -ef | grep $Service | wc -l`
if [ $Service_num -gt 1 ];then
echo "The service is running " &> /dev/null
else
echo "The service is not running" && systemctl start
break
fi
done
- 判断指定的主机是否能ping通,必须使用$1变量
[root@localhost shell]
ping -c 1 $1 &> /dev/null
if [ $? -eq 0 ];then
echo "this ip is ok!"
else
echo "this ip is not ok!"
fi
[root@localhost shell]
this ip is ok!
- 判断用户输入的是否是数字,若不是数字,则重复输入;
[root@localhost shell]
while true
do
read -p "Please enter your info:" info_file
case "$info_file" in
[0-9])
echo "is number!" && break
;;
*)
echo "is not number"
esac
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)