0

I am trying to execute the following code in a bash script to determine whether or not a number is greater than 1 or less than 1.

Bash Code:

weight_check=`bc <<< "(.4+.4+.1+.1+.2)"`
if [[ ${weight_check} -lt "1.0" ]] || [[ ${weight_check} -gt "1.0" ]];
then
    echo "Incorrect Weights!"
    exit 0
fi

Error:

syntax error: invalid arithmetic operator (error token is ".2")

The sum of ${weight_check} equals 1.2 but, the bash script doesn't see this...

The input numbers in ${weight_check} will change so, I need this if/then/fi statement to be able to act dynamically.

Is my if/then/fi statement set up incorrectly?

Aaron Perry
  • 1,021
  • 1
  • 13
  • 33
  • How about an if/then/elif/fi statement. So `if {weight_check} -eq "1.0" then ... elif echo "Incorrect weights!" fi` – liam_g Jan 08 '16 at 17:13
  • @Tom Fenech marked this as a duplicate post but, I just tried the answers that were given in that post and they did not solve my problem... – Aaron Perry Jan 08 '16 at 17:28
  • Additionally, I tried your method @liam_g and it did not work. – Aaron Perry Jan 08 '16 at 17:31
  • 1
    Do the test in `bc`, which will result in 0 or 1. You can test that in bash: `if (( $(bc<<<"$weight_check != 1.0") ));`. Or, with more typing: `if [[ $(bc <<<"$weight_check != 1.0") == 1 ]]; ` – rici Jan 08 '16 at 17:40
  • I am now receiving the following error: `syntax error near unexpected token fi` – Aaron Perry Jan 08 '16 at 17:41

0 Answers0