2

I am trying to perform a OR operation between two sub AND conditions like as follows.

elif [[ $RBIT >= 7000.00 && $RBIT <= 15000.00 ]] || [[ $TBIT >= 7000.00 && $TBIT <= 15000.00 ]]; then
echo #statements;
exit 0;
fi

But getting it in error.

/script: line 20: syntax error in conditional expression
/script: line 20: syntax error near `7000.00'
/script: line 20: `elif [[ $RBIT >= 7000.00 && $RBIT <= 15000.00 ]] || [[ $TBIT >= 7000.00 && $TBIT <= 15000.00 ]]; then'

Of course, the variables RBIT and TBIT has float values with 2 decimal points.

I am confused if I caused a syntax error here or 'if condition' doesn't work like that way. Kindly help.

codeforester
  • 39,467
  • 16
  • 112
  • 140
vjwilson
  • 754
  • 2
  • 14
  • 30
  • The error message doesn't look vanilla. – iBug Dec 23 '17 at 04:36
  • @iBug can you clarify?. – vjwilson Dec 23 '17 at 04:45
  • `[[ $TBIT >= 7000.00 && $RBIT <= 15000.00 ]]` in error message but `[[ $TBIT >= 7000.00 && $TBIT <= 15000.00 ]]` in code. – iBug Dec 23 '17 at 04:46
  • I think you might want to read some of the answers here: https://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-bash You might also be interested in this, although it's kind of moot for floats anyway: http://mywiki.wooledge.org/BashFAQ/031 – EdmCoff Dec 23 '17 at 04:52
  • @iBug: I corrected it in the script, but still same error. – vjwilson Dec 23 '17 at 04:57

1 Answers1

2

Bash supports only integer math. So, you could rewrite your condition as:

elif ((RBIT >= 7000 && RBIT <= 15000)) || ((TBIT >= 7000 && TBIT <= 15000)); then

While dealing with integers, ((...)) is a better construct to use. And $ is optional for variable expansion inside ((...)) expression.


You may want to see this related posts:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    I noticed too that downvote. @codeforester, your solution perfectly worked for me. I solved the whole bash script now due to this. – vjwilson Dec 23 '17 at 07:47