Errors in if conditions with to many OR conditions

Hi ALL

I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS"

if [ $var1 -gt 1500 -o $var2 -gt 1000 -o $var3 -gt 1000 -o $var4 -gt 1000 -o $var5 -gt 1000 ]
then

msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR , bmResponseVAR=$bmResponseVAR , bmRequestVAR=$bmRequestVAR "
SendSms "$msg"
wget -q --no-proxy "http://10.10.1.8/cgi-bin/provider?host_name=LION_MACHINE&svc_desc=MESSAGE_VAR_BM_LION&return_code=0&plugin_output=$msg"
echo "BM Q message sent" >>$monitorLogPath
echo $msg >>$monitorLogPath
fi

ERROR is as follows

++ '[' 0 -gt 1500 -o -gt 1000 ']'
./QQQ.sh: line 50: [: too many arguments

What about using || ?

use sth like this

 if [[ $var1 -gt 1500 || $var2 -gt 1000 ]]

I have tested as below. It seems you have a problem in var# variable.
Please check v$varX variable if values are assigned to the varX variable.

# cat sam.sh
var1=2000
var2=2000
var3=3000
var4=4000
var5=5000
if [ $var1 -gt 1500 -o $var2 -gt 1000 -o $var3 -gt 1000 -o $var4 -gt 1000 -o $var5 -gt 1000 ]
then
  echo "test"
fi
# ./sam.sh
test

Cheers,

@Pamu : no luck yet
@joseph : I did try the way you said, but still there is an issue.

The part of if below has no issue.

 
if [ $var1 -gt 1500 -o $var2 -gt 1000 -o $var3 -gt 1000 -o $var4 -gt 1000 -o $var5 -gt 1000 ]

Error msg said line 50 so I guess your issue might be in another part of your script.

The script is giving too many arguments error because one or more of your variable value is not set:-

++ '[' 0 -gt 1500 -o ... -gt 1000 ']'
./QQQ.sh: line 50: [: too many arguments

You can put a check before comparison to see if the variable values are set and then proceed to avoid this error.

From the POSIX spec for test (a.k.a. [ ):
"the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent"

Use multiple tests joined by && or || :

if [ $x -gt 2 ] || [ $y -lt 99 ] || [ ... ]
then
   :
fi

Thanks Johnsoe,
Your advice is good to know. :slight_smile:

Note that this problem could also have been fixed by quoting the variables:

if [ "$var1" -gt 1500 -o "$var2" -gt 1000 -o "$var3" -gt 1000 -o "$var4" -gt 1000 -o "$var5" -gt 1000 ]

The unset variables in this case would evaluate as zero when performing the numeric comparisons used in this test command instead of giving a syntax error.

Don Cragun, I see quoting the variables is not helping:-

# cat test.sh
#!/bin/bash

var1=100
var3=1500

if [ "$var1" -gt 1500 -o "$var2" -gt 1000 -o "$var3" -gt 1000 ]
then
    echo "Success"
else
    echo "Failed"
fi
# ./test.sh
./test.sh: line 6: [: : integer expression expected
Failed
# uname
Linux GNU/Linux
# echo $SHELL
/bin/bash

Sorry, I should have said it wasn't a portable fix.

It works with ksh on OS X, but comparing an empty string using a numeric test operator is not required to work. However, I believe that even though it didn't give you what you wanted with bash on Linux, the diagnostic message would be easier to interpret. (At least I think it would be easier to try to find out what to look for when test says it expected an integer expression than it was to figure out what was wrong when it said too many arguments and meant that an unset variable caused a test operator to be misinterpreted as an operand.)

Of course the underlying problem of using variables that don't contain data of the type expected when they are expanded can only be fixed by verifying the format of data before it is used. And, we weren't shown the first 49 lines of the script that failed to set or verify that the variables used in this test contained numeric string values.

1 Like