1. 整数测试举例
范例1:整数条件测试举例
root@test-1 ~]# a1=10;a2=13 [root@test-1 ~]# echo $a1 $a2 10 13 [root@test-1 ~]# [ $a1 -eq $a2 ] && echo 1 || echo 0 #条件不成立,返回为0 0 [root@test-1 ~]# [ $a1 -gt $a2 ] && echo 1 || echo 0 0 [root@test-1 ~]# [ $a1 -lt $a2 ] && echo 1 || echo 0 1 [root@test-1 ~]# [ $a1 -le $a2 ] && echo 1 || echo 0 1 [root@test-1 ~]# [ $a1 -ge $a2 ] && echo 1 || echo 0 0 [root@test-1 ~]# [ $a1 -ne $a2 ] && echo 1 || echo 0 1 [root@test-1 ~]# a=0001 [root@test-1 ~]# b=100 [root@test-1 ~]# [ "$a" -ge "$b" ] && echo 1 || echo 0 0 [root@test-1 ~]# [ "$a" -le "$b" ] && echo 1 || echo 0 1
范例2:直接通过数学运算符比较
[root@test-1 ~]# a1=10;a2=13 [root@test-1 ~]# echo $a1 $a2 10 13 [root@test-1 ~]# [[ "$a1" = "$a2" ]] && echo 1 || echo 0 #功能同上,条件不成立,返回0 0 [root@test-1 ~]# [[ "$a1" == "$a2" ]] && echo 1 || echo 0 #功能同上,条件不成立,返回0 0 [root@test-1 ~]# (( "$a1" > "$a2" )) && echo 1 || echo 0 0 [root@test-1 ~]# [ "$a1" > "$a2" ] && echo 1 || echo 0 #没报错,但结果不对 1 [root@test-1 ~]# [ "$a1" \> "$a2" ] && echo 1 || echo 0 #加转义或用[[ ]] 0 [root@test-1 ~]# [[ "$a1" > "$a2" ]] && echo 1 || echo 0 0 提示:"=" 和 "==" 也可以用于[ ] 中。 [root@test-1 ~]# (( "$a1" < "$a2" )) && echo 1 || echo 0 1 [root@test-1 ~]# (( "$a1" > "$a2" )) && echo 1 || echo 0 0