您的位置:首页 > 运维架构 > Shell

Shell笔记4——if条件语句的知识和实践

2018-06-03 08:17 441 查看
本文主要讲解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
指令2else 指令3
fi多个elif如下所示if <条件表达式1> then
指令1
elif <条件表达式2> then
指令2elif <条件表达式3> then
指令3else 指令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单分支条件语句示例如下:
[root@aliyun if]# [ -f /etc/hosts ] && echo 1
1
[root@aliyun if]# [[ -f /etc/hosts ]] && echo 1
1
[root@aliyun if]# test -f /etc/hosts && echo 1
1
[root@aliyun 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
[root@aliyun if]# sh if.sh
1
1
1


四:if条件语句的深入实践及场景使用1)开发监控MySQL数据库的脚本
数据库端口检测方法:
[root@aliyun ywxi]# netstat -tnlp | grep 3306|awk -F "[ :]+" '{print $5}'
3306
[root@aliyun ywxi]# netstat -tnlp |grep 3306 |wc -l
1
[root@aliyun ywxi]# netstat -tnlp | grep mysql | wc -l
1
[root@aliyun ywxi]# ss -lntup|grep mysql|wc -l
1
[root@aliyun ywxi]# ss -lntup|grep 3306|wc -l
1
[root@aliyun ywxi]# lsof -i :3306|wc -l
2
[root@aliyun ywxi]# yum install telnet nmap nc lsof -y

[root@aliyun scripts]# nmap 127.0.0.1 -p 3306|grep open|wc -l
#查看远端3306端口是否开通,过滤open关键字,结果返回1,说明有open关键字,表示3306端口是通的

[root@aliyun scripts]# echo -e "\n"|telnet 127.0.0.1 3306 2>/dev/null|grep Connected|wc -l
#telnet是常用来检测服务器端口是否通畅的一个好用的命令,在非交互时需要采用特殊写法才行,过
滤的关键字为Connected,返回1,说明有Connected,表示3306端口是通的。

[root@aliyun scripts]# nc -w 2 127.0.0.1 3306 &>/dev/null
[root@aliyun scripts]# echo $?
0
#nc命令很强大,这里用来检测端口。根据执行命令的返回值判断端口是否顺畅,如果返回0,则表示顺
畅,-w为超时时间

[root@aliyun scripts]# ps -ef |grep mysql|grep -v grep |wc -l
2
#对服务进程或进程数进行监控

#下面是客户端模拟用户访问的方式进行监控。
[root@aliyun scripts]# wget --spider --timeout=10 --tries=2 www.baidu.com &>/dev/null
[root@aliyun scripts]# echo $?
0
#在wget后面加url的检测方法,&>/dev/null表示不输出,只看返回值。--spider的意思是模拟爬取,--t
imeout=10的意思是10秒超时,--tries=2表示如果不成功,则重试2次。查看返回值来判断

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

[root@aliyun scripts]# curl -s -o /dev/null http://www.baidu.com [root@aliyun scripts]# echo $?
0
#利用curl进行检测,-s为沉默模式,-o /dev/null表示将输出定向到空

[root@aliyun 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数据库的脚本:
[root@aliyun 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访问来进行检测。
[root@aliyun scripts]# netstat -tnlp|grep -w 80|awk -F "[ :]+" '{print $5}'
80
[root@aliyun scripts]# netstat -tnlp|grep -w 80|wc -l
1
[root@aliyun scripts]# netstat -tnlp|grep nginx|wc -l
1
[root@aliyun scripts]# ss -lntup|grep nginx|wc -l
1
[root@aliyun scripts]# ss -lntup|grep -w 80|wc -l
2
[root@aliyun scripts]# lsof -i tcp:80|wc -l
4
[root@aliyun scripts]# nmap 127.0.0.1 -p 80|grep open|wc -l
1
[root@aliyun scripts]# echo -e "\n"|telnet 127.0.0.1 80 2>/dev/null |grep Connected|wc -l
1
[root@aliyun scripts]# nc -w 2 127.0.0.1 80 &>/dev/null
[root@aliyun scripts]# echo $?
0
[root@aliyun scripts]# ps -ef | grep nginx|grep -v grep|wc -l
2
[root@aliyun scripts]# ps -C nginx  --no-header
8094 ?        00:00:00 nginx
8121 ?        00:00:00 nginx
[root@aliyun scripts]# ps -C nginx --no-header|wc -l
2
[root@aliyun scripts]#  wget --spider --timeout=10  --tries=2 http://127.0.0.1/index.html  &>/dev/null
[root@aliyun scripts]# echo $?
0
[root@aliyun scripts]# wget -T 10 -q --spider http://127.0.0.1/index.html &>/dev/null
[root@aliyun scripts]# echo $?
0
[root@aliyun scripts]# curl -s -o /dev/null http://127.0.0.1/index.html [root@aliyun scripts]# echo $?
0

[root@aliyun 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"

[root@aliyun 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 运维 shell