第四章 流程控制之if判断

一、单分支if

1)语法

if 条件;then
  要执行的命令1
  要执行的命令2
  要执行的命令3
 ...
fi
# 上述语法可以用一行代码代替
[ 条件信息 ] && xxx

2)示例

[root@jh 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@jh test]# . disk_monitor.sh
warning:Not enough hard disk space

注意:if 测试中还可以执行命令 根据命令的返回值做判断
[root@jh ~]# if cd / ;then echo Y ;fi
Y
[root@jh /]# if grep -q root /etc/passwd ;then echo Y ;fi
Y

二、双分支if

1)语法

if 条件;then
  要执行的命令1
  要执行的命令2
  要执行的命令3
 ...
else
  要执行的命令1
  要执行的命令2
  要执行的命令3
 ...
fi
# 上述语法可以用一行代码代替
[ 条件信息 ] && xxx || xxxx

2)示例

#!/bin/bash
username='jh'
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

1)语法

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

2)示例

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@db04 /scripts/day04]# vim if_file.sh
#!/bin/bash

if [ -d $1 ];then
    echo "$1 is a directory"
elif [ -b $1 ];then
    echo "$1 is block"
elif [ -f $1 ];then
    echo "$1 is regular file" 
else
    echo  "unknown"
fi

[root@db04 /scripts/day04]# sh if_file.sh /etc/yum.repos.d/
/etc/yum.repos.d/ is a directory

2、检测指定的主机是否可以ping通,必须使用$1变量
[root@db04 /scripts/day04]# vim ping.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  16:35:23
# Name:ping.sh
# Version: 1.0
# Discription: To  

ping -c4 $1 &>/dev/null
if  [ $? -eq 0 ];then
    echo "ping $1 is ok"
else
    echo "ping $1 is fail"
fi

或者
ping -c4 $1 &>/dev/null
[ $? -eq 0 ] && echo "ping $1 is ok" || echo "ping $1 is fail"

[root@db04 /scripts/day04]# chmod +x ping.sh 
[root@db04 /scripts/day04]# sh ping.sh 172.16.1.52
ping 172.16.1.52 is fail
[root@db04 /scripts/day04]# sh ping.sh www.baidu.com
ping www.baidu.com is ok

3、判断一个用户是否存在
[root@db04 /scripts/day04]# vim if_user.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  16:49:39
# Name:if_user.sh
# Version: 1.0
# Discription: To 

id $1 &>/dev/null
[ $? -eq 0 ] && echo "$1 用户存在" || echo ""$1 用户不存在"
[root@db04 /scripts/day04]# chmod +x if_user.sh 
[root@db04 /scripts/day04]# sh if_user.sh mysql
mysql 用户不存在
[root@db04 /scripts/day04]# sh if_user.sh root
root 用户存在

4、检测httpd软件是否安装,没有的话则安装
[root@db04 /scripts/day04]# vim if_httpd.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  17:05:18
# Name:if_httpd.sh
# Version: 1.0
# Discription: To  

rpm -qc $1 &>/dev/null
if [ $? -eq  0 ] ;then
    echo "$1 已经安装"
else
    echo "$1 正在安装"
    yum -y install $1 &>/dev/null
    echo "$1 安装完成"
fi
[root@db04 /scripts/day04]# chmod  +x if_httpd.sh 
[root@db04 /scripts/day04]# sh if_httpd.sh unzip
unzip 已经安装
[root@db04 /scripts/day04]# sh if_httpd.sh reids
reids 正在安装
reids 安装完成

5、判断80端口的状态,未开启则重启
[root@db04 ~]# vim if_port.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  17:17:14
# Name:if_port.sh
# Version: 1.0
# Discription: To  

netstat  -lntp |grep 80 &>/dev/null
if [ $? -eq 0 ];then
    echo "80 is starting" 
else
    echo "80 is opening"
    systemctl start httpd &>dev/null
    if [ $? -eq 0 ];then
        echo "80 is started"
    else
        echo "80 is star fail"
    fi
fi  
[root@db04 ~]# chmod  +x if_port.sh
[root@db04 /scripts/day04]# sh if_port.sh 
80 is opening
80 is started

6、编写监控脚本,如果
根分区剩余空间小于10%
内存的剩余空间小tg于30%
向用户jh发送告警邮件,邮件的内容包含使用率相关信息
[root@db04 /scripts/day04]# vim use_space.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  18:58:18
# Name:use_space.sh
# Version: 1.0
# Discription: To  

#提取磁盘使用空间
use_disk=`df |awk 'NR==2{print $(NF-1)}' |cut -d% -f1`

#提取内存使用情况
free_mem=`free -m | awk '/Mem/{print $4}'`
total_mem=`free -m | awk '/Mem/{print $2}'`
free_percent=`echo "scale=2;$free_mem/$total_mem" | bc | cut -d. -f2`

#磁盘报警设置
if [ $use_disk -gt 90 ];then
    echo "${use_disk}% of root partition space used,The remaining space of the root partition is less than 10%.Please ha
ndle it as soon as possible" | mail -s "Space use warning"  974089352@qq.com
fi

#内存报警设置
if [ $free_percent -gt 70 ];then
    echo "${free_percent}% of the memory space has been used, and the memory space is less than 30%. Please handle it as 
soon as possible" | mail -s "Space use warning"  974089352@qq.com
fi

[root@db04 /scripts/day04]# chmod  +x use_space.sh 
[root@db04 /scripts/day04]# sh use_space.sh 

#配置邮件信息
[root@db04 ~]# yum -y install  mailx
[root@db04 ~]# vim /etc/mail.rc 
set from=974089352@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=974089352@qq.com
set smtp-auth-password="myednghevhawbcib"
set smtp-auth=login
set ssl-verify=ignore
# set nss-config-dir=/etc/pki/nssdb/
set nss-config-dir=/root/.certs

[root@db04 /scripts/day04]# mkdir -p /root/.certs/
[root@db04 /scripts/day04]# cd /root/.certs/
[root@db04 ~/.certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
[root@db04 ~/.certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@db04 ~/.certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@db04 ~/.certs]# certutil -L -d /root/.certs
[root@db04 ~/.certs]# cd /root/.certs
[root@db04 ~/.certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt



7、根据操作系统不同进行yum源优化 centos6 centos7 centos8
[root@db04 /scripts/day04]# vim sys_opt.sh
#!/bin/bash
# Author:jh
# Time:2020-11-19  20:23:26
# Name:sys_opt.sh
# Version: 1.0
# Discription: To  

\cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

sys_version=`cat /etc/redhat-release |awk '{print $4}' |cut -d. -f1`
if [ $sys_version -eq 7 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
    [ $? -eq 0 ]  && echo "Centos 7 yum update success..."  
elif [ $sys_version -eq 6 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
    [ $? -eq 0 ]  && echo "Centos 6 yum update success..." 
elif [ $sys_version -eq 5 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo &>/dev/null
    [ $? -eq 0 ]  && echo "Centos 5 yum update success..."
else
    echo "Please check system version"
fi````````````````````````````````````` 

[root@db04 /scripts/day04]# chmod +x sys_opt.sh

posted @ 2020-11-23 19:15  年少纵马且长歌  阅读(133)  评论(0编辑  收藏  举报