0

I have a scenario where I am taking difference of two numbers.

Then the calculated difference needs to be checked within a range.
If it lie within that range then display status as success, else display failure.

Below is my code

Range = -1 to 1

var1=12.23
var2=13.23

diff=$(echo "($var1-$var2)"| bc -l)
echo $diff

if [ "$diff" -ge -1 ] && [ "$diff" -le 1 ]; then
        echo "$diff" "success"
else
   echo "Not within range -1 to 1"
   echo "Failure"
fi

Error getting like below

script.sh: 8: [: Illegal number: -1.00
5
  • 1
    The shell only does integer arithmetics and tests against integers. See e.g. How to do integer & float calculations, in bash or other languages/frameworks? You may want to call bc or some other tool to calculate the value of ($var1 - $var2) > 1 (etc.) instead and use the boolean value of that in your script. Commented Feb 26, 2020 at 8:13
  • @Kusalananda can you write code for me which work | how to resolve this error | i have ubuntu Commented Feb 26, 2020 at 8:17
  • @Kusalananda the calculated difference i need to check within range of -1 to 1. If the difference falls under this range i need success status as 1 or display as 0 Commented Feb 26, 2020 at 8:19
  • @Kusalananda 1 = success and 0 = Fail Commented Feb 26, 2020 at 8:20
  • There is an error in line one. Only post code that you have run. Commented Feb 26, 2020 at 10:17

2 Answers 2

1

If you want to play with awk you can use script like:

var1=12.23
var2=13.23
echo |awk -v v1=$var1 -v v2=$var2 '{diff=v1-v2;if(diff >= -1 && diff <=1) print diff " success" ;else printf "%s not within range [-1,1]\nFailure\n", diff }'
3
  • code not working Commented Feb 26, 2020 at 9:20
  • @techvikky, now should work Commented Feb 26, 2020 at 9:22
  • 1
    code working as expected Commented Feb 26, 2020 at 10:44
1

The shell can't compare strings that represent floating point values in arithmetic contexts. You can however use bc to evaluate a boolean (integer) value that you can use in the shell:

#!/bin/bash

val1=12.23
val2=13.23

difference=$( bc -l <<<"$val1 - $val2" )
range_ok=$( bc -l <<<"$difference >= -1 && $difference <= 1" )

if [ "$range_ok" -ne 0 ]; then
        printf '%s success\n' "$difference"
else
        printf '%s not within range [-1,1]\n' "$difference"
        echo "Failure"
fi
5
  • let me check the code does it full my requirement or not Commented Feb 26, 2020 at 8:43
  • this test case fails : if var1=12 and var2=13 result = -1 ..even if it gives equal to -1 the script should display success and vice versa Commented Feb 26, 2020 at 9:05
  • range_ok=$( bc -l <<<"$difference > -1" && "$difference < 1" && "$difference == 1" && "$difference == -1" ) Commented Feb 26, 2020 at 9:15
  • @techvikky The code I posted does that correctly already. Note that I'm using >= and <=. Commented Feb 26, 2020 at 9:24
  • code is working as expected Commented Feb 26, 2020 at 10:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.