Friday, 6 September 2013

Comparing numbers in bash

Comparing numbers in bash

I'm starting to learn about writing scripts for the bash terminal, but I
can't work out how to get the comparisons to work properly. I'm sure this
is very basic to many of you I just can't seem to find the answer to my
question anywhere (or at least I'm not totally sure what to search for).
The script I'm using is:
echo "enter two numbers";
read a b;
echo "a=$a";
echo "b=$b";
if [ $a \> $b ];
then
echo "a is greater than b";
else
echo "b is greater than a";
fi;
The problem I have is that it compares the number from the first digit on,
i.e. 9 is bigger than 10000, but 1 is greater than 09. How can I convert
the numbers into a type to do a true comparison? I realise that this is
probably irritatingly simple, but any help would be greatly appreciated!

No comments:

Post a Comment