05@流程控制之if判断
流程控制之if判断
一、单分支(if)
语法
if 条件;then
要执行的命令1
要执行的命令2
要执行的命令3
...
fi
# 上述语法可以用一行代码代替
[ 条件信息 ] && xxx
示例
#案列一:
[root@egon test]# cat disk_monitor.sh
#!/usr/bin/env bash
disk_use=$(df -P |grep '/$' |awk '{print $5}' |awk -F% '{print $1}')
if [ $disk_use -gt 10 ];then
echo "warning:Not enough hard disk space"
fi
[root@egon test]# . disk_monitor.sh
warning:Not enough hard disk space
#案列二:
[root@web01 ~]# vim disk.sh
#!/bin/bash
DISK=`df |grep "/$" |awk '{print $5}' |awk -F% '{print $1}'`
if [ $DISK -lt 10 ];then
echo "温馨提示:你的磁盘根空间为$DISK%"
fi
[root@web01 ~]# sh disk.sh
温馨提示:你的磁盘根空间为2%
注意:if 测试中还可以执行命令 根据命令的返回值做判断
[root@egon ~]# if cd / ;then echo Y ;fi
Y
[root@egon /]# if grep -q root /etc/passwd ;then echo Y ;fi #判断是否有root用户,有则返回Y,无则返回空
Y
二、双分支(if)
语法
if 条件;then
要执行的命令1
要执行的命令2
要执行的命令3
...
else
要执行的命令1
要执行的命令2
要执行的命令3
...
fi
# 上述语法可以用一行代码代替
[ 条件信息 ] && xxx || xxxx
示例
#案列一
[root@web01 ~]# cat linux.sh
#!/bin/bash
USERNAME="hzl"
PASSWORD="123"
read -p "请输入你的用户:" name
read -p "请输入你的密码:" passwd
if [[ $name -eq $USERNAME && $passwd -eq $PASSWORD ]];then
echo "登录成功"
else
echo "用户或密码输入错误,请重新输入"
fi
[root@web01 ~]# sh linux.sh
请输入你的用户:hzl
请输入你的密码:123
登录成功
#案列二
#!/bin/bash
username='egon'
password='123'
read -p 'user: ' name
read -p 'passwd: ' passwd
if [ $name = $username -a $passwd = $password ];then
echo 'login successful'
else
echo 'username or password err'
fi
三、多分支(if)
语法:
if 条件;then
要执行的命令1
要执行的命令2
要执行的命令3
...
elif 条件;then
要执行的命令1
要执行的命令2
要执行的命令3
...
elif 条件;then
要执行的命令1
要执行的命令2
要执行的命令3
...
...
else
要执行的命令1
要执行的命令2
要执行的命令3
...
fi
1、案列:猜年龄
======================版本1======================
[root@web01 ~]# vim hzl.sh
#!/bin/bash
AGE=18
age1=30
age2=50
read -p "请输入你的年龄" age
if [ $age -lt $AGE ];then
echo "你太小了,没成年"
elif [[ $age -ge $AGE && $age -lt $age1 ]];then
echo "二十不惑,精神小伙"
elif [[ $age -gt $age1 && $age -lt $age2 ]];then
echo "三十而已,年老色衰"
else
echo "安享晚年"
fi
[root@web01 ~]# sh hzl.sh
请输入你的年龄21
二十不惑,精神小伙
#!/bin/bash
age=87
read -p 'num: ' n
if [ $n -eq $age ];then
echo 'you get it'
elif [ $n -gt $age ];then
echo 'too big'
elif [ $n -lt $age ];then
echo 'too small'
fi
======================版本2======================
#!/bin/bash
read -p ">>> " num
[[ ! $num =~ ^[0-9]+$ ]] && echo "请输入数字" && exit
if [ $num -gt 18 ];then
echo "too big"
elif [ $num -lt 18 ];then
echo "too small"
else
echo "you got it"
fi
2、示例:查询成绩
======================版本1======================
[root@web01 ~]# vim hzl.sh
#!/bin/bash
read -p "输入成绩:" score
if [ $score -ge 90 ];then
echo "优秀"
elif [ $score -ge 70 ];then
echo "优良"
elif [ $score -ge 60 ];then
echo "一般"
elif [ $score -lt 60 ];then
echo "较差"
fi
[root@web01 ~]# sh hzl.sh
输入成绩:87
优良
[root@web01 ~]#
======================版本2======================
#!/bin/bash
read -p "your score>>> " score
[[ ! $score =~ ^[0-9]+$ ]] && echo "请输入数字" && exit
if [ $score -ge 90 ];then
echo "优秀"
elif [ $score -ge 70 ];then
echo "良好"
elif [ $score -ge 60 ];then
echo "一般"
else
echo "较差"
fi
3、示例:判断是否是数字
read -p "请输入一个数值: " num
while :
do
if [[ $num =~ ^[0-9]+$ ]];then
break
else
read -p "不是数字,请重新输入数值: " num
fi
done
echo "你输入的数字是: $num"
4、示例:编写99乘法表
[root@web01 opt]# cat hzl.sh
#!bin/bash
for x in {1..9}
do
for y in {1..9}
do
[ $y -le $x ] && echo -n "$y x $x = `echo $(($x*$y))` "
done
echo ""
done
[root@web01 opt]# sh hzl.sh
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
四、脚本练习
1、编写脚本,命令行传入一个文件路径,判断文件的类型
[root@localhost ~]# cat test_file.sh
#!/bin/bash
if [ -d $1 ]
then
echo "$1 is directory"
elif [ -b $1 ]
then
echo "$1 is block"
elif [ -f $1 ]
then
echo "$1 is regular file"
else
echo 'unknown'
fi
[root@localhost ~]# ./test_file.sh /etc/passwd
/etc/passwd is regular file
2、检测指定的主机是否可以ping通,必须使用$1变量
[root@egon test]# cat ping.sh
#!/bin/bash
ping -c2 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "ok"
else
echo "down"
fi
[root@egon test]# chmod +x ping.sh
[root@egon test]# ./ping.sh 10.10.0.1
down
[root@egon test]#
3、判断一个用户是否存在
[root@egon test]# cat check_user.sh
#!/bin/bash
id $1 &> /dev/null
if [ $? -eq 0 ];then
echo "user $1 exists"
else
echo "user $1 not exists"
fi
[root@egon test]# chmod +x check_user.sh
[root@egon test]# ./check_user.sh egon
user egon exists
[root@egon test]# ./check_user.sh xx
user xx not exists
4、检测httpd软件是否安装,没有的话则安装
[root@egon test]# cat check_httpd.sh
#!/bin/bash
rpm -q httpd &>/dev/null
if [ $? -eq 0 ];then
echo "已经安装"
else
echo "正在安装..."
yum install httpd -y &>/dev/null
fi
5、判断80端口的状态,未开启则重启
[root@egon test]# cat check_port.sh
#!/bin/bash
netstat -an |grep LISTEN |grep '\b80\b' &>/dev/null
if [ $? -eq 0 ];then
echo "80端口ok"
else
echo "80端口down"
echo "正在重启..."
systemctl restart httpd &> /dev/null
if [ $? -eq 0 ];then
echo "重启成功"
else
echo "重启失败"
fi
fi
6、编写监控脚本,如果根分区剩余空间小于10%,内存的可用空间小于30%
向用户egon发送告警邮件,邮件的内容包含使用率相关信息
#脚本编写
[root@egon test]# cat monitor.sh
#!/bin/bash
#! /bin/bash
# 提取根分区剩余空间
use_disk=`df / | grep / | awk '{print $5}'`
use_percent=`echo $use_disk|cut -d% -f1`
# 提取内存剩余空间
avail_mem=`free | awk 'NR==2{print $NF}'`
total_mem=`free | awk 'NR==2{print $2}'`
avail_percent=`echo "scale=2;$avail_mem/$total_mem"|bc | cut -d. -f2`
# 注意 磁盘提取的数值单位为 kb、 内存提取的单位为 Mb
if [ $use_percent -gt 90 ];then
echo "邮件内容:根分区已经使用为${user_disk}低于10%,请及时处理!!!" | mail -s "硬盘报警邮件" root
fi
if [ $avail_percent -lt 30 ];then
echo "邮件内容:内存剩余${free_percent}%,低于30%" | mail -s "内存报警邮件" xxx@163.com
fi
测试:
# 查看163邮箱
[root@egon test]# cat /var/spool/mail/root
mailx配置
[root@egon ~]# yum install mailx -y
[root@egon ~]# cat /etc/mail.rc
set from=378533872@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=378533872@qq.com
set smtp-auth-password="xxxxxxxxxx"
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
注释
set from:设置发件人
set smtp:设置外部STMP服务器
set smtp-auth-user:设置STMP用户名(一般为完整邮箱地址)
set smtp-auth-password:设置SMTP密码,需要登录378533872@qq.com在设置->账户->开启POP3/SMTP服务->获取密码
测试
[root@egon ~]# echo "卧槽" | mail -s "报警邮件" 18611453110@163.com
[root@egon ~]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.
上述报错的解决方式为,依次执行下述命令
mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -L -d /root/.certs
cd /root/.certs
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
# 最后出现这句就可以了
Notice: Trust flag u is set automatically if the private key is present.
# 重新修改配置文件的最后一行
[root@egon ~]# cat /etc/mail.rc
set from=378533872@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=378533872@qq.com
set smtp-auth-password="xxxxxxxxxx"
set smtp-auth=login
set ssl-verify=ignore
# set nss-config-dir=/etc/pki/nssdb/ # 改为下面一行
set nss-config-dir=/root/.certs
# 然后重新测试邮件发送即可
7、根据操作系统不同进行yum源优化 centos6 centos7 centos8
[root@egon shell]# cat check_yum.sh
#!/bin/bash
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &>/dev/null
var=$(awk '{print $(NF-1)}' /etc/redhat-release)
os_version=`echo ${var%%.*}`
if [ $os_version -eq 7 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
elif [ $os_version -eq 6 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
elif [ $os_version -eq 5 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo &>/dev/null
else
echo "请检查确认系统版本信息"
fi
本文来自博客园,作者:ଲ一笑奈&何,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121525.html