天天看點

《Linux指令行與shell腳本程式設計大全》第十二章 使用結構化指令

許多程式要就對shell腳本中的指令施加一些邏輯控制流程。

結構化指令允許你改變程式執行的順序。不一定是依次進行的

12.1 使用if-then語句

如下格式:

if command

then

         commands

fi

if語句會允許if後面的那個指令,如果該指令的退出碼的0(代表成功了)位于then部分的指令就會被執行。否則不執行。

例子:

#!/bin/bash

# if then test

if pwd 

echo “pwd success”

# this is error cmd, no run then commands

if thisErrorCmd

echo “Com success”

echo “this end”

then部分可以使用不止一條指令。bash  shell會把這些指令當成一個塊,要麼不執行,要麼全部執行。

例子:

testuser=xcy

if grep $testuser /etc/passwd

echo "this is first command"

echo "this is second command"

echo "i can even put in other commands besides echo:"

ls -a /home/$testuser/.b*

12.2 if –then-else語句

另外一組指令:

else

else 後面也可以接多條指令。

testuser=hshsh

echo "error: this is first command"

echo "error: not find user $testuser"

12.3嵌套if

可以這樣

         if commands

         then

                   commands

         else

         fi

這種嵌套問題在于代碼不易閱讀,很難理清楚邏輯流程。下面介紹另外一種方式:

if command1

         command1s

elif command2

command2s

elif command3

command3s

# if-then-elif  test, can changed testuser testuser1

testuser1=xcy

        echo "step0:i can even put in other commands besides echo:"

        ls -a /home/$testuser/.b*

elif grep $testuser1 /etc/passwd

        echo "step1: i can even put in other commands besides echo:"

        ls -a /home/$testuser1/.b*

        echo "not find user $testuser and $testuser1"

12.4 test指令

test提供了在if-then語句中測試不同條件的的途徑。

如果test指令中列出的條件成立,test指令就就會退出并傳回退出狀态碼0。這樣if then就會順利執行了。

如果不成立,test指令就會退出并傳回非0的退出狀态碼。if-then語句就不會再被執行了

格式如下:

if test condition

當你假如一個條件時,test會測試該條件。

(1)如果不寫condition部分,會以非0的退出狀态碼退出。

還可以這麼寫:

// 注意condition兩邊一定要有空格。

if [ condition ]

test 可以判斷三類條件:

數值比較

字元串比較

檔案比較

12.4.1 數值比較

下面是數值比較指令的表:不能在test指令中使用浮點數

n1 –eq n2  相當于 ==

n1 –ge n2  相當于 >=

n1 –gt n2  相當于 >

n1 –le n2  相當于 <=

n1 –lt n2  相當于 <

n1 –ne n2  相當于 !=

12.4.2 字元串比較

str1 = str2  是否相同

str1 != str2  是否不相同

str1 < str2  str1是否小于str2

str1 > str2  str1是否大于str2

-n  str1  長度是否非0

-z  str1  長度是否為0

1. 字元串相等性

直接用=,  != 就行了

2. 字元串順序:有兩個注意事項

(1)大于号小于号必須轉義,否則會被認為是重定向

(2)大于和小于順序 和 sort采用的不同。

比較測試中大寫字母是小于小寫字母的。sort指令恰好

# test command test

if test

        echo "step1 true"

        echo "step1 false"

num=8

if [ $num -ge 8 ]  # 還可以接 –le  -eq 等一大堆

        echo "step2 num >= 8"

        echo "step2 num not >= 8"

testuser=xcddy

if [ $USER != $testuser ]

# if [ $USER = $testuser ]

        echo "step3, user not $testuser"

        echo "step3, user is $testuser"

str1=Test1  # small

str2=test1  # big

if [ $str1 \> $str2 ]  #  需要轉義

        echo "step4, $str1 > $str2"

        echo "step4, $str1 <= $str2"

str3=

if [ -z $str3 ]

#if [ -n $str3 ]

        echo "step5, $str3 len is 0"

        echo "step5, $str3 len is not  0"

12.4.3 檔案比較

最為強大,也是用的最多的比較形式。允許測試liunx檔案系統上檔案和目錄的狀态

-d file  是否存在并且是目錄

-e file  是否存在

-f file  是否存在并且是檔案

-r file  是否存在并可讀

-s file  是否存在并非空

-w file  是否存在并可寫

-x file  是否存在并可執行

-O file  是否存在并屬目前使用者所有  // 大寫的O

-G file  是否存在并預設組與目前使用者相同

file1 –nt file2  file1是否比file2新

file1 –ot file2  file1是否比file2舊

jump_dir=testDir

if [ -d $jump_dir ]

        echo "$jump_dir is exist, now change in this:"

        cd $jump_dir

        echo -n  "now pwd:"

        pwd

        echo "$jump_dir is not exist, should create Dir"

        mkdir $jump_dir

        echo "change Dir to $jump_dir"

test_file=moduleok

if [ -e $test_file ]

        echo "$test_file is exist"

        mv moduleok running

        sleep 3

        mv running moduleok

        echo "$test_file is not exist, touch this:"

        touch $test_file

echo "check up dir:"

cd ..

echo -n  "now pwd:"

pwd

run_test=test2

if [ -x $run_test ]

        echo "you can run the script: $run_test"

        ./$run_test

        echo "you are unable to execute the script: $run_test"

file1=test1

file2=test5

if [ $file1 -nt $file2 ]

        echo "$file1 is new, $file2 is old"

        echo "$file1 is old, $file2 is new"

12.5 符合條件測試

if-then 允許使用布爾邏輯來組合測試:

(1)[ condition1 ] && [ condition2 ]:使用AND布爾運算符來組合兩個條件

(2)[ condition1 ] || [ condition2 ]:使用OR來組合兩個條件

12.6 if-then的進階特性

提供了兩項可在if-then語句中使用的進階特性:

(1)用于數學表達式的雙括号

(2)用于進階字元串處理功能的雙方括号

12.6.1 使用雙括号

test指令隻能在比較中使用簡單的算數操作。

允許你在比較過程中使用進階數學表達式。提供了更多的數學符号

val++, val--  後增  後減

++val, --val   先增  先減

! 邏輯求反

~ 位求反

**  幂運算

<<  左位移  >>  右位移

& 位布爾和   |  位布爾或

&& 邏輯和  ||  邏輯或

雙括号裡面的大于号不需要轉義

12.6.2 使用雙方括号

注意不是所有的shell都支援雙方括号。

在模式比對中可以定義一個正規表達式來比對字元串

[[ expression ]]

expression使用了test指令中采用的标準字元串比較,但它提供了test指令未提供的另一個特性 – 模式比對

# (( )) test

val=10

#val=5

if (( val ** 2 > 90 ))

        echo "$val ^ 2 > 90"

        (( val2 = $val ** 2 ))

        echo "Result: $val ^ 2 is $val2"

        echo "$val ^ 2 < 90"

str1=xcyhaha

if [[ $str1 == xcy* ]]

        echo "str1 == xcy* , str1 is $str1"

        echo "str1 != xcy* , str1 is $str1"

12.7 case指令

有了case指令就不需要再寫出所有的elif語句來不停的檢查同一個變量的值了。

case指令會采用清單格式來檢查單個變量的多個值。

文法:注意後面的那兩個分号

case variable in

pattern1 | pattern2 ) commands1;;

pattern3) commands2;;

*) default commands;;

esac

case指令會将指定的變量與不同模式進行比較。如果變量和模式比對,那麼shell會執行為改模式指定的指令。

可以通過豎線操作符在一行中分割出多個模式模式。

*星号會捕獲所有與已知模式不比對的值。

case指令提供了一個更清晰的方法來為變量每個可能的值指定不同的選項。

var=68

case $var in

3)

        echo "var == 3";;

4)

        echo "var == 4";;

5)

        echo "var == 5";;

7|6)

        echo "var == 6|7";;

*)

        echo "var defaule";;

繼續閱讀