3 回答

TA贡献1898条经验 获得超8个赞
(通常)正确的方法
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
${var+x}
var
x
引号离题
${var+x}
"${var+x}"
x
[ -z ]
[ -z "" ]
[ -z "${var+x}" ]
(经常)走错了路
if [ -z "$var" ]; then echo "var is blank"; else echo "var is set to '$var'"; fi
var=''
[ -z "$var" ]

TA贡献1831条经验 获得超9个赞
if [ -n "$1" ]
-z
-n
-z
.
if [ -n "$1" ]; then echo "You supplied the first parameter!"else echo "First parameter not supplied."fi

TA贡献1818条经验 获得超11个赞
下面是如何测试参数是否是未定,或空(“空”)或用值设置:
+--------------------+----------------------+-----------------+-----------------+
| | parameter | parameter | parameter |
| | Set and Not Null | Set But Null | Unset |
+--------------------+----------------------+-----------------+-----------------+
| ${parameter:-word} | substitute parameter | substitute word | substitute word |
| ${parameter-word} | substitute parameter | substitute null | substitute word |
| ${parameter:=word} | substitute parameter | assign word | assign word |
| ${parameter=word} | substitute parameter | substitute null | assign word |
| ${parameter:?word} | substitute parameter | error, exit | error, exit |
| ${parameter?word} | substitute parameter | substitute null | error, exit |
| ${parameter:+word} | substitute word | substitute null | substitute null |
| ${parameter+word} | substitute word | substitute word | substitute null |
+--------------------+----------------------+-----------------+-----------------+
在以“替代”表示的所有情况下,表达式都将替换为所显示的值。在所有用“赋值”显示的情况下,都为参数赋值,这也替换了表达式。
添加回答
举报