专题(二)比较
1、字符串比较
[ $str1 = $str2 ] 等于
[ $str1 != $str2 ] 不等于
[ -z $str ] 空字符串返回true
[ -n $str ] 或者 [ $str ] 非空字符串返回true
2、数字比较
count=1 if [ $count -gt 0 ];then echo "$count 大于 0" else echo "$count 不大于 0" fi
-eq 相等(equal)
-ne 不等(not equal)
-gt 大于(greater than)
-lt 小于(less than)
-ge 大于等于 (greater than or equal)
-le 小于等于 (less than or equal)
3、-z 和 -n 区别
- -n : 字符串长度不等于 0 为真
- -z : 字符串长度等于 0 为真
1)用法
在 [] 中,使用 -z 或 -n 判断字符串长度时,变量要加 "" 或 []。
echo "[ -z \$a ] && echo yes || echo no" [ -z $a ] && echo yes || echo no echo "[ -n \$a ] && echo yes || echo no" [ -n $a ] && echo yes || echo no echo "[ -z \"\$a\" ] && echo yes || echo no" [ -z "$a" ] && echo yes || echo no echo "[ -n \"\$a\" ] && echo yes || echo no" [ -n "$a" ] && echo yes || echo no echo "[[ -n \$a ]] && echo yes || echo no" [[ -n $a ]] && echo yes || echo no echo "[[ -z \$a ]] && echo yes || echo no" [[ -z $a ]] && echo yes || echo no

https://www.cnblogs.com/wangcp-2014/p/13129873.html

浙公网安备 33010602011771号