條件判斷if
阿新 • • 發佈:2018-06-18
then mail ice suid 配置 inux G1 red AD 條件判斷:
語法結構:
if [ 條件 ];then
command...
fi
++++++++++++++++++++++
if [ 條件 ];then
command...
else
command...
fi
++++++++++++++++++++++
if [ 條件1 ];then
command1...
elif [ 條件2 ];then
command2...
else
command3...
fi
++++++++++++++++++++++
if [ 條件1 ];then
command1...
if [ 條件2 ];then
command2...
fi
else
if [ 條件3 ];then
command3...
elif [ 條件4 ];then
command4...
else
command5...
fi
fi
# man test
文件存在與否的判斷
-e:是否存在;
-f:是否存在並且為普通文件
-d:是否存在並且為目錄
-S:是否存在並且套接字文件
-p:是否存在並且管道文件
-c:是否存在並且字符文件
-b:是否存在並且塊設備文件
-L:是否存在並且符號連接文件
文件權限相關判斷:
-r:可讀
-w:可寫
-x:可執行
-u:是否有suid
-g:是否有sgid
-k:是否有t位
-s:是否為空白文件,-s表示非空;! -s 空文件
兩個文件比較:
file1 -nt file2 : 比較文件的新(文件創建的時間)
file1 -ot file2:比較文件的老(文件創建的時間)
file1 -ef file2 比較file1和file2是否是同一個文件,也可以來判斷是否為硬鏈接文件
整數比較:
-eq :相等
-ne :不等
-gt:大於
-lt:小於
-ge:大於等於
-le:小於等於
字符串比較:
-z:判斷是否為空的字符串
-n:是否為非空的字符串
string1 = string2
string1 != string2
多重條件判斷:
-a 和 && 邏輯與 [ 條件1 -a 條件2 ] 條件1和條件2必須同時成立整個大條件才成立
-o 和 || 邏輯或 [ 條件1 -o 條件2 ] 條件1和條件2其中一個滿足整個大條件就成立
! 非
優先級:
-a 和 && 大於 -o 和 || 大於 !
demo1:判斷ip是否通
ping -c --->$?
# vim ping_ip.sh
#!/bin/bash
#Name:xxx
#Usage:xxx
#Update:
#Path:xxx
(1) ping -c 2 10.1.1.254 > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log
else
echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log
fi
(2) ping -c 2 $1 > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log
else
echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log
fi
(3)
read -p "Input your ip:" IP
ping -c 2 $IP > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log
else
echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log
fi
(4)
read -p "Input your ip:" IP
ping -c 2 $IP > /dev/null 2>&1
[ $? -eq 0 ] && echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log || echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log
(5)
read -p "Input your ip:" IP
ping -c 2 $IP > /dev/null 2>&1
test $? -eq 0 && echo "$(date ‘+%F %T‘)the ip is ok" > /tmp/ip.log || echo "$(date ‘+%F %T‘)the is is not ok" >> /tmp/ip.log
demo2:判斷一個進程是否存在
#!/bin/bash
ps -ef|grep vsftpd|grep -v ‘grep‘ &>/dev/null
或者
( # pgrep -l vsftpd)
[ $? -eq 0 ] && echo "vsftpd ok" >>/tmp/log ||echo "vsftpd not ok" >>/tmp/log
demo3:判斷一個服務是否正常
#!/bin/bash
wget -P /tmp http://localhost &>/dev/null
[ $? -ne 0 ] && echo "httpd not ok" >>/tmp/log ||echo "httpd is ok" >>/tmp/log
練習:
1、寫一個腳本判斷一個用戶是否存在
2、完善上第一個例子中給腳本傳參時的bug,如果每個給腳本傳參或者參數個數不等於1時,提示腳本的用法:usage:xxx.sh ip
練習1:
1、判斷/tmp/run目錄是否存在,如果不存在就建立,如果存在就刪除目錄裏所有文件
#!/bin/bash
dir=/tmp/run
[ -f $dir ] && mv $dir $dir.bak
[ -d $dir ] && rm -fr $dir/* || mkdir $dir -p
2、輸入一個路徑,判斷路徑是否存在,而且需要輸出是文件還是目錄,如果是鏈接文件,還需要輸出是有效連接還是無效鏈接
3、交互式輸入一個ip,然後腳本判斷這個ip是否能ping通,輸入結果如下:
Server 10.1.1.20 is Down 最後要求將結果郵件給管理員root和redhat
echo hello |mail root
mail -s xxx root
4、寫一個腳本,要求當給腳本輸入參數hello時,腳本返回world;給腳本輸入world時,腳本返回hello。當腳本沒有參數或者參數錯誤時,屏幕上輸出腳本的用法:"usage:xxx hello or world"
5、寫一個腳本自動搭建nfs服務
/nfs/share 10.1.1.1(rw)
#!/bin/bash
#關閉防火墻和selinux
service iptables stop
setenfoce 0 > /dev/null
#配置yum源
#1.判斷內網網絡是否ok
ip=10.1.1.254
ping -c $ip >/dev/null
# 判斷
# wget -P /etc/yum.repos.d/ ftp://$ip/xxx.repo &>/dev/null
#軟件三步曲
# yum -y install xxxx
read -p "dir" dir
[ ! -d $dir ] && mkdir -p $dir
chmod 1777 $dir
read -p "192.168.0.1(ro):" host
cat >> /etc/exports << end
$dir $host
end
#重啟服務
#測試驗證
mkdir /u01
mount.nfs $ip:/$dir /u01
....
條件判斷if