Linux學習之道:Linux Shell學習筆記 1.bash把[[ $a -lt $b ]]看作一個單獨的元素,並且返回一個退出碼。退出碼0為真,非零為假例如:a=1b=c[[ $a -lt $b ]]echo $? #0 a小於b為真[[ $b -lt $a ]] echo $? #1 b小於a為假2. ((...))和let...結果也能夠返回一個退出碼。當它們所測試的算術運算式的結果為非0的時候,它們的退出碼將返回非0。退出碼0為真,非零為假例如:let "1<2"echo $? #0(( 0 && 1 )) echo $? #13. if命令可以測試任何命令,不僅僅是括弧中的條件例如,建立指令碼test.sh,#!/bin/bashif grep -q root $1 # 參數提供的檔案中,如果含有root字串,則返回File contains at last on occurence of root# 其中-q用來阻止echo的輸出grep獲得的內容then echo "$1 contains at last on occurence of root"else echo "$1 does not contain"fiexit 0並chmod 777 test.sh,執行: ./test.h /etc/passwd之後,返回File contains at last on occurence of root4.一個if/then結構可以包含多級比較和tests(嵌套)if [ condition - true ]then command 1 command 2 ...else #可選 command 3 command 4 ...fi當if和then在一個條件測試的同一行時,必須用";"來終止if運算式(因為:if和then都是關鍵字)例如:if [ -x "$filename" ] ; then5.elif的用法:elif是else if的縮減形式:if [ condition1 ]then command 1 command 2 command 3elif [ condition 2 ]#same as else ifthen command 4 command 5else default-command(type ... #ls,test,cd 可以查看相應命令的類型 或 在/sbin/和/bin/下的路徑)6.幾種等效命令:test,/usr/bin/test,[],/usr/bin/[]#!/bin/bashechoif test -z "$1" #if /usr/bin/test -z "$1" 等效then echo "input length is 0"else echo "input length is not 0"fiechoif [ -z "$1" ] #if /usr/bin/[ -z "$1" ] 等效then echo "input length is 0"else echo "input length is not 0"fiexit 07.[[]]結構將沒有檔案擴充或單詞分離,但是會發生參數擴充和命令替換8.在if後,也可以不用test/[]例如#!/bin/bashdir=$1if cd "$dir" 2>/dev/null#2>/dev/null隱藏了出錯提示then echo "Now in $dir"else echo "Can't change to $dir"fi9.test或[]的使用,也不一定要有if例如#!/bin/bashvar1=20var2=22[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"home=/home[ -d $home ] || echo "$home directory does not exist"注意:&&:前一個操作失敗,後一個就不再執行|| : 前一個操作成功,後一個就不再執行10.算數測試的使用(())(())結構計算並測試算數運算式的結果,退出碼與[]相反true返回0,false返回1((0)) #返回1((1)) #返回0((5>4)) #返回0((5>9)) #返回1((5-5)) #返回1((5/4)) #大於1,返回0((1/2)) #小於1,返回1((1/0)) #報錯,返回111.檔案測試操作:返回true,如果:-e 檔案存在-a 檔案存在(已被棄用)-f 被測檔案是一個regular檔案(正常檔案,非目錄或裝置)-s 檔案長度不為0-d 被測對象是目錄-b 被測對象是塊裝置-c 被測對象是字元裝置-p 被測對象是管道-h 被測檔案是符號串連-L 被測檔案是符號串連-S(大寫) 被測檔案是一個socket-t 關聯到一個終端裝置的檔案描述符。用來檢測指令碼的stdin[-t0]或[-t1]是一個終端-r 檔案具有讀許可權,針對運行指令碼的使用者-w 檔案具有寫入權限,針對運行指令碼的使用者-x 檔案具有執行許可權,針對運行指令碼的使用者-u set-user-id(suid)標誌到檔案,即普通使用者可以使用的root許可權檔案,通過chmod +s file實現-k 設定粘貼位-O 運行指令碼的使用者是檔案的所有者-G 檔案的group-id和運行指令碼的使用者相同-N 從檔案最後被閱讀到現在,是否被修改f1 -nt f2 檔案f1是否比f2新f1 -ot f2 檔案f1是否比f2舊f1 -ef f2 檔案f1和f2是否硬串連到同一個檔案12.二元比較操作符,比較變數或比較數字整數比較:-eq 等於 if [ "$a" -eq "$b" ]-ne 不等於 if [ "$a" -ne "$b" ]-gt 大於 if [ "$a" -gt "$b" ]-ge 大於等於 if [ "$a" -ge "$b" ]-lt 小於 if [ "$a" -lt "$b" ]-le 小於等於 if [ "$a" -le "$b" ]< 小於(需要雙括弧) (( "$a" < "$b" ))<= 小於等於(...) (( "$a" <= "$b" ))> 大於(...) (( "$a" > "$b" ))>= 大於等於(...) (( "$a" >= "$b" ))字串比較:= 等於 if [ "$a" = "$b" ]== 與=等價!= 不等於 if [ "$a" = "$b" ]< 小於,在ASCII字母中的順序: if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] #需要對<進行轉義> 大於-z 字串為null,即長度為0-n 字串不為null,即長度不為0注意:使用-z或-n判斷字串變數時,必須要用""把變數引起來。例如:if [ -n $string1 ] #string1未被初始化then echo "String \"string1\" is not null."else echo "String \"string1\" is null"fi#結果顯示string1為非空,錯誤if [ -n "$string1" ] #string1仍然未被初始化then echo "String \"string1\" is not null"else echo "String \"string1\" is null"fi#結果顯示string1為空白,結果正確if [ $string1 ] #string1裸體判斷then echo "String \"string1\" is not null"else echo "String \"string1\" is null"fi#結果正確#但這種用法存在漏洞,比如:string1="1 > 2"if [ $string1 ]then echo "String \"string1\" is not null"else echo "String \"string1\" is null"fi#實際上[]中的內容被擴充為[ "1 > 2" ],所以結果會出錯。而使用[[ $string1 ]],則可以避免錯誤13.混合比較:-a 邏輯與:exp1 -a exp2,如果exp1跟exp2都為true的話,這個運算式將返回trueif [ exp1 -a exp2 ]與[[ condition1 && condition2 ]]功能相同-o 邏輯或:exp1 -o exp2,如果exp1、exp2中,有一個為true的話,那麼運算式返回trueif [ exp1 -o exp2 ]與[[ condition1 || condition2 ]]功能相同14.嵌套的if/then條件test可以使用if/then來進行嵌套的條件test。最終的結果與上邊的使用&&混合比較是相同的if [ condition1 ]then if [ condition2 ] then do-something fifi