linux 運維 shell腳本

本文主要講解if條件語句在shell的使用場景和示例

基本大綱:

1.if條件語句的語法

2.if條件語句多種條件表達式語法

3.單分支if條件語句實踐

4.if條件語句的深入實踐及場景使用




一:if條件語句的語法

1)單分支結構

第一種語法:

if <條件表達式>

then

指令

fi


第二中語法:

if <條件表達式>;then

指令

fi

上面的“<條件表達式>”部分可以是test、[]、[[]]、(())等條件表達式,也可以直接使用命令作為條件表達式。


2)雙分支結構

if條件語句的單分支結構主體就是"如果···,那麽···",而if條件語句的雙分支結構主體則為"如果···,那麽···,否則···"

if條件語句的雙分支結構語法為:

if <條件表達式>

then

指令集1

else

指令集2

fi


3)多分支結構

if條件語句多分支結構的主體為”如果···,那麽···,否則如果···,那麽,否則如果···,那麽···,否則···“

if條件語句多分支語法為:

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的寫法,每個elif都要帶有then。

  • 最後結尾的else後面沒有then。




二:if條件語句多種條件表達式語法

1)test條件表達式

if test 表達式

then

指令

fi


2)[]條件表達式

if [字符串或算術表達式]

then

指令

fi


3)[[]]條件表達式

if [[字符串表達式]]

then

指令

fi


4)(())條件表達式

if ((算術表達式))

then

指令

fi


5)命令表達式

if 命令

then

指令

fi




三:單分支if條件語句實踐

if單分支條件語句示例如下:

[[email protected] if]# [ -f /etc/hosts ] && echo 1 
1
[[email protected] if]# [[ -f /etc/hosts ]] && echo 1 
1
[[email protected] if]# test -f /etc/hosts && echo 1
1
[[email protected] if]# cat if.sh 
#!/bin/bash
#Author:ywxi
#Time:2018-06-03 07:55:58
#Version:V1.0
if [ -f /etc/hosts ]
then
    echo 1
fi
if [[ -f /etc/hosts ]]
then
    echo 1
fi
if test -f /etc/hosts
then
    echo 1
fi
[[email protected] if]# sh if.sh 
1
1
1




四:if條件語句的深入實踐及場景使用

1)開發監控MySQL數據庫的腳本

數據庫端口檢測方法:
[[email protected] ywxi]# netstat -tnlp | grep 3306|awk -F "[ :]+" '{print $5}' 
3306
[[email protected] ywxi]# netstat -tnlp |grep 3306 |wc -l    
1
[[email protected] ywxi]# netstat -tnlp | grep mysql | wc -l 
1
[[email protected] ywxi]# ss -lntup|grep mysql|wc -l
1
[[email protected] ywxi]# ss -lntup|grep 3306|wc -l     
1
[[email protected] ywxi]# lsof -i :3306|wc -l
2
[[email protected] ywxi]# yum install telnet nmap nc lsof -y

[[email protected] scripts]# nmap 127.0.0.1 -p 3306|grep open|wc -l
#查看遠端3306端口是否開通,過濾open關鍵字,結果返回1,說明有open關鍵字,表示3306端口是通的

[[email protected] scripts]# echo -e "\n"|telnet 127.0.0.1 3306 2>/dev/null|grep Connected|wc -l 
#telnet是常用來檢測服務器端口是否通暢的一個好用的命令,在非交互時需要采用特殊寫法才行,過
濾的關鍵字為Connected,返回1,說明有Connected,表示3306端口是通的。

[[email protected] scripts]# nc -w 2 127.0.0.1 3306 &>/dev/null 
[[email protected] scripts]# echo $?
0
#nc命令很強大,這裏用來檢測端口。根據執行命令的返回值判斷端口是否順暢,如果返回0,則表示順
暢,-w為超時時間

[[email protected] scripts]# ps -ef |grep mysql|grep -v grep |wc -l
2
#對服務進程或進程數進行監控

#下面是客戶端模擬用戶訪問的方式進行監控。
[[email protected] scripts]# wget --spider --timeout=10 --tries=2 www.baidu.com &>/dev/null
[[email protected] scripts]# echo $?
0
#在wget後面加url的檢測方法,&>/dev/null表示不輸出,只看返回值。--spider的意思是模擬爬取,--t
imeout=10的意思是10秒超時,--tries=2表示如果不成功,則重試2次。查看返回值來判斷

[[email protected] scripts]# wget -T 10 -q --spider http://www.baidu.com >&/dev/null 
[[email protected] scripts]# echo $?
0
#與上面相似

[[email protected] scripts]# curl -s -o /dev/null http://www.baidu.com
[[email protected] scripts]# echo $?
0
#利用curl進行檢測,-s為沈默模式,-o /dev/null表示將輸出定向到空

[[email protected] scripts]# cat testmysql.php 
<?php
$link_id=mysql_connect('127.0.0.1','root','xiwei1995') or mysql_error();
if($link_id){
     echo "mysql successful by ywxi";
  }else{
     echo mysql_error();
  }
?>
#此方法是監控數據庫是否異常的最佳的方法


開發監控MySQL數據庫的腳本:
[[email protected] scripts]# cat ifmysql.sh 
#腳本1:
#!/bin/sh
echo "#####method1######"
if  [ `netstat -tnlp |grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本2:
echo " "
echo "#####method2######"
if  [ "`netstat -tnlp |grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本3:
echo " "
echo "#####method3######"
if  [ `netstat -tnlp |grep mysqld|wc -l ` -gt 0 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本4:
echo " "
echo "#####method4######"
if  [ `lsof -i tcp:3306 |wc -l` -gt 0 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本5:
echo " "
echo "#####method5######"
[ `rpm -qa nmap|wc -l` -lt 1 ] && yum -y install nmap &>/dev/null
if  [ `nmap 127.0.0.1 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本6:
echo " "
echo "#####method6######"
[ `rpm -qa nc|wc -l` -lt 1 ] && yum -y install nc &>/dev/null
if  [ `nc -w 2 127.0.0.1 3306 &>/dev/null &&echo ok|grep ok|wc -l` -gt 0 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi

#腳本7:
echo " "
echo "#####method7######"
if  [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]
  then
   echo "MySQL is Runnig."
else
   echo "MySQL is Stopped."
   /etc/init.d/mysqld start
fi


2)監控Nginx Web服務是否異常

監控Nginx Web服務異常的方法和監控MySQL數據庫一樣,也是使用端口、進程或通過wget/curl訪問來進行檢測。

[[email protected] scripts]# netstat -tnlp|grep -w 80|awk -F "[ :]+" '{print $5}'
80
[[email protected] scripts]# netstat -tnlp|grep -w 80|wc -l
1
[[email protected] scripts]# netstat -tnlp|grep nginx|wc -l      
1
[[email protected] scripts]# ss -lntup|grep nginx|wc -l
1
[[email protected] scripts]# ss -lntup|grep -w 80|wc -l      
2
[[email protected] scripts]# lsof -i tcp:80|wc -l
4
[[email protected] scripts]# nmap 127.0.0.1 -p 80|grep open|wc -l
1
[[email protected] scripts]# echo -e "\n"|telnet 127.0.0.1 80 2>/dev/null |grep Connected|wc -l
1
[[email protected] scripts]# nc -w 2 127.0.0.1 80 &>/dev/null 
[[email protected] scripts]# echo $?
0	
[[email protected] scripts]# ps -ef | grep nginx|grep -v grep|wc -l
2
[[email protected] scripts]# ps -C nginx  --no-header
 8094 ?        00:00:00 nginx
 8121 ?        00:00:00 nginx
[[email protected] scripts]# ps -C nginx --no-header|wc -l
2
[[email protected] scripts]#  wget --spider --timeout=10  --tries=2 http://127.0.0.1/index.html  &>/dev/null
[[email protected] scripts]# echo $?
0
[[email protected] scripts]# wget -T 10 -q --spider http://127.0.0.1/index.html &>/dev/null 
[[email protected] scripts]# echo $?
0
[[email protected] scripts]# curl -s -o /dev/null http://127.0.0.1/index.html
[[email protected] scripts]# echo $?
0

[[email protected] scripts]# curl -I -s -w "%{http_code}\n" -o /dev/null http://127.0.0.1/index.html
200
#根據http相應header的結果進行判斷(200.301.302都表示正常)
等價於:
curl -I http://127.0.0.1/index.html 2>/dev/null|head -1|egrep "200|302|301" 

[[email protected] scripts]# cat ifNginx.sh 
#腳本1:
#!/bin/bash
echo "######http method1######"
if [ `netstat -tnlp|grep -w 80|awk -F "[ :]+" '{print $5}'` -eq 80 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本2:
echo " "
echo "######http method2######"
if [ "`netstat -tnlp|grep -w 80|awk -F "[ :]+" '{print $5}'`"  =  "80" ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本3:
echo " "
echo "######http method3######"
if [ `netstat -tnlp|grep nginx|wc -l`  -gt 0 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本4:
echo " "
echo "######http method4######"
if [ `lsof -i tcp:80|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本5:
echo " "
echo "######http method5######"
[ `rpm -qa nmap|wc -l` -lt 1 ] && yum -y install nmap &>/dev/null
if [ `nmap 127.0.0.1 -p 80 2>/dev/null|grep open|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本6:
echo " "
echo "######http method6######"
[ `rpm -qa nc|wc -l` -lt 1 ] && yum -y install nc &>/dev/null
if [ `nc -w 2 127.0.0.1 80 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本7:
echo " "
echo "######http method7######"
if [ `ps -ef|grep -v grep|grep nginx|wc -l`  -ge 1 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本8:
echo " "
echo "######http method8######"
if [[ `curl -I -s -w "%{http_code}\n" -o /dev/null http://127.0.0.1/index.html` =~ [23]0[012] ]]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本9:
echo " "
echo "######http method9######"
if [ `curl -I http://127.0.0.1/index.html 2>/dev/null|head -1|egrep "200|302|301"|wc -l` -eq 1 ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi

#腳本10:
echo " "
echo "######http method10######"
if [ "`curl -s http://127.0.0.1/index.html`" = "ywxitest" ]
  then
    echo "Nginx is Running."
  else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi



















Shell中if條件語句的知識和實踐