流程控制
一、if判断
1.单分支
if 条件1;then
命令1
命令2
命令3
。。。
fi
等同于:
[ 条件1 ] && 命令1
2.双分支
if 条件1;then
命令1
命令2
命令3
。。。
else
命令1
命令2
命令3
。。。
fi
等同于:
[ 条件1 ] && 命令1 || 命令2
- 示例
!/bin/bash
read -p "用户名>>: " name
read -p "密码>>: " pass
if [ $name == "jkz" ] && [ $pass == "123" ];then
echo "登录成功"
else
echo "账号或密码输入错误"
fi
3.多分支
if 条件1;then
命令1
命令2
命令3
。。。
elif 条件2;then
命令1
命令2
命令3
。。。
elif 条件2;then
命令1
命令2
命令3
。。。
...
else
命令1
命令2
命令3
。。。
fi
- 示例1
!/bin/bash
1、获取硬盘根分区的使用率
disk_use=df | grep '/$' |awk '{print $5}' | cut -d% -f1
disk_use=99
disk_use=73
disk_use=53
disk_use=30
2、根据比较输出不同级别的日志
if [ $disk_use -ge 90 ];then # >=90
echo "Critical:disk space is full"
elif [ $disk_use -ge 70 ];then # >=70 <90
echo "Error:disk space will be full"
elif [ $disk_use -ge 50 ];then # >=50 < 70
echo "Warning:disk space is not enough"
else
echo "info:disk space is ok"
fi
2) 示例2
!/bin/bash
[ $# -ne 1 ] && echo "必须输入1个参数" && exit
[[ ! $1 =~ [1]+$ ]] && echo "必须输入数字" && exit
if [ $1 -gt 18 ];then
echo "too big"
elif [ $1 -lt 18 ];then
echo "too small"
else
echo "you got it"
fi
3)示例3
!/bin/bash
[ $# -ne 1 ] && echo "必须输入1个参数" && exit
if [ -L $1 ];then
echo "是一个链接文件"
elif [ -d $1 ];then
echo "是一个目录文件"
elif [ -f $1 ];then
echo "是一个标准文件"
else
echo "其他类型的文件"
fi
4) 示例5:编写监控脚本
实现: 简单的
根分区剩余空间小于10%
内存的剩余空间小于30%
向用户发送告警邮件,邮件的内容包含使用率相关信息
!/bin/bash
1、获取根分区的剩余空间
disk_use=df | grep '/$' | awk '{print $5}' | cut -d% -f1
2、获取内存的剩余率
mem_free=free | grep 'Mem' |awk '{print $4}'
mem_total=free | grep 'Mem' |awk '{print $2}'
mem_free_percent=echo "scale=2;$mem_free / $mem_total" |bc |cut -d. -f2
3、判断+报警
if [ $disk_use -gt 90 ];then
echo "硬盘使用率${disk_use}%,不够用啦,请及时处理" | mail -s "磁盘报警" a525158068@163.com
fi
if [ $mem_free_percent -lt 30 ];then
echo "内存剩余率${mem_free_percent}%,不够用啦,请及时处理" | mail -s "内存报警" a525158068@163.com
fi
ps:发送报警邮件
[root@Centos7 ~]# mail -s "标题" root <<< "要爆炸啦"
[root@Centos7 ~]# echo "邮件的内容" | mail -s "标题" root
应用
[root@Centos7 ~]# echo "内存不够用啦,请及时处理" | mail -s "报警邮件" a525158068@163.com
You have new mail in /var/spool/mail/root
[root@Centos7 ~]# hostname # 注意主机名的格式
jkz.xxx.com
二、case语句
1.case语法
case $变量名 in
值1|值1.1)
命令1
命令2
命令3
;;
值2|值2.2)
命令1
命令2
命令3
;;
值3|值3.3)
命令1
命令2
命令3
;;
*)
命令1
命令2
命令3
esac
1)示例一
!/bin/bash
if [ $# -ne 1 ];then
echo "Usage:$0 {start|stop|reload|status}"
exit 1
fi
case $1 in
"start")
netstat -an |grep 'LISTEN' |grep '\b80\b' &>/dev/null
if [ $? -eq 0 ];then
echo "nginx already start"
else
# /usr/local/nginx/sbin/nginx
systemctl start nginx &>/dev/null
if [ $? -eq 0 ];then
echo "nginx start ok"
else
echo "nginx start failed"
fi
fi
;;
"stop")
# /usr/local/nginx/sbin/nginx -s stop
systemctl stop nginx &>/dev/null
if [ $? -eq 0 ];then
echo "nginx stop ok"
else
echo "nginx stop failed"
fi
;;
"reload")
# /usr/local/nginx/sbin/nginx -s reload
systemctl reload nginx &>/dev/null
if [ $? -eq 0 ];then
echo "nginx reload ok"
else
echo "nginx reload failed"
fi
;;
"status")
netstat -an |grep 'LISTEN' |grep '\b80\b' &>/dev/null
if [ $? -eq 0 ];then
echo "nginx is up"
else
echo "nginx is down"
fi
;;
*)
echo "Usage:$0 {start|stop|reload|status}"
esac
三、function内置变量
成功或失败有个true或false
[root@Centos7 ~]# . /etc/init.d/functions
[root@Centos7 ~]# action "nginx start ok" true
nginx start ok [ OK ]
[root@Centos7 ~]# action "nginx start ok" false
nginx start ok [FAILED]
[root@Centos7 ~]#
0-9 ↩︎