Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float conditional in bash

Tags:

bash

in bash I need to compare two float numbers, one which I define in the script and the other read as paramter, for that I do:

   if [[ $aff -gt 0 ]]
    then
            a=b
            echo "xxx "$aff
            #echo $CX $CY $CZ $aff
    fi

but I get the error:

[[: -309.585300: syntax error: invalid arithmetic operator (error token is ".585300")

What is wrong?

Thanks

like image 482
Open the way Avatar asked Apr 21 '10 13:04

Open the way


1 Answers

Using bc instead of awk:

float1='0.43255'
float2='0.801222'

if [[ $(echo "if (${float1} > ${float2}) 1 else 0" | bc) -eq 1 ]]; then
   echo "${float1} > ${float2}"
else
   echo "${float1} <= ${float2}"
fi
like image 70
yabt Avatar answered Oct 28 '22 11:10

yabt