Shell中if条件语句的知识和实践

本文主要讲解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

原文地址:http://blog.51cto.com/13707680/2123573

时间: 2024-07-31 18:06:57

Shell中if条件语句的知识和实践的相关文章

Shell中的条件判断语句if~then~fi

Shell中的条件判断语句是前面一篇"Shell中的条件测试语句"的升级篇,也就是说,前面的测试语句是为了现在的判断语句if~then~fi语句服务的. 我们还是按照注意点和代码实现的方式铺开: 1)基本的if-then-fi语句可以用来判断基本的单层的分支结构,其形式如下: 其中if后面的测试语句一般都使用[]命令来做.如下面的例子: #-----------------------------/chapter4/ex4-18.sh------------------ #! /bin

Shell中的循环语句实例

1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:"for" 循环总是接收 "in" 语句之后的某种类型的字列表.在本例中,指定了四个英语单词,但是字列表也可以引用磁盘上的文件,甚至文件通配符.实例1.2 #!/bin/bash for x in /var/log/* do     #echo "$x is a file

js中的条件语句

1 //js中的条件语句 2 3 var age=60; 4 5 6 //example1 单分支语句 7 if(age>30){ 8 console.log("你已经不年轻了!"); 9 }else{ 10 console.log("你依然很有活力!"); 11 } 12 13 14 //example2 多分子语句 当程序执行到第一个满足条件的情况下就不再往下判断 15 16 if(age>40&&age<=60){ 17 co

shell中if条件字符串、数字比对,[[ ]]和[ ]区别

shell中if条件字符串.数字比对,[[ ]]和[ ]区别 引用: http://www.51testing.com/?uid-7701-action-viewspace-itemid-13731 http://blog.csdn.net/sunboy_2050/article/details/6836382 shell 括号 学习shell的时候总是被shell里的条件判断方式搞得头疼,经常不知道改 用[],[[]],(())还是test,let,而很少有书把它们的关系讲解的很清楚(应该是我

shell中的case语句

shell中case语法如下: case word in pattern1) Statement(s) to be execute if pattern1 matchs ;; pattern2) Statement(s) to be execute if pattern2 matchs ;; pattern3) Statement(s) to be execute if pattern3 matchs ;; *) Default action ;; esac [注]pattern模式不能包含元字

linux shell中的条件判断

1. 退出状态 在Linux系统中,每当一条命令执行完成后,系统都会返回一个退出状态,这个状态被存放在$? 这个变量中,是一个整数值,我们可以根据这个值来判断命令运行的结果是否正确. 通常情况下,退出状态值为0,表示执行成功,不为0的时候表示执行失败. POSIX规定的退出状态和退出状态的含义: 0       运行成功 1-255   运行失败,脚本命令.系统命令错误或参数传递错误 126     找到了该命令但无法执行 127     未找到要运行的命令 128     命令被系统强行结束

Bourne Shell中的条件判断

条件判断是一个程序获得智能的基础,而Bourne Shell脚本则通过命令 [ 来模拟大多数编程语言中的条件表达式. shell中支持的控制结构有: (1) if then else fi (2) for in do done (3) while do done 第二种主要用于遍历,可能不需要条件判断,其它两种则免不了和 [ 命令共同使用了.下面讲解这个命令如何模拟条件表达式. 文件/目录判断[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真.[ -c FILE ] 如果 FI

pb中的条件语句,if else,choose case ,for

顾名思义下面这些语句的作用就是对某一个或者一些值进行判断,然后根据判断结果进行下一步的操作. 一.IF条件语句 1. 单行IF ... THEN语句 IF  condition THEN 语句1   [ELSE 语句2] 单行IF ... THEN语句书写时整条语句写在一行上,当逻辑表达式的结果为True时执行语句1,否则执行语句2. 例如:IF sle_State.text="BJ" THEN   MessageBox("欢迎","北京")2.

Shell语法—— if 条件语句

if 条件语句语法 1.单分支结构 if < 条件表达式 >; then 指令 fi 2.双分支结构 if < 条件表达式 >; then 指令 1 else 指令 2 fi 3.多分支结构 if < 条件表达式 1 >; then 指令 1 else if < 条件表达式 2 >;then 指令 2 elif < 条件表达式 3 >;then 指令 3 else 指令 4 fi if 条件语句多种条件表达式语法 1.test 条件表达式 if