@yanglt7
2018-10-26T04:20:03.000000Z
字数 5838
阅读 822
Shell
第一种:
if <条件表达式>then指令fi
第二种:
if <条件表达式>; then指令fi
条件语句嵌套:
if <条件表达式>thenif <条件表达式>then指令fifi
if <条件表达式>then指令集1else指令集2fi
if <条件表达式>; then指令1elif <条件表达式2>; then指令2else指令3fi
(1)test 条件表达式
if test 表达式; then指令fi
(2)[] 条件表达式
if [ 字符串或算数表达式 ]; then指令fi
(3)[[]] 条件表达式
if [[ 字符串表达式 ]]; then指令fi
(4)(()) 条件表达式
if ((算术表达式)); then指令fi
(5) 命令表达式
if 命令; then指令fi
例 6-1 开发 Shell 脚本判断系统剩余内存的大小,如果低于 100 MB,就邮件报警给系统管理员,并且将脚本加入定时任务,即每 3 分钟检查一次。
1)获取系统当前剩余内存的值
[root@web001 ~]# free -mtotal used free shared buff/cache availableMem: 1823 154 1071 34 596 1451Swap: 1023 0 1023[root@web001 ~]# free -m|awk 'NR==3 {print $NF}'1023
2)配置邮件报警
[root@web001 ~]# echo -e "set from=yanglt7@163.com smtp=smtp.163.com nset smtp-auth-user=yanglt7 smtp-auth-password=授权码 smtp-auth=login" >>/etc/mail.rc[root@web001 ~]# tail -1 /etc/mail.rcset from=yanglt7@163.com smtp=smtp.163.com nset smtp-auth-user=yanglt7 smtp-auth-password=授权码 smtp-auth=login[root@web001 ~]# echo "ylt"|mail -s "title" yanglt7@163.com#<==测试邮件发送[root@web001 ~]# echo "mail test" >/tmp/test.txt#<==将正文放入文件[root@web001 ~]# mail -s "title" yanglt7@163.com </tmp/test.txt#<==读取文件内容并发送邮件
3)编写 Shell 脚本
#!/bin/bashFreeMem=`free -m|awk 'NR==3 {print $NF}'`CHARS="Current memory is $FreeMem."if [ $FreeMem -lt 100 ]; thenecho $CHARS|tee /tmp/messages.txt #<==屏幕输出提示,并写入文件。mail -s "`date + $F-$T`$CHARS 1622320046@qq.com" </tmp/memssages.txtfi#<==1622320046@qq.com 是接收报警邮件的邮箱,yanglt7@163.com 是发送报警邮件的邮箱。
4)将上述脚本加入crond 定时任务中,每 3 分钟检查一次,达到阀值就发邮件报警
[root@web001 ~]# crontab -l|tail -2# monitor sys mem at 20181024 by ylt*/3 * * * * /bin/sh /home/ylt/scripts/mailMem.sh &>/dev/null
例 6-2 使用 if 语句和 read 读入实现整数大小的比较。
#!/bin/bashread -t 10 -p "pls input two num:" a b[ -z $a ] || [ -z $b ] &&{echo "pls input two num again."exit 1}expr $a + 1 &>/dev/nullRETVAL1=$?expr $b + 1 &>/dev/nullRETVAL2=$?[[ $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ]] ||{echo "pls input two 'num'."exit 3}if [ $a -gt $b ]; thenecho "$a > $b"elif [$a -lt $b ]; thenecho "$a < $b"elseecho "$a=$b"exit 0
例 6-3 用 if 条件语句针对 Nginx Web 服务或 MySQL 数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务。
1)监控 Web 服务和 MySQL 数据库服务是否异常的常见方法:
2)监控 mysql 数据库异常
端口监控:
[root@web001 scripts]# /etc/init.d/mysqld startStarting MySQL.. SUCCESS![root@web001 scripts]# netstat -lntup|grep 3306|wc -l1[root@web001 scripts]# netstat -lntup|grep mysql|wc -l1[root@web001 scripts]# ss -lntup|grep mysql|wc -l1[root@web001 scripts]# ss -lntup|grep 3306|wc -l1[root@web001 scripts]# lsof -i tcp:3306|wc -l2
远端监控服务器监控本地端口:
[root@web001 scripts]# nmap 127.0.0.1 -p 3306|grep open|wc -l1[root@web001 scripts]# echo -e "\n"|telnet 127.0.0.1 3306 2>/dev/null|grep Connected|wc -l1[root@web001 scripts]# nc -w 2 127.0.0.1 3306 &>/dev/null
服务器进程或进程数进行监控(适合本地服务器):
[root@web001 scripts]# ps -ef|grep mysql|grep -v grep|wc -l2
3)开发监控 MySQL 数据库的脚本
#!/bin/bash#if [ `netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]; then#if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]; then#if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]; then#if [ `lsof -i tcp:3306|wc-l` -gt 0 ]; thenif [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]; thenecho "MySQL is Running."elseecho "MySQL is Stopped."/etc/init.d/mysqld startfi
4)监控 Nginx Web 服务异常
端口监控:
[root@web001 ~]# /application/nginx/sbin/nginx[root@web001 ~]# netstat -lntup|grep nginx|wc -l1[root@web001 ~]# netstat -lntup|grep 80|wc -l1[root@web001 ~]# ss -lntup|grep -w 80|wc -l1[root@web001 ~]# lsof -i tcp:80|wc -l3
服务器进程或进程数进行监控(适合本地服务器):
[root@web001 scripts]# ps -ef|grep nginx|grep -v grep|wc -l2
5)开发监控 Nginx 的脚本
#!/bin/bash#if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ]; then#if [ `netstat -lntup|grep -w 80|wc -l` -eq 80 ]; thenif [ `lsof -i tcp:80|wc -l` -gt 0 ]; thenif [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ]echo "Nginx is Running."elseecho "Nginx is Stopped."/application/nginx/sbin/nginxfi
1)使用 sed 加正则表达式
#<==删除一个字符串中的所有数字,看字符串的长度是否为0,如果不为0,则说明不是整数。[root@web001 ~]# [ -n "`echo ylt123|sed 's/[0-9]//g'`" ] && echo char || echo intchar[root@web001 ~]# [ -n "`echo 123|sed 's/[0-9]//g'`" ] && echo char || echo intint[root@web001 ~]# [ -z "`echo ylt123|sed 's/[0-9]//g'`" ] && echo int || echo charchar[root@web001 ~]# [ -z "`echo 123|sed 's/[0-9]//g'`" ] && echo int || echo charint
2)变量的子串替换加正则表达式
#<==如果 num 长度不为0,并且把 num 中的非数字部分删除,然后再看结果是不是等于 num 本身,如果两者成立,则 num 就是数字。[root@web001 ~]# num=222[root@web001 ~]# [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] && echo "it is num."it is num.
3)通过 expr 判断
[root@web001 ~]# expr ylt + 1 &>/dev/null[root@web001 ~]# echo $?2[root@web001 ~]# expr 123 + 1 &>/dev/null[root@web001 ~]# echo $?0
4)利用 “=~” 符号判断
[root@web001 ~]# [[ ylt123 =~ ^[0-9]+$ ]] && echo int || echo charchar[root@web001 ~]# [[ 123 =~ ^[0-9]+$ ]] && echo int || echo charint
1)使用字符串条件表达式 -z 和 -n
[root@web001 ~]# [ -z "ylt" ] && echo 1 || echo 00[root@web001 ~]# [ -n "ylt" ] && echo 1 || echo 01
2)使用变量子串判断
[root@web001 ~]# char=ylt[root@web001 ~]# [ ${#char} -eq 0 ] && echo 1 || echo 00
3)使用 ecpr length 函数判断
[root@web001 ~]# [ `expr length "ylt"` -eq 0 ] && echo 1 || echo 00
4)使用 wc 的 -L 参数统计判断
[root@web001 ~]# [ `echo ylt|wc -L` -eq 0 ] && echo 1 || echo 00
5)使用 awk lenghth 函数判断
[root@web001 ~]# [ `echo ylt|awk '{print length}'` -eq 0 ] && echo 1 || echo 00
#!/bin/bashif [ $# -ne 1 ]; thenecho $"USAGE:$0 {start|stop|restart}"exit 1fiif [ "$1" = "start" ]; thenecho "rsyncd is starting..."rsync --daemonsleep 2if [ `netstat -lntup|grep 873|wc -l` -ge 1 ]; thenecho "rsyndc is started."exit 0fielif [ "$1" = "stop" ]; thenecho "rsyncd is stopping..."pkill rsyncsleep 1if [ `netstat -lntup|grep 873|wc -l` -eq 0 ]; thenecho "rsyncd is stopped."exit 0fielif [ "$1" = "restart" ]; thenecho "rsyncd is stopping..."pkill rsyncsleep 1killpro=`netstat -lntup|grep 873|wc -l`rsync --daemonecho "rsyncd is starting..."sleep 1startpro=`netstat -lntup|grep 873|wc -l`if [ $killpro -eq 0 -a $startpro -ge 1 ]; thenecho "rsyncd is restarted."exit 0fielseecho $"USAGE:$0 {start|stop|restart}"exit 2fi