shell基础04 结构化命令
? ? ? 幾乎和別的編程語言思想一樣,只是關鍵字寫法稍有不同。總結主要包括如下幾種:if-then,for,while
1. if-then
? ? 格式:
? ? ?if command? ? #根據command的狀態碼是否為0進行判斷,為0才執行then中部分。(判斷狀態碼:echo $? )
? ? ?then?
? ? ? ? ? ? ? commands? ? #可以有多條語句
? ? ?fi
? ? 舉例:? ? ? ??
1 [Hermioner@localhost Documents]$ cat test1.sh 2 #!/bin/bash 3 if pwd 4 then 5 echo "Hello world" 6 fi 7 [Hermioner@localhost Documents]$ ./test1.sh 8 /home/Hermioner/Documents 9 Hello worldView Code
? ? ?缺點:只有if一種選擇。
2. if-then-else
? ? 格式:
? ? if? ? ? command
? ? then
? ? ? ? ? ?commands
? ? else
? ? ? ? ? ?commands? ? ?#狀態碼非0時就可以執行啦
? ? ?fi
? ? ?可以嵌套多個if then 語句在else里面。但是這樣不利于代碼的閱讀。為了解決這個問題,可以直接使用elif代替else命令,這樣就相當于在原來的else下面再次嵌套了if 語句。并且,緊跟elif后面的else語句也屬于elif代碼塊,它并不屬于之前的if then代碼塊。可以嵌套多個elif。其實elif就相當于JAVA中的else if語句。
? ? ?缺點:代碼邏輯不容易閱讀。即使有多種可供的邏輯選擇。有時候可以采用case來代替elif,比如判斷多個變量是否有匹配:
? ?case variable in
? ?pattern1 | pattern2) commands1;;
? ?pattern3) commands2;;
? ?*) default commands;;
? ?esac
1 [Hermioner@localhost Documents]$ cat test1.sh 2 #!/bin/bash 3 count=1 4 if test 2 -gt 12 5 then 6 echo "2 is greater than 12" 7 elif test $count -eq 1 8 then 9 echo "the value of count is 1" 10 elif test 2 -lt 12 11 then 12 echo "2 is lower than 12" 13 fi 14 [Hermioner@localhost Documents]$ bash test1.sh 15 the value of count is 1View Code
?
1 [Hermioner@localhost Documents]$ cat test2.sh 2 #!/bin/bash 3 i=a 4 case $i in 5 a | b) 6 echo "a | b";; 7 c) 8 echo "c";; 9 *) 10 echo "default";; 11 esac 12 [Hermioner@localhost Documents]$ bash test2.sh 13 a | b 14 [Hermioner@localhost Documents]$View Code
總結:前面所有的if語句它們的判斷條件只能是命令狀態碼。
3. test命令
? ? ?為了測試其它條件,我們可以利用工具test來解決。它只支持判斷三類條件:1)數值比較;2)字符串比較;3)文件比較
? ? ?命令格式:
? ? ? if?test condition? ? ? #condition是test要測試的一系列參數和值,如果條件成立,test命令就會退出并返回退出狀態碼0------(類似JAVA中的if判斷啦)
? ? ? note: 如果只寫test,直接返回非0。如果不想使用test關鍵字,可以采用如下格式代替,效果一樣:
? ? ? if [? condition? ]?? 方括號必須要和condition之間有空格隔開。
? ? ? 舉例:
? ? ??
1 [Hermioner@localhost Documents]$ bash test2.sh 2 hello world 3 [Hermioner@localhost Documents]$ cat test2.sh 4 #!/bin/bash 5 a="hello" 6 if test $a 7 then 8 echo "hello world" 9 else 10 echo "wrong" 11 fiView Code
?
1 [Hermioner@localhost Documents]$ cat test2.sh 2 #!/bin/bash 3 a="hello" 4 if [ $a ] 5 then 6 echo "hello world" 7 else 8 echo "wrong" 9 fi 10 [Hermioner@localhost Documents]$ bash test2.sh 11 hello world 12 [Hermioner@localhost Documents]$View Code
(1)數值比較? ?(只能處理整數)
?
? ? ? ? ? ? ? ?比 較? ? ? ? ? ? 含義
?
? ? ? ? ? n1? -eq? n2? ? ? ? ? =
? ? ? ? ? n1? -ge? n2? ? ? ? ? >=
? ? ? ? ? n1? -gt? ?n2? ? ? ? ? >
? ? ? ? ? n1? -le? ?n2? ? ? ? ? <=
? ? ? ? ? n1? -lt? ? n2? ? ? ? ? <
? ? ? ? ? n1? -ne? n2? ? ? ? ? !=
?
1 [Hermioner@localhost Documents]$ test 1 -eq 2 ;echo $? 2 1 3 [Hermioner@localhost Documents]$ test 1 -ge 2;echo $? 4 1 5 [Hermioner@localhost Documents]$ test 1 -gt 2;echo $? 6 1 7 [Hermioner@localhost Documents]$ test 1 -le 2;echo $? 8 0 9 [Hermioner@localhost Documents]$ test 1 -lt 2;echo $? 10 0 11 [Hermioner@localhost Documents]$ test 1 -ne 2;echo $? 12 0View Code
(2)字符串比較? ? ??
?
? ? ? ? ? ?比較? ? ? ? ? ? ? ? ? ? ? ? ? ? ?含義
?
? ? ? ? ?str1 = str2? ? ? ? ?檢查str1和str2是否相同
? ? ? ? ?str1 !=str2? ? ? ? ?檢查str1和str2是否不同
? ? ? ? ?str1 < str2? ? ? ? ?檢查str1是否小于str2
? ? ? ? ?str1 > str2? ? ? ? ?檢查str1是否大于str2
? ? ? ? ?-n str1? ? ? ? ? ? ? ?檢查str1的長度是否非0
? ? ? ? ?-z str1? ? ? ? ? ? ? ?檢查str1的長度是否為0
? ? ? ? note: 操作符兩端必須要有空格
?
1 [Hermioner@localhost Documents]$ str1="a";str2="b" 2 [Hermioner@localhost Documents]$ test $str1 = $str2;echo $? 3 1 4 [Hermioner@localhost Documents]$ test $str1 != $str2;echo $? 5 0 6 [Hermioner@localhost Documents]$ test $str1 \> $str2;echo $? 7 1 8 [Hermioner@localhost Documents]$ test $str1 \< $str2;echo $? 9 0 10 [Hermioner@localhost Documents]$ test -n $str1;echo $? 11 0 12 [Hermioner@localhost Documents]$ test -z $str1;echo $? 13 1View Code
? ? ? ? note:關于大于小于符號,必須用轉義字符,否則會當成重定向符號。并且,在比較測試中,大寫字母被認為是小于小寫字母的,和sort剛好相反。因為比較測試使用的是標準的ASCII順序;而sort命令使用的系統的本地化語言設置中定義的排序順序。
?
(3)文件比較
? ? ? ? 文件測試比較是非常強大的。允許測試Linux文件系統上文件和目錄的狀態。
?
? ? ? ? ? ?比較? ? ? ? ? ? ? ? ? ? ? ? ? ?描述
?
? ? ? ? ? -d file? ? ? ? ? ? ? ? 檢查file是否存在并是一個目錄
? ? ? ? ? -e file? ? ? ? ? ? ? ? 檢查file是否存在
? ? ? ? ? -f file? ? ? ? ? ? ? ? ?檢查file是否存在并是一個文件
? ? ? ? ? -r file? ? ? ? ? ? ? ? ?檢查file是否存在并可讀
? ? ? ? ? -s file? ? ? ? ? ? ? ? 檢查file是否存在并非空
? ? ? ? ?-w file? ? ? ? ? ? ? ? 檢查file是否存在并可寫
? ? ? ? ?-x file? ? ? ? ? ? ? ? ?檢查file是否存在并可以執行
? ? ? ? ?-O file? ? ? ? ? ? ? ? ?檢查file是否存在并屬當前用戶所有
? ? ? ? ?-G file? ? ? ? ? ? ? ? ?檢查file是否存在并且默認組與當前用戶相同
? ? ? ? ?file1 -nt file2? ? ? ?檢查file1是否比file2新
? ? ? ? ?file1 -ot file2? ? ? ?檢查file1是否比file2舊
?
1 [Hermioner@localhost Documents]$ ls 2 a test2.sh 3 [Hermioner@localhost Documents]$ test -d a;echo $? 4 0View Code
?
4. if then可以用的測試條件總結
? ? ?(1)邏輯測試:采用|| 和&&? ?(或? 和? ?與)? ?直接if? [ condition ] && [ condition ]? ,這樣來包含多種邏輯組合condition
? ? ?(2)test中的三種測試? (數值計算和字符串比較的操作符都很簡單,不能包含更加復雜的,比如+=這種符號)
? ? ?(3)使用雙括號來計算更加復雜的數值計算;使用雙方括號來計算更加復雜的字符串比較
?
? ? ? ? ? ? ?符號? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?描述
?
? ? ? ? ? ? val++? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?后增
? ? ? ? ? ? val--? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?后減
? ? ? ? ? ? ++val? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?先增
? ? ? ? ? ? --val? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?先減
? ? ? ? ? ? !? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 邏輯求反
? ? ? ? ? ? ~? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 位求反
? ? ? ? ? ? **? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?冪運算
? ? ? ? ? ? <<? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 左位移
? ? ? ? ? ? >>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 右位移
? ? ? ? ? ? ?&? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?位布爾和
? ? ? ? ? ? |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?位布爾或
? ? ? ? ? ? &&? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 邏輯和
? ? ? ? ? ? ||? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?邏輯或
5. for & while? ?參考我的 shell練習01
6. until ------和while的判斷剛好相反
? ? untile test commands? ? ? #狀態碼不為0時才執行do中代碼
? ? do
? ? ? ? ? ? other commands
? ? done
1 [Hermioner@localhost Documents]$ cat test2.sh 2 #!/bin/bash 3 var1=100 4 until [ $var1 -eq 0 ] 5 do 6 echo $var1 7 var1=$[ $var1-25 ] 8 done 9 [Hermioner@localhost Documents]$ bash test2.sh 10 100 11 75 12 50 13 25View Code
7. break
? ? break命令的作用是退出循環。它可以退出任意類型的循環。可以跟數字,也可以不跟。?
? ? break n? #n代表循環層級,默認為1,代表當前循環。n越大,從內往外。eg:n=2,跳出的是從內往外數的第二個循環。
1 [Hermioner@localhost Documents]$ cat test2.sh 2 #!/bin/bash 3 for (( a=1;a<4;a++ )) 4 do 5 echo "Outer loop:$a" 6 for (( b=1;b<100;b++ )) 7 do 8 if [ $b -gt 4 ] 9 then 10 break 2 11 fi 12 echo " Inner loop:$b" 13 done 14 done 15 [Hermioner@localhost Documents]$ bash test2.sh 16 Outer loop:1 17 Inner loop:1 18 Inner loop:2 19 Inner loop:3 20 Inner loop:4 21 [Hermioner@localhost Documents]$View Code
8. continue?
? ? ?和break一樣,仍然可以使用continue n來指定級別。但是,continue是結束本次循環后面的代碼,又跳到循環的開始位置準備執行下一次循環。
?
總結:shell中的結構化語句和JAVA中類似,只是在條件語句的判斷上稍有差別,會根據狀態碼來進行判斷;并且,對于括號使用以及括號兩邊的空格要求極其嚴格。中括號和小括號使用上也是有差別的。
?
?
參考文獻:
Linux命令行與shell腳本編程大全(第3版)[美]?布魯姆(Richard Blum),布雷斯納漢(Christine Bresnahan) 著,門佳,武海峰?譯
?
轉載于:https://www.cnblogs.com/Hermioner/p/9381514.html
總結
以上是生活随笔為你收集整理的shell基础04 结构化命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ef linq 中判断实体中是否包含某集
- 下一篇: Python day10 global关