shell指令碼中的if條件語句介紹和使用案例
#前言:在生產工作中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" [email protected] 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" [email protected] 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。好了,到這裡又要說再見了,寫的不好地方還望指出,多多交流提高,下次再會。
&n