[: -eq: 预期的一元运算符
[: -eq: unary operator expected
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
if [ ${TABLE_ID} -eq "" ]; then
echo hello
fi
以上抛出错误:
[: -eq: unary operator expected
如何使用双方括号解决此问题 [[ ${TABLE_ID} -eq "" ]]
。
用 =
.
测试字符串是否相等
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
if [ "${TABLE_ID}" = "" ]; then
echo hello
fi
您可以使用 -z
来测试变量是否为空:
if [ -z "$variable" ]; then
...
fi
来自man test
:
-z STRING
the length of STRING is zero
看例子:
$ r="roar"
$ [ -z "$r" ] && echo "empty" || echo "not empty"
not empty
$ r=""
$ [ -z "$r" ] && echo "empty" || echo "not empty"
empty
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
[ -z ${TABLE_ID} ] && echo hello
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
if [ ${TABLE_ID} -eq "" ]; then
echo hello
fi
以上抛出错误:
[: -eq: unary operator expected
如何使用双方括号解决此问题 [[ ${TABLE_ID} -eq "" ]]
。
用 =
.
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
if [ "${TABLE_ID}" = "" ]; then
echo hello
fi
您可以使用 -z
来测试变量是否为空:
if [ -z "$variable" ]; then
...
fi
来自man test
:
-z STRING
the length of STRING is zero
看例子:
$ r="roar"
$ [ -z "$r" ] && echo "empty" || echo "not empty"
not empty
$ r=""
$ [ -z "$r" ] && echo "empty" || echo "not empty"
empty
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
[ -z ${TABLE_ID} ] && echo hello