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
  • 55,929
  • 26
  • 146
  • 227
user105264
  • 49
  • 1
  • 5

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,183
  • 18
  • 69
  • 104
  • 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?](http://stackoverflow.com/help/someone-answers). – John1024 Mar 31 '15 at 07:15