bash Shell 脚本错误:[: 0: 应为一元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34070761/
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 13:59:14  来源:igfitidea点击:

Shell script error: [: 0: unary operator expected

linuxbashshell

提问by JHarder51

Using this if statment in bash:

使用这个 if 语句bash

if [ "$TOTAL_LOAD" >= "2" ];then

RESULT=$STATE_WARNING

msg_text="The system load on $HOST is greater than 200% please investigate {$TOTAL_LOAD}"

fi

Getting the error:

得到错误:

 line 28: [: 0: unary operator expected

Not seeing the error in my ways. Anyone able to help?

没有以我的方式看到错误。任何人都可以提供帮助?

回答by Alex Howansky

Bash uses different operators for string vs arithmetic comparison. You want -geinstead of >=:

Bash 使用不同的运算符进行字符串与算术比较。你想要-ge而不是>=

if [ "$TOTAL_LOAD" -ge "2" ];then

回答by Tom Tromey

If I run this by hand I see the error:

如果我手动运行它,我会看到错误:

pokyo. if [ "0" >= "2" ]; then echo hi; fi
bash: [: 0: unary operator expected

The problem is that ">=" is not a valid operator for test. Instead you must write -ge:

问题是 ">=" 不是 的有效运算符test。相反,你必须写-ge

pokyo. if [ "0" -ge "2" ]; then echo hi; fi