shell中測試命令
test命令提供了if–than語句中測試不同條件的途徑。如果test命令中列出的條件成立,test命令就會退出并返回退出狀態嗎0 。這樣if–than語句就與其他編程語言中的if–than語句類似的方式工作了。如果條件不成立,test命令就會退出并返回非零的退出狀態碼,使得if–than語句不會被執行。
1 test 命令的基本格式
test condition
condition是test命令要測試的一系列參數和值。
2 用在if-than語句中,格式是這樣的:
if test condition
then
commands
fi
如果不寫condition部分,它會以非零的退出狀態碼退出。then 語句塊不會被執行,并執行else語句塊。
cat test6.sh
#!/bin/bash
#
if test
then
echo
“No expressing returns a Ture”
else
echo
“No expressing returens a False”
fi
[root@local data]# bash test6.sh
“No expressing returens
a False”
3 在bash shell中的另一種常用的測試條件
if [ condition ]
then
commands
fi
方括號定義了測試條件。
test命令可以判斷三類條件:
(1)數值比較
(2)字符串比較
(3)文件比較
注意:方括號之后和第二個方括號之前必須加上一個空格,否則會報錯。
4 數值比較
下表列出了測試兩個值時可用的條件參數
比較 |
描述 |
n1 –eq n2 |
檢查n1是否等于n2 |
n1 –ge n2 |
檢查n1是否大于或等于n2 |
n1 –gt n2 |
檢查n1是否大于n2 |
n1 –le n2 |
檢查是否小于等于n2 |
n1 –lt n2 |
檢查n1是否小于n2 |
n1 –ne n2 |
檢查n1是否不等于n2 |
注意:測試命令中只能處理整數
[root@local data]# cat number_test.sh
#!/bin/bash
value1=10 #定義變量value1、value2
value2=11
if [ $value1 -gt 5 ] #測試value1的值是否大于5
then
echo
“The test value $value1 is greater than 5”
fi
if [ $value1 -eq $value2 ] #測試value1的值是否和變量value2的值相等。
then
echo
“The values are equal”
else
echo
“The values are different”
fi
[root@local
data]# bash number_test.sh
The test
value 10 is greater than 5
The values
are different
5 字符串比較
條件測試運行字符串比較,下表列出了字符串附加測試
比較 |
描述 |
str1 = str2 |
檢查str1是否和str2 相同 |
str1 != str2 |
檢查str1是否和str2不同 |
str1 < str2 |
檢查str1 是否比str2小 |
str1 > str2 |
檢查str1 是否比str2大 |
–n str1 |
檢查str1的長度是否非0 |
–z str1 |
檢查str1的長度是否為0 |
要測試一個字符串是否比另一個字符串大是一個麻煩的問題,主要有一下兩個問題:
(1)大于號和小于號必須轉義,否則SHELL會把它們當做重定向符號,把字符串值當作文件名;
(2)大于和小于順序和sort命令所采用的不同。
在字符串比較測試中,大寫字母被認為是大于小寫字母的;比較測試中使用的是標準的ASCII順序,根據每個字符的ASCII數值來決定排序結果。
[root@local data]# cat str_comparison.sh
#!/bin/bash
var1=baseball
var2=hockey
if [ $var1 \> $var2 ]
then
echo
“$var1 is greater than $var2”
else
echo
“$var1 is lees than $var2”
fi
[root@local
data]# bash str_comparison.sh
baseball
is lees than hockey
6 文件比較
測試Linux上文件系統上文件和目錄的狀態。
比較 |
描述 |
–d file |
檢查file 是否存在并是一個目錄 |
–e file |
檢查file是否存在 |
–f file |
檢查file是否存在并是一個文件 |
–r file |
檢查file是否存在并可讀 |
–s file |
檢查file是否存在并非空 |
–w file |
檢查file是否存在并可寫 |
–x file |
檢查file是否存在并可執行 |
–O file |
檢查file是否并屬當前用戶所有 |
–G file |
檢查file 是否存在且默認組與當前用戶相同 |
file1 –nt file2 |
檢查file1是否比file2新 |
file1 –ot file2 |
檢查file1是否比file2舊 |
[root@local data]# cat testfile_exist.sh
#!/bin/bash
item_name=$HOME
echo
echo “The item being checked:$item_name”
echo
if [ -e $item_name ]
then #item_name does exist
echo
“The item,$item_name,does exist”
echo
“But is it a file?”
echo
if [ -f $item_name ]
then #item_name is a file
echo
“Yes,$item_name is a file”
else #item_name is not a file
echo
“No,$item_name is not a file”
fi
else #item_name does not exist
echo
“The item,$item_name, does not exist”
echo
“Nothing to update”
fi
[root@local
data]# bash testfile_exist.sh
The item
being checked:/root
The item,/root,does
exist
But is
it a file?
No,/root
is not a file
7 復合條件
if–than語句中可以使用布爾邏輯來組合測試
(1) [ condition1 ] &&
[ condition2 ]
(2) [ conditon1 ] || [ conditon2 ]
第一種布爾運算使用AND布爾運算符來組合兩個條件。要讓then 部分的命令執行,兩個條件必須都滿足。
第二種布爾運算使用OR布爾運算符來組合兩個條件。如果任意條件為TRUE,then部分的命令就會執行。
[root@local data]# cat compund_comparison.sh
#!/bin/bash
#
if [ -d $HOME ] && [ -w $HOME/testing
]
then
echo
“The file exists and you can write to it”
else
echo
“I can not write to the file”
fi
[root@local data]# bash compund_comparison.sh
I can
not write to the file
第一個比較會檢查用戶的$HOME目錄是否存在。第二個比較會檢查在用戶的$HOME目錄下是否有個叫做testing 的文件,以及用戶是否有該文件的寫入權限。如果兩個比較中有一個失敗了,if語句就會失敗,shell就會執行else的部分。如果兩個條件都通過了,則if語句通過,shell會執行then部分的命令。
原創文章,作者:linux is not unix,如若轉載,請注明出處:http://www.www58058.com/73459