1. 程式人生 > >shell腳本的if判斷語句

shell腳本的if判斷語句

mkdir 大小 是否 elif for tmp tar.gz 等於 命令

if語句

1、按照文件類型進行判斷
-d 文件)判斷該文件是否存在,並且是否為字符設備文件(是字符設備為真)
-e 文件)判斷該文件是否存在(存在為真)
[-e /root]
echo $?輸出為0 #判斷為真,即為0
常用的格式有:
[ -e /root ] && echo yes || echo no

一、單分支if條件語句格式有如下兩種:

if [ 條件判斷式 ];then
程序
fi
或者
if [ 條件判斷式 ]
then
程序
fi

但分支條件語句需要註意幾個點
·if語句使用fi結尾,和一般語言使用大括號結尾不同
·[ 條件判斷式 ]就是使用test命令判斷,所以中括號和條件判斷式之間必須有空格

·then後面和符合條件之後執行的程序,可以放在[]之後,用“;”分割。也可以換行寫入,就不需要是“;”了

例子:判斷分區使用率
#!/bin/bash
#統計根分區使用率
#Author:xuyizhong (E-mail:[email protected])
rate=$(df -h |grep "/dev/vda1" | awk ‘{print $5}‘ | cut -d "%" -f1)
#把根分區使用率作為變量值賦予變量rate
if [ $rate -ge 80 ]
then
echo "Warning! /dev/vda1 is full !!!"
fi

二、雙分支if條件語句

if [ 條件判斷式 ]
then
條件成立時,執行的程序
else
條件不成立時,執行的另一個程序
fi
例子1:備份mysql數據庫
#!/bin/bash
#備份mysql數據庫
#Author:xuyizhong (E-mail:[email protected])
ntpdate asia.pool.ntp.org &> /dev/null
#同步系統時間
date=$(date +%y%m%d)
#把當前系統時間按照"年月日"格式賦予變量date
size=$(du -sh /var/lib/mysql)
#統計mysql數據庫的大小,並把大小賦予size變量
if [ -d /tmp/dbbak ]

#判斷/tmp/dbbak文件夾是否存在,存在執行then,不存在執行else
then
echo "Date : $date" > /tmp/dbbak/dbinfo.txt
echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt
cd /tmp/dbbak
tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &> /dev/null
rm -rf /tmp/dbbak/dbinfo.txt
else
mkdir /tmp/dbbak
echo "Date : $date" > /tmp/dbbak/dbinfo.txt
echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt
cd /tmp/dbbak
tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &> /dev/null
rm -rf /tmp/dbbak/dbinfo.txt
fi

例子2:判斷apache是否啟動
#!/bin/bash
#Author:xuyizhong (E-mail:[email protected])

port=$(nmap -sT 192.168.1.156 | grep tcp |grep http |awk ‘print $2‘)
#使用nmap命令掃描服務器,並截取apache服務的狀態,賦予變量port
if [ "$port" == "open" ]
then
echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
else
/etc/rc.d/init.d/httpd start &> /dev/null
echo "$(date) restart httpd !!" >> /tmp/autostart-err.log
fi

#自己將apache停止,查看apache會不會自動重新啟動!

三、多分支if條件分支

if [ 條件判斷式1 ]
then
當條件判斷式1成立時,執行程序1
elif [ 條件判斷式2 ]
then
當條件判斷式2成立時,執行程序2
...省略更多條件...
else
當所有條件都不成立時,最後執行此程序
fi

例子1:判斷用戶輸入指令的是什麽
#!/bin/bash
#Author:xuyizhong (E-mail:[email protected])

read -p "Please input a filename:" file
#接收鍵盤的輸入,並賦予變量file

if [ -z "$file" ]
#判斷file變量是否為空
then
echo "Error,please input a filename"
exit 1
elif [ ! -e "$file" ]
#判斷file的值是否存在
then
echo "Your input is not a file!"
exit 2
elif [ -f "$file" ]
#判斷file的值是否為普通文件
then
echo "$file is a regulare file!"
elif [ -d "$file" ]
#判斷file的值是否為目錄文件
then
echo "$file is a directory!"
else
echo "$file is an other file!"
fi

case語句

case語句和if...elif...else語句一樣都是多分支條件語句,不過和if多分支語句不同的是,case語句只能判斷一種條件關系,而if語句可以判斷多種條件關系

case $變量名 in
"值1")
如果變量的值等於值1,則執行程序1
;;
"值2")
如果變量的值等於值2,則執行程序2
;;

如果變量的值都是以上的值,則執行此程序
;;
esac
例子1:
#!/bin/bash
#判斷用戶輸入
#Author:xuyizhong (E-mail:[email protected])
read -p "Please choose yes/no " -t 30 cho
#將用戶的值賦予給cho
case $cho in
"yes")
echo "Your choose is yes!"
;;
"no")
echo "Your choose is no!"
;;
)
echo "Your choose is error!"
;;
esac
例子2:
#!/bin/bash
echo ‘ you want to shanghai,please input "1" ‘
echo ‘ you want to guangzhou,please input "2" ‘
echo ‘ you want to chengdu,please input "3" ‘
read -t 30 -p "Please input your choose " -t 30 cho

case "$cho" in
"1")
echo "shanghai 的機票已經發售"
;;
"2")
echo "guangzhou 的機票已經發售"
;;
"1")
echo "chengdu 的機票已經發售"
;;
*)
echo "error 1/2/3"
;;
esac

for循環

while循環

shell腳本的if判斷語句