存在 and has been mod如果ied since it was last read則為真。
[
-S
FILE
]
如果
FILE
存在且是一個套接字則為真。
[
FILE1
-nt
FILE2
]
如果
FILE1
has been changed more recently than
FILE2
, or 如果
FILE1
FILE2
does not則為真。 exists and
[
FILE1
-ot
FILE2
]
如果
FILE1
比
FILE2
要老, 或者
FILE2
存在且
FILE1
不存在則為真。
[
FILE1
-ef
FILE2
]
如果
FILE1
和
FILE2
指向相同的裝置和節點号則為真。
[
-o
OPTIONNAME ]
如果 shell選項 “OPTIONNAME” 開啟則為真。
[ -z
STRING ]
“STRING” 的長度為零則為真。
[ -n
STRING ] or [ STRING ]
“STRING” 的長度為非零 non-zero則為真。
[ STRING1 == STRING2 ]
如果2個字元串相同。 “=” may be used instead of “==” for strict POSIX compliance則為真。
[ STRING1 != STRING2 ]
如果字元串不相等則為真。
[ STRING1 < STRING2 ]
如果 “STRING1” sorts before “STRING2” lexicographically in the current locale則為真。
[ STRING1 > STRING2 ]
如果 “STRING1” sorts after “STRING2” lexicographically in the current locale則為真。
[ ARG1 OP ARG2 ]
“OP” is one of
-eq
,
-ne
,
-lt
,
-le
,
-gt
or
-ge
. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.
表達式可以借以下操作符組合起來,以降序列出:listed in decreasing order of precedence: 表 7.2. 組合表達式
操作
效果
[ ! EXPR ]
如果 EXPR 是false則為真。
[ ( EXPR ) ]
傳回 EXPR 的 值。這樣可以用來忽略正常的操作符優先級。
[ EXPR1 -a EXPR2 ]
如果 EXPR1 and EXPR2 全真則為真。
[ EXPR1 -o EXPR2 ]
如果 EXPR1 或者 EXPR2 為真則為真。
[ (或作 test ) 内建指令對條件表達式使用一系列基于參數數量的規則來求值。更多關于這個主題的資訊可以在Bash文檔中查找。就像if 使用fi 來結束一樣,在條件列完之後必須用">"來結束。
7.1.1.2. 後接then語句的指令
CONSEQUENT-COMMANDS 列出了跟在 then 語句後面可以是任何有效的UNIX指令,任何可執行的程式,任何可執行的shell腳本或者任何shell語句,除了 fi . 。重要地記住 then 和 fi 在shell裡面被認為是分開的語句。是以,在指令行上使用的時候,他們用分号隔開。 在腳本中,if語句的不同部分通常是良好分隔的。以下是一些簡單的例子:
7.1.1.3. 檢查檔案
第一個例子檢查一個檔案是否存在:
anny ~>
cat msgcheck.sh
#!/bin/bash
echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
echo
echo "...done."
anny ~>
./msgcheck.sh
This scripts checks the existence of the messages file.
Checking...
/var/log/messages exists.
...done.
7.1.1.4. 檢查shell選項
加入到你的Bash配置檔案中去:
# These lines will print a message if the noclobber option is set:
if [ -o noclobber ]
then
echo "Your files are protected against accidental overwriting using redirection."
fi
linux shell 條件語句 shell 嵌套條件語句時,盡量把靠近兩次判斷 收藏
環境
以上的例子将在指令行輸入後開始工作:
anny ~>
if [ -o noclobber ]
; then echo ; echo "your files are protected
against overwriting."
; echo ; fi
your files are protected against overwriting.
anny ~>
anny ~>
if [ $? -eq 0 ] More input>
then echo 'That was a good job!' More input>
fi
That was a good job!
anny ~>
以下的例子證明了 TEST-COMMANDS 可以是任何有傳回和退出狀态的UNIX指令,之後 if 再次傳回零的退出狀态:
anny ~>
if ! grep $USER /etc/passwd More input>
then echo "your user account is not managed locally"
; fi
your user account is not managed locally
anny >
echo $? anny >
以下能得到同樣的結果:
anny >
grep $USER /etc/passwd anny >
if [ $? -ne 0 ]
; then echo "not a local account"
; fi
not a local account
anny >
7.1.2.2. 數字的比較
以下的例子是用了數值的比較:
anny > num
= `wc -l work.txt` anny >
echo $num
201
anny >
if [ "$num" -gt "150" ] More input>
then echo ; echo "you've worked hard enough for today." More input>
echo ; fi
you've worked hard enough for today.
anny >
這個腳本在每個星期天由cron來執行。如果星期的數是偶數,他就提醒你把垃 圾箱清理:
#!/bin/bash
# Calculate the week number using the date command:
WEEKOFFSET=$[ $(date +"%V") % 2 ]
# Test if we have a remainder. If not, this is an even week so send a message.
# Else, do nothing.
if [ $WEEKOFFSET -eq "0" ]; then
echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" [email protected]_domain.org
7.1.2.3. 字元串比較
一個通過比較字元串來測試使用者ID的例子:
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi
使用Bash,你可以縮短這樣的結構。下面是以上測試的精簡結構:
[ "$(whoami)" != 'root' ] && ( echo you are using a non-privileged account; exit 1 )
anny > gender
= "female" anny >
if [[ "$gender" == f* ]] More input>
then echo "Pleasure to meet you, Madame."
; fi
Pleasure to meet you, Madame.
anny >