xywuyiba7 发表于 2018-8-19 08:43:38

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

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

  
# 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"
  

  
# 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` =~ 0 ]]
  
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


页: [1]
查看完整版本: Shell笔记4——if条件语句的知识和实践