Hi,
I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out.
I have tried using below scripts. But its giving me error.
1)
amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }')
if [ "$var" -eq 1 ];then echo "correct" else echo "Wrong" fi
2)Tried using bc also as follows , but no resolution.
amt="12.3" opn_amt="12.2" if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ] then
echo "correct" else echo "Wrong" fi
Thanks
Sibadatta
A number of semicolons were missing, try:
amt="12.3" opn_amt="12.4"; if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ]; then echo "correct"; else echo "Wrong" ; fi
You can avoid this and improve readability by using more lines:
amt="12.3" opn_amt="12.2"
if [ $(bc <<< "$amt <= $opn_amt") -eq 1 ]; then
echo "correct"
else
echo "Wrong"
fi
Just another way
if [ $(echo $amt - $opn_amt | bc | grep ^-) ]; then
echo "correct"
else
echo "Wrong"
fi
It should be noted that all of these examples are using the external bc utility, because BASH itself -- and most bourne shells except ksh -- do not support floating-point numbers.