第一节: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

注意:if 测试中还可以执行命令 根据命令的返回值做判断

[root@egon ~]# if cd / ;then echo Y ;fi
Y
[root@egon /]# if grep -q root /etc/passwd ;then echo Y ;fi
Y

二:双分支if

语法

if 条件;then
    要执行的命令1
    要执行的命令2
    要执行的命令3
    ...
else
    要执行的命令1
    要执行的命令2
    要执行的命令3
    ...
fi

# 上述语法可以用一行代码代替
[ 条件信息 ] && xxx || xxxx

示例

#!/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======================
#!/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======================
#!/bin/bash
read -p 'your score: ' score

if [ $score -ge 90  ];then
    echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
    echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
    echo '一般'
elif [ $score -lt 60 ];then
    echo '较差'
fi

======================版本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"

四 练习

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发送告警邮件,邮件的内容包含使用率相关信息

答案

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术