-eq 等于則為真 [ 1 -eq 1 ]
-ne 不等于
-ge 大于等于
-le 小于等于
-gt 大于
-lt 小于
[root@shell /scripts]# [ 1 -eq 1 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 11 -eq 1 ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ 11 -ne 1 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 1 -ne 1 ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ 1 -ge 1 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 1 -ge 2 ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ 1 -le 2 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 11 -le 2 ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ 11 -gt 2 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 1 -gt 2 ] && echo "為真"? || echo "為假"
為假
[root@shell /scripts]# [ 1 -lt 2 ] && echo "為真"? || echo "為假"
為真
[root@shell /scripts]# [ 11 -lt 2 ] && echo "為真"? || echo "為假"
為假
#編寫(xiě)一個(gè)腳本,判斷一個(gè)服務(wù)是否在運(yùn)行
systemctl? status? nginx? 查看服務(wù)的是否在運(yùn)行
返回值等于0? 服務(wù)正在運(yùn)行
返回值等于3? 服務(wù)沒(méi)有在運(yùn)行
返回值等于4? 沒(méi)有這個(gè)服務(wù)
[root@shell /scripts]# cat services.sh
#!/bin/bash
#1.提示用戶(hù)輸入要檢測(cè)的服務(wù)名稱(chēng)
read -p "請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):" Ser
#2.判斷服務(wù)所在的狀態(tài)
systemctl status? $Ser? &>/dev/null
Rc=$?
#3.根據(jù)返回值的結(jié)果進(jìn)行判斷
if [ $Rc -eq 0 ];then
? ? echo "服務(wù)$Ser 正在運(yùn)行中........"
elif [ $Rc -eq 3 ];then
? ? echo "服務(wù)$Ser 沒(méi)有運(yùn)行.........."
elif [ $Rc -eq 4 ];then
? ? echo "您所輸入的服務(wù),系統(tǒng)沒(méi)有安裝。"
else
? ? echo "您輸入的服務(wù)名稱(chēng)不符合規(guī)范."
fi
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):nginx
您所輸入的服務(wù),系統(tǒng)沒(méi)有安裝。
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):mariadb
服務(wù)mariadb 沒(méi)有運(yùn)行..........
[root@shell /scripts]# systemctl? start mariadb.service
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):mariadb
服務(wù)mariadb 正在運(yùn)行中........
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):firewalld
服務(wù)firewalld 沒(méi)有運(yùn)行..........
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):sshd
服務(wù)sshd 正在運(yùn)行中........
[root@shell /scripts]# sh? services.sh
請(qǐng)輸入你要檢測(cè)的服務(wù)名稱(chēng):ssh
您所輸入的服務(wù),系統(tǒng)沒(méi)有安裝。
#判斷磁盤(pán)根分區(qū)的使用率,如果使用率大于80,則發(fā)郵件報(bào)警
1.什么命令查看磁盤(pán)的使用率
df -h | awk? '/\/$/{print $(NF-1)}'
[root@shell /scripts]# cat? disk.sh
#!/bin/bash
#1.獲取磁盤(pán)根分區(qū)的使用率
Disk_Use=$(df -h | awk? '/\/$/{print $(NF-1)}')
#2.根據(jù)使用率值的大小進(jìn)行判斷是否觸發(fā)報(bào)警
if [ ${Disk_Use/\%/} -gt 6 ];then
? ? echo "當(dāng)前系統(tǒng)的根分區(qū)的使用率大于80%,使用率為$Disk_Use"
else
? ? echo "當(dāng)前系統(tǒng)根分區(qū)的使用率正常,使用率為$Disk_Use"
fi
[root@shell /scripts]# sh disk.sh
當(dāng)前系統(tǒng)根分區(qū)的使用率正常,使用率為5%
[root@shell /scripts]# sh disk.sh
當(dāng)前系統(tǒng)的根分區(qū)的使用率大于80%,使用率為9%
[root@shell ~]# dd? if=/dev/zero of=/root/test.log? bs=100M count=100
#判斷內(nèi)存的使用率,大于80%則報(bào)警
#創(chuàng)建用戶(hù)的腳本
1. 怎么創(chuàng)建用戶(hù)? useradd
2. 如果用戶(hù)已經(jīng)存在,則不需要?jiǎng)?chuàng)建
3. 創(chuàng)建完用戶(hù),判斷用戶(hù)是否創(chuàng)建成功
[root@shell /scripts]# cat? useradd.sh
#!/bin/bash
#1.提示用戶(hù)輸入要?jiǎng)?chuàng)建的用戶(hù)
read -p "請(qǐng)輸入你要?jiǎng)?chuàng)建的用戶(hù):" User
#2. 判斷該用戶(hù)是否存在該系統(tǒng)
id? $User &>/dev/null?
if [ $? -eq 0 ];then
? ? echo "用戶(hù)$User 已經(jīng)存在!"
else
? ? useradd $User &>/dev/null
? ? if [ $? -eq 0 ];then
? ? ? ? echo "用戶(hù)$User 創(chuàng)建成功!"
? ? else
? ? ? ? echo "用戶(hù)$User 創(chuàng)建失?。?
? ? fi
fi
[root@shell /scripts]# sh? useradd.sh
請(qǐng)輸入你要?jiǎng)?chuàng)建的用戶(hù):mysql
用戶(hù)mysql 已經(jīng)存在!
[root@shell /scripts]# sh? useradd.sh
請(qǐng)輸入你要?jiǎng)?chuàng)建的用戶(hù):www
用戶(hù)www 創(chuàng)建成功!
[root@shell /scripts]# id www
uid=1000(www) gid=1000(www) groups=1000(www)
[root@shell /scripts]# sh? useradd.sh
請(qǐng)輸入你要?jiǎng)?chuàng)建的用戶(hù):www
用戶(hù)www 已經(jīng)存在!
#使用函數(shù)庫(kù),測(cè)試網(wǎng)址是否能通。
[root@shell /scripts]# cat url.sh
#!/bin/bash
#0.調(diào)用函數(shù)庫(kù)
[ -f /etc/init.d/functions ] &&? source /etc/init.d/functions
#1.提示用戶(hù)輸入要進(jìn)行測(cè)試的url網(wǎng)址
read -p? "請(qǐng)輸入你要測(cè)試的Url地址:" Url
#2. 判斷該網(wǎng)址是否能通
ping? -c1 -W1 $Url &>/dev/null
#3.根據(jù)返回值判斷網(wǎng)址是否通暢
if [ $? -eq 0 ];then
? ? action? "Url網(wǎng)址$Url 是通暢的......."? /bin/true
else
? ? action? "Url網(wǎng)址$Url 是不通暢的....."? /bin/false
fi
[root@shell /scripts]# sh url.sh
請(qǐng)輸入你要測(cè)試的Url地址:www.baidu.com
Url網(wǎng)址www.baidu.com 是通暢的.......? ? ? ? ? ? ? ? ? ? ? [? OK? ]
[root@shell /scripts]# sh url.sh
請(qǐng)輸入你要測(cè)試的Url地址:www.baiduy.com
Url網(wǎng)址www.baiduy.com 是不通暢的.....? ? ? ? ? ? ? ? ? ? ? [FAILED]