1. 程式人生 > >if語句和case語句用法展示

if語句和case語句用法展示

法律 括號 如果 if...else 是你 acc back please status

                              if語句和case語句用法展示

                                                    作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

一.單分支if條件語句 1>.語法格式一
1 if [ 條件判斷式 ];then
2   源代碼
3 fi

2>.語法格式二
1 if [ 條件判斷式 ]
2   then
3     源代碼
4 fi

3>.但分支條件語句需要註意幾個點
a>.if語句使用fi結尾,和一般語言使用大括號結尾不同;
b
>.[ 條件判斷式 ]就是使用test命令判斷,所以中括號和條件判斷式之間必須有空格; c>.then後面跟符合條件之後執行的程序,可以放在[]之後,用";"分割。也可以換行寫入,就不需要";"啦;

4.案例展示
 1 [[email protected] shell]# more partitions.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:[email protected]
 6 
 7 res=$(df -h | grep
"/dev/sda2" | awk {print $5}| cut -d "%" -f1) 8 9 if [ $res -ge 20 ] #為了測試方便,我這裏將數據修改,生產環境建議設置成80! 10 then 11 echo "Warning! /dev/sda2 is full!!!" 12 fi 13 [[email protected] shell]#

二.多分支if條件語句 1.語法格式
1 if [ 條件判斷式 ]
2 then
3   條件成立時,執行的源代碼。
4 else
5   條件不成立是,執行的另一個程序。
6 fi

2.案例展示一(備份服務器的某個目錄)
 1 [[email protected] backup]# more file_backup.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:[email protected]
 6 
 7 #同步系統時間,可以先ping通再選擇服務器,1.cn.pool.ntp.org;2.asia.pool.ntp.org;3.asia.pool.ntp.org
 8 ntpdate 2.asia.pool.ntp.org &> /dev/null
 9 
10 FilePath="/etc"
11 BackSave="/tmp/backup"
12 
13 
14 date=`date +%F` #也可以這樣玩,date +%y%m%d,看你個人習慣了!
15 
16 size=$(du -sh $FilePath)
17 
18 if [ -d $BackSave ]
19     then
20         echo "Date is:$date" > $BackSave/backup.log
21         echo "Size is:$size" > $BackSave/backup.log
22         cd $BackSave
23         tar -zcf $FilePath_$date.tar.gz $FilePath $BackSave/backup.log &> /dev/null
24         rm -rf $BackSave/backup.log
25     else
26         mkdir $BackSave
27         echo "Date is:$date" > $BackSave/backup.log
28         echo "Size is:$size" > $BackSave/backup.log
29         cd $BackSave
30         tar -zxf $FilePath_$date.tar.gz $FilePath $BackSave/backup.log &> /dev/null
31         rm -rf $BackSave/backup.log
32 fi
33 [[email protected] backup]# 
3.案例展示二(判斷某個服務是否啟動)
 1 [[email protected] backup]# more ServerAlarm.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:[email protected]
 6 
 7 Host=192.168.1.110
 8 
 9 res=`rpm -qa | grep nmap`
10 if [ -z $res ]
11     then
12         yum -y install nmap &> /dev/null 
13     else
14         echo “準備開始掃描主機服務!”
15 fi
16 
17 ServiceStatus=$(nmap -sT $Host |grep tcp | grep http|awk {print $2}) #使用nmap可以準確的判斷一臺服務器的服務是否正常。
18 
19 if [ "$ServiceStatus" == "open" ]
20     then
21         echo "$(date) httpd is ok !" >> /tmp/HttpdAlarm-acc.log
22     else
23         /etc/rc.d/init.d/httpd restart &>/dev/null  #註意,如果不是你本機的話,不能直接這麽幹喲,需要先遠程上去再執行命令!
24         echo "`date` restart httpd!!" >> /tmp/HttpdAlarm-err.log
25 fi
26 [[email protected] backup]# 
三.多分支if條件語句 1>.語法格式
1 if [ 條件判斷式1 ]
2 then
3     當條件判斷式1成立時,執行的程序1
4 elif [ 條件判斷式2 ]
5 then
6     當條件判斷式2成立時,執行程序2
7 else
8     當所有條件都不成立時,最後執行此程序
9 fi

2.案例展示
 1 [[email protected] backup]# more InputFile.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:[email protected]
 6 
 7 read -p "Please input a filename:" file
 8 
 9 if [ -z "$file" ]
10     then
11         echo "Eorr,please input a filename!"
12         exit 100
13 elif [ ! -e "$file" ]
14     then
15         echo "Your input is not a file!"
16 elif [ -f "$file" ]
17     then
18         echo "$file is a regulare file!"
19 elif [ -d "$file" ]
20     then
21         echo "$file is direcory!"
22 else
23         echo "$file is an other file!"
24 fi
25 [[email protected] backup]# 

四.多分支case條件語句 case語句和if...elif...else語句一樣都是多分支條件語句,不過if多分支語句不同的是,case語句只能判斷一種條件關系,而if語句可以判斷多種條件關系。 1.語法格式
 1 case $變量名 in
 2 "值1")
 3     如果變量的值等於1,則執行程序1
 4     ;;
 5 "值2")
 6     如果變量的值等於2,則執行程序2
 7     ;;
 8 "值3")
 9     如果變量的值等於3,則執行程序3
10     ;;
11 *12     如果變量的值都不是以上的值,則執行此程序
13     ;;
14 esac

2.案例展示
 1 [[email protected] backup]# more choose.sh 
 2 #!/bin/bash
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 #EMAIL:[email protected]
 6 
 7 echo "If you want to beijing,please input [1]"
 8 echo "If you want to shanghai,please input [2]"
 9 echo "If you want to shijiazhuang,please input [3]"
10 echo "If you want to wenzhou,please input [4]"
11 
12 read -t 30 -p "Where do you want to go ? >>> " choose
13 
14 case "$choose" in
15     "1")
16         echo "Go to beijing!"
17         ;;
18     "2")
19         echo "Go to shanghai!"
20         ;;
21     "3")
22         echo "Go to shijiazhuang!"
23         ;;
24     "4")
25         echo "Go to wenzhou!"
26         ;;
27      *)
28         echo "Please enter 1/2/3/4,Thanks!"
29         ;;
30 esac
31 
32 [[email protected] backup]# 

if語句和case語句用法展示