Shell脚本错误:[:0:一元运算符预期

时间:2015-12-03 16:07:57

标签: linux bash shell

bash中使用此if语句:

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

RESULT=$STATE_WARNING

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

fi

获取错误:

 line 28: [: 0: unary operator expected

没有以我的方式看到错误。有人能帮忙吗?

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

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

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

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

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