Shell中if条件语句的知识和实践
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 " "|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 " "|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} " -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} " -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条件语句的知识和实践的主要内容,如果未能解决你的问题,请参考以下文章