Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

We tested our script and it fails on negative floating points comparison in bash. What are we doing wrong ?

$F1=10000000000
$F2=300
FLP=$(bc <<< "scale=5; ($F2 - $F1)/$F2 * 100")
if [ $FLP -eq "1" ]; then
    echo " greater"
else
    echo "smaller"
fi

It keeps printing "greater" with -99.99900

Thanks all for help

share|improve this question
add comment

1 Answer

#!/bin/bash

F1=$1
F2=$2
FLP=$(bc <<< "scale=5; (${F2} - ${F1})/${F2} * 100")

if (( $(echo "${FLP} 1" | awk '{print ($1 > $2)}') )); then
  echo "greater"
else
  echo "smaller"
 fi

Adopted from: http://stackoverflow.com/questions/11237794/how-to-compare-two-decimal-numbers-in-bash-awk

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.