shell_条件判断_if语法
条件判断IF
if语法
- 单分支if语法
if <条件表达式>
then
代码 . . 。 。
fi
可以简化
if <条件表达式> ; then
代码 。。。
fi
###写入命令行
[root@localhost sub_str]# if [ -f xujianbing_1_.jpg ]; then echo "文件存在" ; fi
文件存在
[root@localhost sub_str]# if [ -f xujianbing_123_.jpg ]; then echo "文件存在" ; fi
[root@localhost sub_str]#
-
双分支语法
if < 条件表达式>
then
当条件成立,执行命令(命令集合1)
else
条件不成立,执行命令(命令集合2)
fi
-
多分支处理
if <条件表达式>
then
代码1
elif <条件表达式2>
then
代码2
else
代码3
fi
if实践
-
单分支if
[root@localhost sub_str]# [ -f /etc/hosts ] && echo yes || echo no yes [root@localhost sub_str]# [[ -f /etc/hosts ]] && echo yes || echo no yes [root@localhost sub_str]# test -f /etc/hosts && echo yes || echo no ###转换if [root@localhost tmp]# cat testif.sh #!/bin/bash # # # if [ -f /etc/hosts ] then echo "yes" fi if [[ -f /etc/hosts ]] then echo "yes" fi if test -f /etc/hosts then echo yes fi [root@localhost tmp]# bash testif.sh yes yes yes [root@localhost tmp]# ################################################################ ###单分支举例2,比大小 [root@localhost tmp]# cat testif01.sh #!/bin/bash a=${1} b=${2} #验证参数个数 [ $# -ne "2" ]&& { echo "请输入2个参数" exit 1; } #验证参数为数字 expr ${a} + ${b} + 1 &>/dev/null [ $? -ne 0 ]&&{ echo "请输入数字类型参数" exit 2; } if [ $a -gt $b ] then echo "确实${a}大于${b}属实" exit 0; #程序正确退出 fi if [ $a -eq $b ] then echo "确实${a}等于${b}" exit 0; #程序正确退出 fi if [ $a -lt $b ] then echo "确实${a}小于${b}" exit 0; #程序正确退出 fi [root@localhost tmp]# sh testif01.sh 1 2 确实1小于2 [root@localhost tmp]# sh testif01.sh 2 2 确实2等于2 [root@localhost tmp]# sh testif01.sh 3 3 确实3等于3 [root@localhost tmp]# sh testif01.sh 3 1 确实3大于1属实 [root@localhost tmp]# sh testif01.sh 3 1 1 请输入2个参数 [root@localhost tmp]# sh testif01.sh a 1 请输入数字类型参数 [root@localhost tmp]# ####################################################### #######转换为多分支 [root@localhost tmp]# sh testif02.sh 2 1 确实2大于1属实 [root@localhost tmp]# sh testif02.sh 2 2 确实2等于2 [root@localhost tmp]# sh testif02.sh 2 3 确实2小于3 [root@localhost tmp]# sh testif02.sh 2 a 请输入数字类型参数 [root@localhost tmp]# sh testif02.sh 2 a f 请输入2个参数 [root@localhost tmp]# cat testif0 testif01.sh testif02.sh [root@localhost tmp]# cat testif02.sh #!/bin/bash a=${1} b=${2} #验证参数个数 [ $# -ne "2" ]&& { echo "请输入2个参数" exit 1; } #验证参数为数字 expr ${a} + ${b} + 1 &>/dev/null [ $? -ne 0 ]&&{ echo "请输入数字类型参数" exit 2; } if [ $a -gt $b ] then echo "确实${a}大于${b}属实" exit 0; #程序正确退出 elif [ $a -eq $b ] then echo "确实${a}等于${b}" exit 0; #程序正确退出 else echo "确实${a}小于${b}" exit 0; #程序正确退出 fi [root@localhost tmp]#
-
if分支嵌套
-
开发一个内存监控的脚本
eg1 需求;开发系统监控脚本
'#'步骤思路;
'#'1.获取当前内存情况
'#'2.配置邮件告警,用linux发送邮件(mail服务的配置) ,邮件内容为剩余情况
'#'3.开发脚本,判断剩余内存是否小于100M,if判断
'#'4.脚本加入crontab.写规则###过程 #1. 获取内存,获取available的数据,它是现实系统可以提供给应用程序可用的内存大小 [root@localhost tmp]# free -m total used free shared buff/cache available Mem: 990 655 69 26 266 160 Swap: 2047 41 2006 [root@localhost tmp]# ###获取使用awk [root@localhost tmp]# free -m |awk 'NR==2 {print $NF}' 156 [root@localhost tmp]# ######################################总脚本 [root@localhost tmp]# cat testifree.sh #!/bin/bash FreeMem=`free -m |awk 'NR==2 {print $NF}'` CHARS="Current memory is $FreeMem" if [ "$FreeMem" -lt "200" ] then echo $CHARS|tee /tmp/memssages.txt ###用tee 命令将信息写入emmssages.txt #mail -s "主题" 收件人 < #mail -s "`date +%F-%T`$CHARS" XXXX139@.com < /tmp/messages.txt #因为没维护邮件所以mail该命令无法使用,用输出替换 echo "内存不足,抓紧维护服务器" fi [root@localhost tmp]# sh testifree.sh Current memory is 164 内存不足,抓紧维护服务器 [root@localhost tmp]# #####加入定时器 3 * * * * /root/tmp/testifree.sh &>/dev/null
-
开发nginx,mysql服务监控脚本
-
开发rsync启停脚本
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!