分类: LINUX
2018-03-15 23:28:29
1.1、整数比较
-eq 等于 如:if [ “$a” -eq “$b” ]
-ne 不等于 如:if [ “$a” -ne “$b” ]
-gt 大于 如:if [ “$a” -gt “$b” ]
-ge 大于或等于 如:if [ “$a” -ge “$b” ]
-lt 小于或等于 如:if [ “$a” -lt “$b”]
实例:
#!/bin/bash
a=8
b=10
##
if [ "${a}" -eq "${b}" ];then
printf "$a is equal $b\n"
else
printf "$a is not equal $b \n"
fi
##
if [ "${a}" -gt "${b}" ];then
printf "$a is great than $b\n"
else
printf "$a is not great than $b\n"
fi
执行结果:
tay@tay:/mnt/hgfs/hzs/shell$ ./if.sh
8 is not equal 10
8 is not great than 10
1.2、字符串的比较
= 等于 如:if [ “$a” = “$b” ]
== 等于 如:if [ “$a” == “$b” ],与=等价
例子:
#!/bin/bash
_STRING_A_="this is string a"
_STRING_B_="this is string b"
if [ "${_STRING_A_}" = "${_STRING_B_}" ];then
printf "_STRING_A_ is equal _STRING_B_\n"
else
printf "_STRING_A_ is not equal _STRING_B_\n"
fi
if [ "${_STRING_A_}" == "${_STRING_B_}" ];then
printf "_STRING_A_ is equal _STRING_B_\n"
else
printf "_STRING_A_ is not equal _STRING_B_\n"
fi
执行结果:
tay@tay:/mnt/hgfs/hzs/shell$ ./string_compare.sh
_STRING_A_ is not equal _STRING_B_
_STRING_A_ is not equal _STRING_B_
关键点:括号两边各有一个空格,这是unix shelll的要求