(1) 關(guān)于某個(gè)檔名的文件類型判斷,如 test -e filename 表示存在
-e 該『檔名』是否存在?(常用)
-f 該『檔名』是否存在且為檔案(file)?(常用)
-d 該『文件名』是否存在且為目錄(directory)?(常用) -b 該『檔名』是否存在且為一個(gè) block device 裝置?
-c 該『檔名』是否存在且為一個(gè)character device 裝置?
-S 該『檔名』是否存在且為一個(gè) Socket 檔案?
-p 該『檔名』是否存在且為一個(gè) FIFO (pipe) 檔案?
-L 該『檔名』是否存在且為一個(gè)連結(jié)檔?
(2) 關(guān)于檔案的權(quán)限檢測(cè),如 test -r filename 表示可讀否 (但 root 權(quán)限常有例外)
-r 偵測(cè)該檔名是否存在且具有『可讀』的權(quán)限?
-w 偵測(cè)該檔名是否存在且具有『可寫』的權(quán)限?
-x 偵測(cè)該檔名是否存在且具有『可執(zhí)行』的權(quán)限?
-u 偵測(cè)該文件名是否存在且具有『SUID』的屬性?
-g 偵測(cè)該文件名是否存在且具有『SGID』的屬性?
-k 偵測(cè)該文件名是否存在且具有『Sticky bit』的屬性?
-s 偵測(cè)該檔名是否存在且為『非空白檔案』?
(3)兩個(gè)文檔之間的比較 test file1 -nt file2
-nt (newer than)判斷 file1 是否比 file2 新
-ot (older than)判斷 file1 是否比 file2 舊
-ef 判斷 file1 和file2 是否為同一檔案,可用在判斷 hard link 的判定上。
(4) 關(guān)于兩個(gè)整數(shù)之間的判定,例如 test n1 -eq n2
-eq 兩數(shù)值相等 (equal)
-ne 兩數(shù)值不等 (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)判定字符串的數(shù)據(jù)
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)兩狀況同時(shí)成立!例如 test -r file -a -x file,則 file 同時(shí)具有 r 與x 權(quán)限時(shí),才回傳 true。
-o (or)兩狀況任何一個(gè)成立!例如 test -r file -o -x file,則 file 具有 r 或者 x 權(quán)限時(shí),就可回傳 true。
! 反相狀態(tài), 如 test ! -x file ,當(dāng) file 不具有 x 時(shí),回傳 true。
例子:
# 1. 讓使用者輸入檔名,并判斷使用者是否真的有輸入字符串?
echo -e "Please input a filename, I will check the filename's type and
permission. nn"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
# 2. 判斷檔案是否存在?若不存在則顯示訊息結(jié)束腳本
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.利用判斷符號(hào) [ ]
除了我們很喜歡使用的 test之外,其實(shí),我們還可以常用判斷符號(hào)[](就是中括號(hào)啦)來進(jìn)行數(shù)據(jù)的判斷呢!但要注意以下幾點(diǎn):
(1)在中括號(hào) []內(nèi)的每個(gè)組件都需要有空格鍵來分隔;
(2)在中括號(hào)內(nèi)的發(fā)數(shù),最好都以雙引號(hào)括號(hào)起來;
(3)在中括號(hào)內(nèi)的常數(shù),最好都以單或者雙引號(hào)括號(hào)起來。
例子:
#!/bin/bash
PATH=http://www.3lian.com/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中經(jīng)常在測(cè)試中用到的幾個(gè)常數(shù)
(1)執(zhí)行的腳本檔名為 $0這個(gè)變量,第一個(gè)接的參數(shù)就是 $1,依次類推。
(2) $# :代表后接的參數(shù)『個(gè)數(shù)』,$0不算在里面。
(3) $@:代表『 "$1" "$2" "$3" "$4"』之意,每個(gè)變量是獨(dú)立的(用雙引號(hào)括起來)。
(4) $*:代表『 "$1c$2c$3c$4"』,其中 c 為分隔字符,默認(rèn)為空格鍵,所以本例中代表『 "$1 $2 $3 $4"』之意。
例子:
腳本如下:
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"
執(zhí)行結(jié)果:
[root@www scripts]# sh sh07.sh theone haha quot
The script name is ==> sh07.sh <==檔名
Total parameter number is ==> 3 <==果然有三個(gè)參數(shù)
Your whole parameter is ==> 'theone haha quot' <==參數(shù)的全部內(nèi)容
The 1st parameter ==> theone <==第一個(gè)參數(shù)
The 2nd parameter ==> haha <==第二個(gè)參數(shù)
注:還可以用shift進(jìn)行參數(shù)偏移