[关闭]
@yanglt7 2018-10-26T04:20:03.000000Z 字数 5838 阅读 749

《Shell 编程》06_if 条件语句

Shell


6.1 if 条件语句的语法

6.1.1. 单分支结构

第一种:

  1. if <条件表达式>
  2. then
  3. 指令
  4. fi

第二种:

  1. if <条件表达式>; then
  2. 指令
  3. fi

条件语句嵌套:

  1. if <条件表达式>
  2. then
  3. if <条件表达式>
  4. then
  5. 指令
  6. fi
  7. fi

6.1.2. 双分支结构

  1. if <条件表达式>
  2. then
  3. 指令集1
  4. else
  5. 指令集2
  6. fi

6.1.3 多分支结构

  1. if <条件表达式>; then
  2. 指令1
  3. elif <条件表达式2>; then
  4. 指令2
  5. else
  6. 指令3
  7. fi

6.2 if 条件语句多种条件表达式语法

(1)test 条件表达式

  1. if test 表达式; then
  2. 指令
  3. fi

(2)[] 条件表达式

  1. if [ 字符串或算数表达式 ]; then
  2. 指令
  3. fi

(3)[[]] 条件表达式

  1. if [[ 字符串表达式 ]]; then
  2. 指令
  3. fi

(4)(()) 条件表达式

  1. if ((算术表达式)); then
  2. 指令
  3. fi

(5) 命令表达式

  1. if 命令; then
  2. 指令
  3. fi

例 6-1 开发 Shell 脚本判断系统剩余内存的大小,如果低于 100 MB,就邮件报警给系统管理员,并且将脚本加入定时任务,即每 3 分钟检查一次。

1)获取系统当前剩余内存的值

  1. [root@web001 ~]# free -m
  2. total used free shared buff/cache available
  3. Mem: 1823 154 1071 34 596 1451
  4. Swap: 1023 0 1023
  5. [root@web001 ~]# free -m|awk 'NR==3 {print $NF}'
  6. 1023

2)配置邮件报警

  1. [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
  2. [root@web001 ~]# tail -1 /etc/mail.rc
  3. set from=yanglt7@163.com smtp=smtp.163.com nset smtp-auth-user=yanglt7 smtp-auth-password=授权码 smtp-auth=login
  4. [root@web001 ~]# echo "ylt"|mail -s "title" yanglt7@163.com
  5. #<==测试邮件发送
  6. [root@web001 ~]# echo "mail test" >/tmp/test.txt
  7. #<==将正文放入文件
  8. [root@web001 ~]# mail -s "title" yanglt7@163.com </tmp/test.txt
  9. #<==读取文件内容并发送邮件

3)编写 Shell 脚本

  1. #!/bin/bash
  2. FreeMem=`free -m|awk 'NR==3 {print $NF}'`
  3. CHARS="Current memory is $FreeMem."
  4. if [ $FreeMem -lt 100 ]; then
  5. echo $CHARS|tee /tmp/messages.txt #<==屏幕输出提示,并写入文件。
  6. mail -s "`date + $F-$T`$CHARS 1622320046@qq.com" </tmp/memssages.txt
  7. fi
  8. #<==1622320046@qq.com 是接收报警邮件的邮箱,yanglt7@163.com 是发送报警邮件的邮箱。

4)将上述脚本加入crond 定时任务中,每 3 分钟检查一次,达到阀值就发邮件报警

  1. [root@web001 ~]# crontab -l|tail -2
  2. # monitor sys mem at 20181024 by ylt
  3. */3 * * * * /bin/sh /home/ylt/scripts/mailMem.sh &>/dev/null

例 6-2 使用 if 语句和 read 读入实现整数大小的比较。

  1. #!/bin/bash
  2. read -t 10 -p "pls input two num:" a b
  3. [ -z $a ] || [ -z $b ] &&{
  4. echo "pls input two num again."
  5. exit 1
  6. }
  7. expr $a + 1 &>/dev/null
  8. RETVAL1=$?
  9. expr $b + 1 &>/dev/null
  10. RETVAL2=$?
  11. [[ $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ]] ||{
  12. echo "pls input two 'num'."
  13. exit 3
  14. }
  15. if [ $a -gt $b ]; then
  16. echo "$a > $b"
  17. elif [$a -lt $b ]; then
  18. echo "$a < $b"
  19. else
  20. echo "$a=$b"
  21. exit 0

6.3 监控 Web 和数据库

例 6-3 用 if 条件语句针对 Nginx Web 服务或 MySQL 数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务。

1)监控 Web 服务和 MySQL 数据库服务是否异常的常见方法:

2)监控 mysql 数据库异常

端口监控:

  1. [root@web001 scripts]# /etc/init.d/mysqld start
  2. Starting MySQL.. SUCCESS!
  3. [root@web001 scripts]# netstat -lntup|grep 3306|wc -l
  4. 1
  5. [root@web001 scripts]# netstat -lntup|grep mysql|wc -l
  6. 1
  7. [root@web001 scripts]# ss -lntup|grep mysql|wc -l
  8. 1
  9. [root@web001 scripts]# ss -lntup|grep 3306|wc -l
  10. 1
  11. [root@web001 scripts]# lsof -i tcp:3306|wc -l
  12. 2

远端监控服务器监控本地端口:

  1. [root@web001 scripts]# nmap 127.0.0.1 -p 3306|grep open|wc -l
  2. 1
  3. [root@web001 scripts]# echo -e "\n"|telnet 127.0.0.1 3306 2>/dev/null|grep Connected|wc -l
  4. 1
  5. [root@web001 scripts]# nc -w 2 127.0.0.1 3306 &>/dev/null

服务器进程或进程数进行监控(适合本地服务器):

  1. [root@web001 scripts]# ps -ef|grep mysql|grep -v grep|wc -l
  2. 2

3)开发监控 MySQL 数据库的脚本

  1. #!/bin/bash
  2. #if [ `netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]; then
  3. #if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]; then
  4. #if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]; then
  5. #if [ `lsof -i tcp:3306|wc-l` -gt 0 ]; then
  6. if [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]; then
  7. echo "MySQL is Running."
  8. else
  9. echo "MySQL is Stopped."
  10. /etc/init.d/mysqld start
  11. fi

4)监控 Nginx Web 服务异常

端口监控:

  1. [root@web001 ~]# /application/nginx/sbin/nginx
  2. [root@web001 ~]# netstat -lntup|grep nginx|wc -l
  3. 1
  4. [root@web001 ~]# netstat -lntup|grep 80|wc -l
  5. 1
  6. [root@web001 ~]# ss -lntup|grep -w 80|wc -l
  7. 1
  8. [root@web001 ~]# lsof -i tcp:80|wc -l
  9. 3

服务器进程或进程数进行监控(适合本地服务器):

  1. [root@web001 scripts]# ps -ef|grep nginx|grep -v grep|wc -l
  2. 2

5)开发监控 Nginx 的脚本

  1. #!/bin/bash
  2. #if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ]; then
  3. #if [ `netstat -lntup|grep -w 80|wc -l` -eq 80 ]; then
  4. if [ `lsof -i tcp:80|wc -l` -gt 0 ]; then
  5. if [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ]
  6. echo "Nginx is Running."
  7. else
  8. echo "Nginx is Stopped."
  9. /application/nginx/sbin/nginx
  10. fi

6.4 判断字符串是否为数字

1)使用 sed 加正则表达式

  1. #<==删除一个字符串中的所有数字,看字符串的长度是否为0,如果不为0,则说明不是整数。
  2. [root@web001 ~]# [ -n "`echo ylt123|sed 's/[0-9]//g'`" ] && echo char || echo int
  3. char
  4. [root@web001 ~]# [ -n "`echo 123|sed 's/[0-9]//g'`" ] && echo char || echo int
  5. int
  6. [root@web001 ~]# [ -z "`echo ylt123|sed 's/[0-9]//g'`" ] && echo int || echo char
  7. char
  8. [root@web001 ~]# [ -z "`echo 123|sed 's/[0-9]//g'`" ] && echo int || echo char
  9. int

2)变量的子串替换加正则表达式

  1. #<==如果 num 长度不为0,并且把 num 中的非数字部分删除,然后再看结果是不是等于 num 本身,如果两者成立,则 num 就是数字。
  2. [root@web001 ~]# num=222
  3. [root@web001 ~]# [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] && echo "it is num."
  4. it is num.

3)通过 expr 判断

  1. [root@web001 ~]# expr ylt + 1 &>/dev/null
  2. [root@web001 ~]# echo $?
  3. 2
  4. [root@web001 ~]# expr 123 + 1 &>/dev/null
  5. [root@web001 ~]# echo $?
  6. 0

4)利用 “=~” 符号判断

  1. [root@web001 ~]# [[ ylt123 =~ ^[0-9]+$ ]] && echo int || echo char
  2. char
  3. [root@web001 ~]# [[ 123 =~ ^[0-9]+$ ]] && echo int || echo char
  4. int

6.5 判断字符串长度是否为 0

1)使用字符串条件表达式 -z 和 -n

  1. [root@web001 ~]# [ -z "ylt" ] && echo 1 || echo 0
  2. 0
  3. [root@web001 ~]# [ -n "ylt" ] && echo 1 || echo 0
  4. 1

2)使用变量子串判断

  1. [root@web001 ~]# char=ylt
  2. [root@web001 ~]# [ ${#char} -eq 0 ] && echo 1 || echo 0
  3. 0

3)使用 ecpr length 函数判断

  1. [root@web001 ~]# [ `expr length "ylt"` -eq 0 ] && echo 1 || echo 0
  2. 0

4)使用 wc 的 -L 参数统计判断

  1. [root@web001 ~]# [ `echo ylt|wc -L` -eq 0 ] && echo 1 || echo 0
  2. 0

5)使用 awk lenghth 函数判断

  1. [root@web001 ~]# [ `echo ylt|awk '{print length}'` -eq 0 ] && echo 1 || echo 0
  2. 0

6.6 开发 rsync 服务的启动脚本

  1. #!/bin/bash
  2. if [ $# -ne 1 ]; then
  3. echo $"USAGE:$0 {start|stop|restart}"
  4. exit 1
  5. fi
  6. if [ "$1" = "start" ]; then
  7. echo "rsyncd is starting..."
  8. rsync --daemon
  9. sleep 2
  10. if [ `netstat -lntup|grep 873|wc -l` -ge 1 ]; then
  11. echo "rsyndc is started."
  12. exit 0
  13. fi
  14. elif [ "$1" = "stop" ]; then
  15. echo "rsyncd is stopping..."
  16. pkill rsync
  17. sleep 1
  18. if [ `netstat -lntup|grep 873|wc -l` -eq 0 ]; then
  19. echo "rsyncd is stopped."
  20. exit 0
  21. fi
  22. elif [ "$1" = "restart" ]; then
  23. echo "rsyncd is stopping..."
  24. pkill rsync
  25. sleep 1
  26. killpro=`netstat -lntup|grep 873|wc -l`
  27. rsync --daemon
  28. echo "rsyncd is starting..."
  29. sleep 1
  30. startpro=`netstat -lntup|grep 873|wc -l`
  31. if [ $killpro -eq 0 -a $startpro -ge 1 ]; then
  32. echo "rsyncd is restarted."
  33. exit 0
  34. fi
  35. else
  36. echo $"USAGE:$0 {start|stop|restart}"
  37. exit 2
  38. fi
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注