#前言:在生產工作中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分鍾檢查一次
#總結:if條件語句可以做的事情還有很多,大家可以根據工作需求去多多開發挖掘,下篇將繼續寫shell腳本的另外一個條件語句case。好了,到這里又要說再見了,寫的不好地方還望指出,多多交流提高,下次再會。