標籤:passwd disk lease .sh 開始 存在 思想 檔案大小 大小
---恢複內容開始---
檔案測試運算式
-f 檔案存在且為普通檔案
-d 檔案存在且為目錄檔案
-s 檔案大小不為0則真
-e 檔案存在則真
-r 檔案存且可讀 #檔案屬性對於root有時不好用
-w 寫
-x 執行
-L 存在且為連結檔案
f1 -nt f2 f1比f2新則為真
f1 -ot f2 f1比f2舊則為真
這些操作符可用於[[]],[],test幾乎通用,更多操作符查看man test.
生產案例:
prog="rpcbind"
[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog #如果存在則載入
[ -f /etc/sysconfig/$prog ] || exit 1 #不存在退出
案例2,大括弧可執行多個命令
[ $q -eq 3 ]&&{
echo 1
echo 2
echo 3
}
判斷是否為整數:去掉“非數字”是否等於本身 ["`echo "123a|sed -r ‘s#[^0-9]##g‘`" = "123a"]
#!/bin/sh
read -p "pls input two num :" num1 num2
a=num1
b=num2
#no.1
[ $# -ne 2 ]&&{ #"$#"表示輸入的變數個數
echo "usage: num1 num2"
exit 1
}
#no.2
["`echo "$a"|sed -r ‘s#[^0-9]##g‘`" = "$a" ]||{
echo "first arg must be int"
exit 2
}
#no.3
[ $1 -lt $b ]&&{
echo "$a<$b"
exit 3
}
企業綜合案例:1.當使用者輸入1時,輸出“start install lamp。” 然後執行/server/script/lamp.sh,指令碼內容輸出 “lamp is install”後退出指令碼
2。當使用者輸出2時,輸出“start install lnmp。” 然後執行/server/script/lnmp.sh,指令碼內容輸出 “lnmp is install”後退出指令碼
3.當輸入3時,退出當前菜單及指令碼
4.當輸入任何其他自負,給出提示“input error”後 退出指令碼
5.要對執行的指令碼進行條件判斷,例如:指令碼是否存在,是否可執行等。
列印簡單選擇菜單樣本1,
cat <<END
1.wangfei
2.gongli
END
read -p "which do you like?:" a
echo "i guess,you like $a"
列印簡單選擇菜單樣本2,函數寫法,
menu(){
cat <<END
1.[install lamp]
2.[install lnmp]
3.[exit]
pls input the num you want:
END
}
menu
read num
[ "$num" = "1" ]&&{
echo "start install lamp."
[ -x /server/script/lamp.sh ]||{
echo "lamp.sh error"
exit 2}
. /server/script/lamp.sh
}
[ "$num" = "3" ]&&{
echo "bye"
exit 0
}
echo "Iput Error"
exit 1
列印多級菜單樣本:
cat << EOF
|-----------System Infomation-----------
| DATE :$DATE
| HOSTNAME :$HOSTNAME
| USER :$USER
| IP :$IPADDR
| DISK_USED :$DISK_SDA
| CPU_AVERAGE:$cpu_uptime
----------------------------------------
|****Please Enter Your Choice:[0-5]****|
----------------------------------------
(1) Configure $SERVER1 Service
(2) Configure $SERVER2 Service
(3) Configure NETWORKE Service
(4) Change Passwd
(5) Logs
(0) Quit
EOF
read -p "Please enter your choice[0-5]: " input
case $input in
#web service
1)
clear
while [ "$flag" -eq 0 ]
do
cat << EOF
----------------------------------------
|****Please Enter Your Choice:[0-3]****|
----------------------------------------
(1) Start $SERVER1 Service
(2) Stop $SERVER1 Service
(3) ReStart $SERVER1 Service
(0) Back
EOF
read -p "Please enter your choice[0-3]: " input1
case $input1 in
1)
echo -e "\n>>>>>>>>>>>$DATE Start $SERVER1">>/log.txt
$SERVER1_START 2>>/log.txt
if [ $? == 0 ];then
echo "Start $SERVER1......................................................[OK]"
else
echo "Start $SERVER1......................................................[FAILED]"
fi
sleep 5
clear
;;
2)
echo -e "\n>>>>>>>>>>>$DATE Stop $SERVER1">>/log.txt
$SERVER1_STOP 2>>/log.txt
if [ $? == 0 ];then
echo "Stop $SERVER1.......................................................[OK]"
else
echo "Stop $SERVER1.......................................................[FAILED]"
fi
sleep 5
clear
;;
3)
echo -e "\n>>>>>>>>>>>$DATE ReStart $SERVER1">>/log.txt
/usr/local/tdoa/php/sbin/php-fpm restart 2>>/log.txt
$SERVER1_RESTART 2>>/log.txt
if [ $? == 0 ];then
echo "Restart $SERVER1....................................................[OK]"
else
echo "Restart $SERVER1....................................................[FAILED]"
fi
sleep 5
clear
;;
0)
clear
break
;;
*) echo "----------------------------------"
echo "| Warning!!! |"
echo "| Please Enter Right Choice! |"
echo "----------------------------------"
---恢複內容結束---
shell檔案測試,菜單表示思想