shell編程基礎(四)——條件判斷

依據某些條件測試結果來決定程序的控制流向和下一步的處理動做。shell

在test語句中,shell通常先執行變量替換或命令替換,而後再執行條件測試。
test命令的語法格式,主要三種:
test expression
[ expression ]
[[ expression ]]
注意:方括號內側的兩邊必須各加一個空格。條件測試返回0表示真,非0表示假。express

1.test expression
[root@mrhcatxq01 shell]# cat test_test.sh
#!/bin/bashbash

fname=/install_cacti/shell/willdel.txt
if test -e ${fname}
then
    rm -f ${fname}
    echo "File ${fname} has removed."
else
    echo "File ${fname} is not exist"
fi
[root@mrhcatxq01 shell]#測試

2.[ expression ]
[root@mrhcatxq01 shell]# cat test_square_brackets.sh
#!/bin/bashrem

fname=/install_cacti/shell/willdel.txt
if [ -e ${fname} ]
then
    rm -f ${fname}
    echo "File ${fname} has removed."
else
    echo "File ${fname} is not exist"
fi
[root@mrhcatxq01 shell]#字符串

3.[[ expression ]]
[root@mrhcatxq01 shell]# cat test_double_square_brackets.sh
#!/bin/bashio

fname=/install_cacti/shell/willdel.txt
if [[ -e ${fname} ]]
then
    rm -f ${fname}
    echo "File ${fname} has removed."
else
    echo "File ${fname} is not exist"
fi
[root@mrhcatxq01 shell]#test

條件測試通常是數值比較、字符串比較和文件屬性檢查。變量

1)數值比較語法

數值比較:
n1 -eq n2        檢查n1是否等於n2 
n1 -ne n2        檢查n1是否不等於n2 
n1 -gt n2         檢查n1是否大於n2 
n1 -ge n2        檢查n1是否大於等於n2 
n1 -lt n2          檢查n1是否小於n2      
n1 -le n2         檢查n1是否小於等於n2
[root@mrhcatxq01 shell]# test 12 -eq 12 ;echo $?
0
[root@mrhcatxq01 shell]# test 12 -eq 1 ;echo $?
1
[root@mrhcatxq01 shell]# test 12 -ne 12;echo $?
1
[root@mrhcatxq01 shell]# test 12 -ne 1;echo $?
0
[root@mrhcatxq01 shell]# [ 12 -gt 1 ];echo $?
0
[root@mrhcatxq01 shell]# [ 12 -gt 12 ];echo $?
1
[root@mrhcatxq01 shell]# [ 12 -lt 1 ];echo $?
1
[root@mrhcatxq01 shell]# [ 12 -lt 12 ];echo $?
1
[root@mrhcatxq01 shell]# [ 12 -ge 1 ];echo $?
0
[root@mrhcatxq01 shell]# [ 12 -ge 12 ];echo $?
0
[root@mrhcatxq01 shell]# [ 12 -le 1 ];echo $?
1
[root@mrhcatxq01 shell]# [ 12 -le 12 ];echo $?
0
[root@mrhcatxq01 shell]#
2)字符串比較

字符串比較:
str1 = str2                        檢查str1與str2是否相同     
str1 != str2                       檢查str1與str2是否不一樣
-z str1                            檢查str1的長度是否爲0    
-n str1                            檢查str1的長度是否大於0
[[ ${option} =~ ^[0-9]+$ ]]        匹配數字
[[ ${option} =~ ^[A-Za-z]+$ ]]     匹配字母


s1 < s2   ASCII碼值字符串s1小於s2,返回真
          test s1 < s2
          [ s1 \< s2 ]  單方括號中,< 須要轉義
          [[ s1 < s2 ]
s1 > s2   ASCII碼值字符串s1大於s2,返回真

[root@mrhcatxq01 shell]# a=hello
[root@mrhcatxq01 shell]# test "${a}" = hello ;echo $?   
0
[root@mrhcatxq01 shell]# test "${a}" = wo ;echo $?
1
[root@mrhcatxq01 shell]# [ "${a}" != hello ];echo $?
1
[root@mrhcatxq01 shell]# [ "${a}" != wo ];echo $?
0
[root@mrhcatxq01 shell]# [ -z "${a}" ];echo $?
1
[root@mrhcatxq01 shell]# [ -n "${a}" ];echo $?
0
[root@mrhcatxq01 shell]#
 #引用的變量必定要加雙引號,不然若變量值爲null時,shell將忽略變量表達式的存在而報語法錯誤
[root@mrhcatxq01 shell]# [ $b = hello ];echo $?
-bash: [: =: unary operator expected
2
[root@mrhcatxq01 shell]# echo $b

[root@mrhcatxq01 shell]# [ "${b}" = hello ];echo $?
1
[root@mrhcatxq01 shell]# [ "X${b}" = "Xhello" ];echo $?
1
[root@mrhcatxq01 shell]#
3)文件判斷(檢查)
-e 判斷文件是否存在
-f 判斷文件是否存在且是普通文件
-s 判斷文件是否存在且非空
-d 判斷文件是否存在且是目錄

4)邏輯判斷 -a   && 且 -o  || 或 ! 非

相關文章
相關標籤/搜索