JHarder51
JHarder51

Reputation: 11

Shell script error: [: 0: unary operator expected

Using this if statment in 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?

Upvotes: 1

Views: 19493

Answers (2)

Tom Tromey
Tom Tromey

Reputation: 22559

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:

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

Upvotes: 1

Alex Howansky
Alex Howansky

Reputation: 53626

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

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

Upvotes: 5

Related Questions