#前言:在生产工作中if条件语句是最常使用的,如使用来判断服务状态,监控服务器的CPU,内存,磁盘等操作,所以我们需要熟悉和掌握if条件语句。

简介

if条件语句,简单来说就是:如果,那么。有if单分支结构,双分支结构,多分支结构

1.单分支结构
#语法结构:

if <条件表达式>
then
指令
fi

if <条件表达式>;then
  指令
fi

if <条件表达式>
  then
    if <条件表达式>
      then
    fi
fi
#简单记忆法:

如果 <你给我足够多的钱>
  那么
    我就给你干活
果如
#说明:<条件表达式> 可以是test、[]、[[]]、(())等条件表达式,每一个if条件语句都是以if开头,并带有then,最后以fi结尾

#例子:

[root@shell scripts]# cat if.sh
#!/bin/bash

if [ -f /etc/hosts ]
then
   echo “[guoke1]”
fi

if [[ -f /etc/hosts ]];then
echo “[[guoke2]]”
fi

if test -f /etc/hosts
then
  echo “guoke3”
fi
#说明:上面都是判断/etc/hosts是否是文件并是否存在,如果是文件并且存在就打印相关的命令

#执行效果:

[root@shell scripts]# sh if.sh
[guoke1]
[[guoke2]]
guoke3
#说明:因为/etc/hosts是一个文件并且存在,所以输出后面的相关命令

2.双分支结构:加一个else否则
#if单分支结构主体是:如果…那么…。而双分支结构就是:如果…那么…否则

#语法结构

if <条件表达式>
then
命令集1
else
命令集2
fi

#简单记忆
如果 <你给我足够多的钱>
  那么
    我就给你干活
  否则
    我再考虑一下
果如

#例子:

[root@shell scripts]# cat if1.sh
#!/bin/bash

if [ -f /etc/hosts ]
then
   echo “is file”
else
   echo “no file”
fi

if [ -f /etc/test ]
then
   echo “is file”
else
  echo “no file”
fi
#执行效果

[root@shell scripts]# sh if1.sh
is file
no file
#说明:因为/etc/test这个文件不存在,所以输出no file

3.多分支结构
#多分支的主体为,“如果…,那么…,或者如果…,那么,否则…”

#语法结构

if <条件表达式1>
then
指令集1
elif <条件表达式2>
then
指令集2
else
指令集3
fi
#写多个elif

if <条件表达式1>
then
指令集1
elif <条件表达式2>
then
指令集2
elif <条件表达式3>
then
指令集3
else
指令集4
fi
#提示:如果加elif,那么就要加then,每个elif都要带有then,最后结尾的else后面没有then

#简单记忆
如果 <你有房>
那么
我就嫁给你
或者如果 <你家里有钱>
那么
我也可以嫁给你
或者如果 <你很努力很吃苦>
那么
  我们可以先谈谈男女朋友
否则
我们没戏
果如

#简单例子:

[root@shell scripts]# cat if2.sh
#!/bin/bash

if [ $1 -eq 1 ]
then
  echo “input 1 success”
elif [ $1 -eq 2 ]
  then
   echo "input 2 success "
elif [ $1 -eq 3 ]
  then
  echo “input 3 success”
else
   echo “input failure”
fi
#说明:如果传入的第一个参数为1就输出相关命令,或者有如果传入的第一个参数为2,就输出相关命令,后面同理,最后是否则又输出什么

#执行效果

[root@shell scripts]# sh if2.sh 1
input 1 success
[root@shell scripts]# sh if2.sh 2
input 2 success
[root@shell scripts]# sh if2.sh 3
input 3 success
[root@shell scripts]# sh if2.sh 4
input failure

4.if条件语句的使用案例
4.1.检查软件包是否安装

#检查sysstat包是否安装

[root@shell scripts]# cat soft_package.sh
#!/bin/bash

if rpm -q sysstat &>/dev/null
then
  echo “sysstat is already installed.”
else
  echo “sysstat is not installed.”
fi
#说明:使用if判断sysstat包有没有安装,如果安装了就打印already installed已经安装,如果没有安装就打印not installed没有安装

#执行效果

[root@shell scripts]# sh soft_package.sh
sysstat is already installed.

#检查mailx包是否安装

[root@shell scripts]# cat soft_package.sh
#!/bin/bash

if rpm -q mailx &>/dev/null;then
echo “mailx is already installed.”
else
echo “mailx is not installed.”
fi
#说明:使用if判断mailx包有没有安装,如果安装了就打印already installed已经安装,如果没有安装就打印not installed没有安装

#执行效果

[root@shell scripts]# sh soft_package.sh
mailx is not installed.

4.2.监控httpd服务

#提示:使用netstat或ss过滤然后使用wc统计,进行判断,如果结果大于0,就表示运行,否则就发邮件报警然后启动服务

[root@shell scripts]# cat web.sh
#!/bin/bash

if [ netstat -untpl | grep httpd | wc -l -gt 0 ];then
echo “httpd is Running”
else
echo “httpd service down” | mail -s “httpd” 1075792988@qq.com
systemctl restart httpd
fi

4.3.监控mysql服务

[root@shell scripts]# cat mysql_mon.sh
#!/bin/bash

if [ netstat -untpl | grep mysqld | wc -l -gt 0 ];then
echo “mysqld is Running”
else
echo “mysqld service down” | mail -s “mysqld” 1075792988@qq.com
systemctl restart mysqld
fi

#然后将写的监控脚本放进定时任务里面,多久运行一次检查

#例如:每3分钟执行一遍

*/3 * * * * root /bin/sh /scripts/web.sh &>/dev/null
*/3 * * * * root /bin/sh /scripts/mysql_mon.sh &>/dev/null

#提示:对于开发程序脚本来说,我们一般是先要明白开发需求,然后进行分析,设计思路,然后再编写代码

#例如:监控系统剩余内存的大小,如果小于200M,就邮件报警,每3分钟执行一次

思路:
1.先在命令行获取到系统剩余的内存的值
2.配置邮件报警功能
3.进行判断,如果取到的值小于200M,就报警
4.编写shell脚本
5.加入crond定时任务,然后每3分钟检查一次