shell中條件測試命令 1.test語句 當我要檢測系統上面某些檔案戒者是相關的屬性時,就得用test指令。 (1) 關於某個檔名的檔案類型判斷,如 test -e filename 表示存在 -e 該『檔名』是否存在?(常用) -f 該『檔名』是否存在且為檔案(file)?(常用) -d 該『檔案名稱』是否存在且為目錄(directory)?(常用) -b 該『檔名』是否存在且為一個 block device 裝置? -c 該『檔名』是否存在且為一個 character device 裝置? -S 該『檔名』是否存在且為一個 Socket 檔案? -p 該『檔名』是否存在且為一個 FIFO (pipe) 檔案? -L 該『檔名』是否存在且為一個連結檔? (2) 關於檔案的許可權檢測,如 test -r filename 表示可讀否 (但 root 許可權常有例外) -r 偵測該檔名是否存在且具有『可讀』的許可權? -w 偵測該檔名是否存在且具有『可寫』的許可權? -x 偵測該檔名是否存在且具有『可執行』的許可權? -u 偵測該檔案名稱是否存在且具有『SUID』的屬性? -g 偵測該檔案名稱是否存在且具有『SGID』的屬性? -k 偵測該檔案名稱是否存在且具有『Sticky bit』的屬性? -s 偵測該檔名是否存在且為『非空白檔案』? (3)兩個文檔之間的比較 test file1 -nt file2 -nt (newer than)判斷 file1 是否比 file2 新 -ot (older than)判斷 file1 是否比 file2 舊 -ef 判斷 file1 和file2 是否為同一檔案,可用在判斷 hard link 的判定上。 (4) 關於兩個整數之間的判定,例如 test n1 -eq n2 -eq 兩數值相等 (equal) -ne 兩數值不等 (not equal) -gt n1 大於 n2 (greater than) -lt n1 小於 n2 (less than) -ge n1 大於等於 n2 (greater than or equal) -le n1 小於等於 n2 (less than or equal) (5)判定字串的資料 test -z string 判定字串是否為 0 ?若 string 為空白字串,則為 true test -n string 判定字串是否非為 0 ?若 string 為空白字串,則為 false。註: -n 亦可省略 test str1 = str2 判定 str1 是否等於 str2 ,若相等,則回傳 true test str1 != str2 判定 str1 是否不等於 str2 ,若相等,則回傳 false (6)多重條件判定,例如: test -r filename -a -x filename -a (and)兩狀況同時成立!例如 test -r file -a -x file,則 file 同時具有 r 與x 許可權時,才回傳 true。 -o (or)兩狀況任何一個成立!例如 test -r file -o -x file,則 file 具有 r 或者 x 許可權時,就可回傳 true。 ! 反相狀態, 如 test ! -x file ,當 file 不具有 x 時,回傳 true。 例子: [html] # 1. 讓使用者輸入檔名,並判斷使用者是否真的有輸入字串? echo -e "Please input a filename, I will check the filename's type and \ permission. \n\n" read -p "Input a filename : " filename test -z $filename && echo "You MUST input a filename." && exit 0 # 2. 判斷檔案是否存在?若不存在則顯示訊息結束指令碼 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 # 3. 開始判斷檔案類型與屬性 test -f $filename && filetype="regulare file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" test -x $filename && perm="$perm executable" # 4. 開始輸出資訊! echo "The filename: $filename is a $filetype" echo "And the permissions are : $perm" 2.利用判斷符號 [ ] 除了我們很喜歡使用的 test之外,其實,我們還可以常用判斷符號[](就是中括弧啦)來進行資料的判斷呢!但要注意以下幾點: (1)在中括弧 []內的每個組件都需要有空格鍵來分隔; (2)在中括弧內的發數,最好都以雙引號括弧起來; (3)在中括弧內的常數,最好都以單或者雙引號括弧起來。 例子: [html] #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input (Y/N): " yn [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0 echo "I don't know what your choice is" && exit 0 3.shell中經常在測試中用到的幾個常數 (1)執行的指令碼檔名為 $0這個變數,第一個接的參數就是 $1,依次類推。 (2) $# :代表後接的參數『個數』,$0不算在裡面。 (3) $@:代表『 "$1" "$2" "$3" "$4"』之意,每個變數是獨立的(用雙引號括起來)。 (4) $*:代表『 "$1c$2c$3c$4"』,其中 c 為分隔字元,預設為空白格鍵,所以本例中代表『 "$1 $2 $3 $4"』之意。 例子: [html] <pre class="html" name="code">指令碼如下: echo "The script name is ==> $0" echo "Total parameter number is ==> $#" [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \ && exit 0 echo "Your whole parameter is ==> '$@'" echo "The 1st parameter ==> $1" echo "The 2nd parameter ==> $2" 執行結果: [root@www scripts]# sh sh07.sh theone haha quot The script name is ==> sh07.sh <==檔名 Total parameter number is ==> 3 <==果然有三個參數 Your whole parameter is ==> 'theone haha quot' <==參數的全部內容 The 1st parameter ==> theone <==第一個參數 The 2nd parameter ==> haha <==第二個參數 </pre><br> <br> <pre></pre> <p> </p> <p>註:還可以用shift進行參數位移</p> <pre></pre> <pre></pre> <pre></pre>