bash [: -eq: 预期的一元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28239469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:17:49  来源:igfitidea点击:

[: -eq: unary operator expected

bashaix

提问by user2711819

#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

if [ ${TABLE_ID} -eq "" ]; then
        echo hello
fi

above throws error:

以上抛出错误:

[: -eq: unary operator expected

[: -eq: 预期的一元运算符

How to fix this with out double square brackets [[ ${TABLE_ID} -eq "" ]].

如何在没有双方括号的情况下解决此问题 [[ ${TABLE_ID} -eq "" ]]

采纳答案by Mars

Test string equality with =.

测试字符串是否与=.

#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

if [ "${TABLE_ID}" = "" ]; then
    echo hello
fi

回答by fedorqui 'SO stop harming'

You can use -zto test if a variable is empty:

您可以使用-z来测试变量是否为空:

if [ -z "$variable" ]; then
   ...
fi

From man test:

来自man test

-z STRING
        the length of STRING is zero

See an example:

看一个例子:

$ r="roar"
$ [ -z "$r" ] && echo "empty" || echo "not empty"
not empty
$ r=""
$ [ -z "$r" ] && echo "empty" || echo "not empty"
empty

回答by Aliaksei Piatrouski

#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

[ -z ${TABLE_ID} ] && echo hello