Linux基礎之bash腳本編程進階篇-選擇執行語句(if,case)

bash腳本的書寫規範簡介

看本文需要了解的腳本撰寫習慣:bash

開頭頂格寫#!緊接着寫解釋器路徑/bin/bash

由於bash屬於腳本語言,腳本語言的運行方式

解釋運行:源代碼 --> 運行時啓動解釋器,由解釋器邊解釋邊運行

Linux中的腳本解釋器有:zsh,csh,bash,tsh衆多shell,不過bash最常用。

第一行寫完之後,就可以直接寫代碼。不過爲了便於他人閱讀通常會增加如下行:

第二行:#版本信息

第三行:#作者與聯繫方式

第四行:#版權宣告方式

第五行:#History

一般會增加上面4行註釋信息。

除第一行#外其餘行只要#後內容腳本統統不識別,因此可用#作爲註釋使用

                                                                  實驗環境CentOS7.2


bash腳本語句執行順序

bash腳本中分爲順序執行選擇執行循環執行這大致三類執行順序。

順序執行,就是從上到下,從左到右的順序逐一讀取命令。

選擇執行,根據一些條件的真僞情況分別執行相應的操作。

循環執行,根據限制條件及循環體,反覆執行,直到不符合循環條件退出循環爲止。


本文介紹較爲簡單的選擇執行語句


選擇執行語句

選擇執行語句大致分爲if語句case語句

………………………………………………………………………………………………………………………

if語句

if語句又分爲if單分支語句if雙分支語句if多分支語句

case可以認爲是if多分支語句的一個特例,因爲其格式更加精簡所以在遇到這種特例會使用case。

………………………………………………………………………………………………………………………

if單分支語句

單分支:

if CONDITION; then

    if-true

fi


CONDITION:可以是測試條件,可以是命令執行結果,如果CONDITION內容爲真,則進入then階段,之後進行相應操作,這個執行操作可以是命令,也可以是其他執行語句。也就是這裏可以進行語句的嵌套

該操作結束後,使用fi進行if語句的結尾動作

………………………………………………………………………………………………………………………

下面舉個簡單例子:寫一個腳本,判斷1與2的大小,1<2則顯示:2 is bigger 

[root@localhost test]# cat >> if11 << EOF
> #!/bin/bash
> # This script is a value test for 1 and 2
> #2016-0828 author chawan
> #
> if [ 1 -lt 2 ];then
>     echo "2 is bigger"
> fi
> EOF
[root@localhost test]# chmod +x if11
[root@localhost test]# ./if11 
2 is bigger

………………………………………………………………………………………………………………………

if雙分支語句

雙分支:

   if CONDITION; then

    if-true

   else

    if-false

   fi

雙分支語句跟單分支語句不同的地方在於else後面接的是CONDITION爲假時的情況

同理else後面可以跟命令也可以跟語句嵌套。最後以fi結束if語句

………………………………………………………………………………………………………………………

示例:比較兩個數的大小,如果第一個數大於等於第二個數則顯示:First number is bigger,否則顯示:Second number is bigger。

[root@localhost test]# cat if12
#!/bin/bash
# Test two number who is bigger
#2016-0828 author chawan
#
[ $# -ne 2 ] && echo "Please give two number !" && exit 1
if [ $1 -ge $2 ];then
    echo "First number $1 is bigger"
else
    echo "Second number $2 is bigger"
fi
[root@localhost test]# chmod +x if12
[root@localhost test]# ./if12
Please give two number !
[root@localhost test]# ./if12 3 9
Second number 9 is bigger

………………………………………………………………………………………………………………………

if多分支語句

多分支:

if CONDITION1; then

   if-true

elif CONDITION2; then

   if-ture 

elif CONDITION3; then

   if-ture 

...

esle

    all-false

fi

多分支語句跟雙分支並沒有什麼大的區別,只是通過elif多了幾個選擇判斷。只要理解了if雙分支的使用,那麼if的多分支就不成問題。

………………………………………………………………………………………………………………………

示例:輸入一個文件的絕對路徑判斷該文件的類型。

#!/bin/bash
#version 1.0
#auther chawan 
#date:20160905
#根據提示信息輸入相應內容
read -p "Please give a path of file :" Path
#判斷輸入內容是否爲空,爲空則提示
test -z $Path && echo "No path" && exit 1
#判斷輸入的路徑對應的文件是否存在,若不存在則提示路徑錯誤並退出
! test -e $Path && echo "Wrong path " && exit 2
#多分支選擇語句,判斷是否爲普通文件,目錄文件及鏈接文件,其他類型表示爲不識別。
if [ -f $Path ];then
    echo "$Path is common file" 
elif [ -d $Path ];then
    echo "$Path is diretory file"
elif [ -h $Path ];then
    echo "$Path is link file"
else 
    echo "Unknown file type"
fi

下面輸入幾個文件路徑進行測試

[root@centos7 test]# sh if_3 
Please give a path of file :/
/ is diretory file
[root@centos7 test]# sh if_3 
Please give a path of file :/test/t1
/test/t1 is common file
[root@centos7 test]# sh if_3 
Please give a path of file :
No path
[root@centos7 test]# sh if_3 
Please give a path of file :/e
Wrong path

………………………………………………………………………………………………………………………

case語句

case語句:特點可以理解爲特殊的if多分支語句

它在特定情況下使用會簡化腳本。

case語句格式

case 變量引用 in

part1

    分支1

    ;;

part2

    分支2

    ;;

...

*)

    默認分支

    ;;

esac

case中的變量引用內容需要等於in分支中的part#部分,也就是出現等值比較時使用case效果會很好。下面舉一個例子。

………………………………………………………………………………………………………………………

示例:輸入一個文件的絕對路徑判斷該文件的類型。

#!/bin/bash
#version 1.0
#auther chawan
#date:20160905
read -p "Please give a path of file :" Path
test -z $Path && echo "No path" && exit 1
! test -e $Path && echo "Wrong path " && exit 2
#取文件類型符:d,-,l,p,s,b,c
type_file=`ls -l -d $Path | cut -c1`
case $type_file in
-)
  echo "$Path is common file"
  ;;
d)
  echo "$Path is diretory file" 
  ;;
l)
  echo "$Path is link file"
  ;;
b)
  echo "$Path is block file"
  ;;
c)
  echo "$Path is char file"
  ;;
s)
  echo "$Path is socket file"
  ;;
p)
  echo "$Path is pipe file"
  ;;
*)
  echo "Unknown file type"
  ;;
esac

下面輸入幾個文件路徑進行測試

[root@centos7 test]# sh case_1
Please give a path of file :/etc/fstab
/etc/fstab is common file
[root@centos7 test]# sh case_1
Please give a path of file :/dev/sdb
/dev/sdb is block file
[root@centos7 test]# sh case_1
Please give a path of file :/usr
/usr is diretory file
[root@centos7 test]# sh case_1
Please give a path of file :/ee
Wrong path 
[root@centos7 test]# sh case_1
Please give a path of file :
No path




小結

腳本中的一些注意事項:

腳本中不識別命令別名以alias設定的別名在腳本中都不識別


選擇語句總結:

if選擇:if單分支,if雙分支,if多分支

case選擇:特殊的if多分支,在等值比較情況下使用效果較好,簡化代碼量


選擇語句的作用:

在腳本執行時按觸發的條件進入相應的語句執行。


什麼時候使用選擇語句?

通常就是需要對一些問題分情況討論時用,這個需要在寫的過程中細細體會。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章