第四章 流程控制之if判斷
阿新 • • 發佈:2020-11-23
一、單分支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" [email protected]
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" [email protected]
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 [email protected]
set smtp=smtps://smtp.qq.com:465
set [email protected]
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