0

I want to compare two variables in bash and find if the variable is exceeding the cut_off value.

For example

cut_off value = 500.00

cut_off=500.00

Now I want to check a variable irrespective of its signage whether it exceeds the cut_off value or not

source_1=-510.00
target_1=1620.00

source_2=490.00
target_2=-488.13

source_3=5490.00
target_3=-4488.13

Now when ever the variables to check are greater than cut_off then echo saying variable value is varying by differenec of cut_off - variable to check`

I have done like below but it is not printing the correct error statement

cutoff=500.0

actual_diff=$(echo "${source}" - "${target}" | bc)

if (( $(echo "$actual_diff > $cutoff" |bc -l) ));
then
    echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
    exit 1
else
    echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi
nmr
  • 605
  • 6
  • 20

1 Answers1

1

Ok, so here is my proposition;

cutoff=500.0

actual_diff=$(echo "${source}" - "${target}" | bc)

if (( $(echo "${actual_diff#-} > ${cutoff#-}" |bc -l) ));

then
    echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
    exit 1
else
    echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi

(thanks to the following post Absolute value of a number)

kalou.net
  • 446
  • 1
  • 4
  • 16