字符串比较操作有:相等、不相等、长度为0、为空。
参数 | 说明 |
= | 相等则为真 |
!= | 不相等则为真 |
-z 字符串 | 字符串的长度为零或者没赋值则为真 |
-n 字符串 | 字符串的长度不为零则为真 |
(1)=
[root@172-0-10-222 shell-test]# [ "123" = "123" ] && echo "相等" || echo "不相等"
相等
(2)!=
[root@172-0-10-222 shell-test]# [ "123" != "123" ] && echo "相等" || echo "不相等"
不相等
(3)-z
[root@172-0-10-222 shell-test]# [ -z "" ] && echo "字符串长度为0或者没赋值" || echo "不是空串"
字符串长度为0或者没赋值
[root@172-0-10-222 shell-test]# [ -z $fuckdlf ] && echo "字符串长度为0或者没赋值" || echo "不是空串"
字符串长度为0或者没赋值
[root@172-0-10-222 shell-test]#
(4)-n
[root@172-0-10-222 shell-test]# [ -n "" ] && echo "不是空串" || echo "空串"
空串
[root@172-0-10-222 shell-test]# [ -n "hello" ] && echo "不是空串" || echo "空串"
不是空串
[root@172-0-10-222 shell-test]#