3

How to compare a decimal value with a integer in shell scripting?

Example:

i=1
j=1.2 
if [$j -gt $i];then
  echo "growth"
else
  echo "None of the condition met"
fi

Im getting output as None of the condition met

But i need output as growth,As 1.2 is greater than 1.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

4

you should use bc (binary calculator).

i=1
j=1.2 
gt=$(echo "$j > $i" | bc -q )
# return 1 if true ; O if not
if [ $gt = 1 ]
then
   echo "growth"
else
   echo "None of the condition met"
fi
Archemar
  • 31,554
  • 1
    @user105264 If Archemar's answer solved your issue, you should consider accepting it by clicking on the check mark to the left. That will mark the question as answered (which keeps the site organized). Also, this and, when your rep is high enough, up-voting are the ways that thanks are expressed here on the Stack Exchange sites. For more information, see What should I do when someone answers my question?. – John1024 Mar 31 '15 at 07:15