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  || 或 ! 非

相关文章
相关标签/搜索