@yanglt7
2018-10-26T04:20:03.000000Z
字数 5838
阅读 749
Shell
第一种:
if <条件表达式>
then
指令
fi
第二种:
if <条件表达式>; then
指令
fi
条件语句嵌套:
if <条件表达式>
then
if <条件表达式>
then
指令
fi
fi
if <条件表达式>
then
指令集1
else
指令集2
fi
if <条件表达式>; then
指令1
elif <条件表达式2>; then
指令2
else
指令3
fi
(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 -m
total used free shared buff/cache available
Mem: 1823 154 1071 34 596 1451
Swap: 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.rc
set 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/bash
FreeMem=`free -m|awk 'NR==3 {print $NF}'`
CHARS="Current memory is $FreeMem."
if [ $FreeMem -lt 100 ]; then
echo $CHARS|tee /tmp/messages.txt #<==屏幕输出提示,并写入文件。
mail -s "`date + $F-$T`$CHARS 1622320046@qq.com" </tmp/memssages.txt
fi
#<==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/bash
read -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/null
RETVAL1=$?
expr $b + 1 &>/dev/null
RETVAL2=$?
[[ $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ]] ||{
echo "pls input two 'num'."
exit 3
}
if [ $a -gt $b ]; then
echo "$a > $b"
elif [$a -lt $b ]; then
echo "$a < $b"
else
echo "$a=$b"
exit 0
例 6-3
用 if 条件语句针对 Nginx Web 服务或 MySQL 数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务。
1)监控 Web 服务和 MySQL 数据库服务是否异常的常见方法:
2)监控 mysql 数据库异常
端口监控:
[root@web001 scripts]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@web001 scripts]# netstat -lntup|grep 3306|wc -l
1
[root@web001 scripts]# netstat -lntup|grep mysql|wc -l
1
[root@web001 scripts]# ss -lntup|grep mysql|wc -l
1
[root@web001 scripts]# ss -lntup|grep 3306|wc -l
1
[root@web001 scripts]# lsof -i tcp:3306|wc -l
2
远端监控服务器监控本地端口:
[root@web001 scripts]# nmap 127.0.0.1 -p 3306|grep open|wc -l
1
[root@web001 scripts]# echo -e "\n"|telnet 127.0.0.1 3306 2>/dev/null|grep Connected|wc -l
1
[root@web001 scripts]# nc -w 2 127.0.0.1 3306 &>/dev/null
服务器进程或进程数进行监控(适合本地服务器):
[root@web001 scripts]# ps -ef|grep mysql|grep -v grep|wc -l
2
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 ]; then
if [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]; then
echo "MySQL is Running."
else
echo "MySQL is Stopped."
/etc/init.d/mysqld start
fi
4)监控 Nginx Web 服务异常
端口监控:
[root@web001 ~]# /application/nginx/sbin/nginx
[root@web001 ~]# netstat -lntup|grep nginx|wc -l
1
[root@web001 ~]# netstat -lntup|grep 80|wc -l
1
[root@web001 ~]# ss -lntup|grep -w 80|wc -l
1
[root@web001 ~]# lsof -i tcp:80|wc -l
3
服务器进程或进程数进行监控(适合本地服务器):
[root@web001 scripts]# ps -ef|grep nginx|grep -v grep|wc -l
2
5)开发监控 Nginx 的脚本
#!/bin/bash
#if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ]; then
#if [ `netstat -lntup|grep -w 80|wc -l` -eq 80 ]; then
if [ `lsof -i tcp:80|wc -l` -gt 0 ]; then
if [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ]
echo "Nginx is Running."
else
echo "Nginx is Stopped."
/application/nginx/sbin/nginx
fi
1)使用 sed 加正则表达式
#<==删除一个字符串中的所有数字,看字符串的长度是否为0,如果不为0,则说明不是整数。
[root@web001 ~]# [ -n "`echo ylt123|sed 's/[0-9]//g'`" ] && echo char || echo int
char
[root@web001 ~]# [ -n "`echo 123|sed 's/[0-9]//g'`" ] && echo char || echo int
int
[root@web001 ~]# [ -z "`echo ylt123|sed 's/[0-9]//g'`" ] && echo int || echo char
char
[root@web001 ~]# [ -z "`echo 123|sed 's/[0-9]//g'`" ] && echo int || echo char
int
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 char
char
[root@web001 ~]# [[ 123 =~ ^[0-9]+$ ]] && echo int || echo char
int
1)使用字符串条件表达式 -z 和 -n
[root@web001 ~]# [ -z "ylt" ] && echo 1 || echo 0
0
[root@web001 ~]# [ -n "ylt" ] && echo 1 || echo 0
1
2)使用变量子串判断
[root@web001 ~]# char=ylt
[root@web001 ~]# [ ${#char} -eq 0 ] && echo 1 || echo 0
0
3)使用 ecpr length 函数判断
[root@web001 ~]# [ `expr length "ylt"` -eq 0 ] && echo 1 || echo 0
0
4)使用 wc 的 -L 参数统计判断
[root@web001 ~]# [ `echo ylt|wc -L` -eq 0 ] && echo 1 || echo 0
0
5)使用 awk lenghth 函数判断
[root@web001 ~]# [ `echo ylt|awk '{print length}'` -eq 0 ] && echo 1 || echo 0
0
#!/bin/bash
if [ $# -ne 1 ]; then
echo $"USAGE:$0 {start|stop|restart}"
exit 1
fi
if [ "$1" = "start" ]; then
echo "rsyncd is starting..."
rsync --daemon
sleep 2
if [ `netstat -lntup|grep 873|wc -l` -ge 1 ]; then
echo "rsyndc is started."
exit 0
fi
elif [ "$1" = "stop" ]; then
echo "rsyncd is stopping..."
pkill rsync
sleep 1
if [ `netstat -lntup|grep 873|wc -l` -eq 0 ]; then
echo "rsyncd is stopped."
exit 0
fi
elif [ "$1" = "restart" ]; then
echo "rsyncd is stopping..."
pkill rsync
sleep 1
killpro=`netstat -lntup|grep 873|wc -l`
rsync --daemon
echo "rsyncd is starting..."
sleep 1
startpro=`netstat -lntup|grep 873|wc -l`
if [ $killpro -eq 0 -a $startpro -ge 1 ]; then
echo "rsyncd is restarted."
exit 0
fi
else
echo $"USAGE:$0 {start|stop|restart}"
exit 2
fi