7.1. 介紹if
7.1.1. 概要
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi
FILE
參數是 /dev/fd/N
這樣的形式,那麼就檢查文件描述符
“N”。stdin
, stdout
和stderr
和他們各自的文件描述符也可以用於測試。7.1.1.1. 和if使用的表達式
Primary | 意義 |
---|---|
[ -a FILE ] |
如果 FILE 存在則爲真。 |
[ -b FILE ] |
如果 FILE 存在且是一個塊特殊文件則爲真。 |
[ -c FILE ] |
如果 FILE 存在且是一個字特殊文件則爲真。 |
[ -d FILE ] |
如果 FILE 存在且是一個目錄則爲真。 |
[ -e FILE ] |
如果 FILE 存在則爲真。 |
[ -f FILE ] |
如果 FILE 存在且是一個普通文件則爲真。 |
[ -g FILE ] |
如果 FILE 存在且已經設置了SGID則爲真。 |
[ -h FILE ] |
如果 FILE 存在且是一個符號連接則爲真。 |
[ -k FILE ] |
如果 FILE 存在且已經設置了粘制位則爲真。 |
[ -p FILE ] |
如果 FILE 存在且是一個名字管道(F如果O)則爲真。 |
[ -r FILE ] |
如果 FILE 存在且是可讀的則爲真。 |
[ -s FILE ] |
如果 FILE 存在且大小不爲0則爲真。 |
[ -t FD ] |
如果文件描述符 FD 打開且指向一個終端則爲真。 |
[ -u FILE ] |
如果 FILE 存在且設置了SUID (set user ID)則爲真。 |
[ -w FILE ] |
如果 FILE 如果 FILE 存在且是可寫的則爲真。 |
[ -x FILE ] |
如果 FILE 存在且是可執行的則爲真。 |
[ -O FILE ] |
如果 FILE 存在且屬有效用戶ID則爲真。 |
[ -G FILE ] |
如果 FILE 存在且屬有效用戶組則爲真。 |
[ -L FILE ] |
如果 FILE 存在且是一個符號連接則爲真。 |
[ -N FILE ] |
如果 FILE 存在 and has been mod如果ied since it was last read則爲真。 |
[ -S FILE ] |
如果 FILE 存在且是一個套接字則爲真。 |
[ FILE1 -nt FILE2 ] |
如果 FILE1 has been changed more recently thanFILE2 ,
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. |
操作 | 效果 |
---|---|
[ ! EXPR ] | 如果 EXPR 是false則爲真。 |
[ ( EXPR ) ] | 返回 EXPR 的值。這 樣可以用來忽略正常的操作符優先級。 |
[ EXPR1 -a EXPR2 ] | 如果 EXPR1 and EXPR2 全真則爲真。 |
[ EXPR1 -o EXPR2 ] | 如果 EXPR1 或者 EXPR2 爲真則爲真。 |
7.1.1.2. 後接then語句的命令
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選項
# 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
環境 | |
---|---|
以上的例子將在命令行輸入後開始工作:
然而,如果你使用依賴環境的測試,當你在腳本中輸入相同的命令你可能得到不用的結果, 因爲腳本會打開一個新的,沒有設置預期的變量和選項的shell。
|
7.1.2. if的簡單應用
7.1.2.1. 測試退出狀態
?
變量包含了之前執行命令的退出狀態(最近完成的前臺進程)。anny ~>
if [ $? -eq 0 ]
More input>
then echo 'That was a good job!'
More input>
fi
That was a good job!
anny ~>
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 $?
0
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 >
#!/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" your@your_domain.org
7.1.2.3. 字符串比較
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi
[ "$(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 >
真正的程序員 | |
---|---|
多數程序員更喜歡使用和方括號相同作用的內建的 test 命令,像這樣:
test "$(whoami)" != 'root' && (echo you are using a non-privileged account; exit 1)
|
-c 當file存在並且是字符文件時返回真
-d 當pathname存在並且是一個目錄時返回真
-e 當pathname指定的文件或目錄存在時返回真
-f 當file存在並且是正規文件時返回真
-g 當由pathname指定的文件或目錄存在並且設置了SGID位時返回爲真
-h 當file存在並且是符號鏈接文件時返回真,該選項在一些老系統上無效
-k 當由pathname指定的文件或目錄存在並且設置了“粘滯”位時返回真
-p 當file存在並且是命令管道時返回爲真
-r 當由pathname指定的文件或目錄存在並且可讀時返回爲真
-s 當file存在文件大小大於0時返回真
-u 當由pathname指定的文件或目錄存在並且設置了SUID位時返回真
-w 當由pathname指定的文件或目錄存在並且可執行時返回真。一個目錄爲了它的內容被訪問必然是可執行的。
-o 當由pathname指定的文件或目錄存在並且被子當前進程的有效用戶ID所指定的用戶擁有時返回真。
比較字符寫法:
-eq 等於
-ne 不等於
-gt 大於
-lt 小於
-le 小於等於
-ge 大於等於
-z 空串
* = 兩個字符相等
* != 兩個字符不等
* -n 非空串