Arithmetic comparison in Shell Script

Hello,

I wrote a script to check the version of Firefox and update it if its older the 2.0.0.5.

It seems that -lt, -gt, or -eq does not look at anything after the first decimal. So, if my current version is 2.0.0.4, Firefox does not get updated.

The portion of code that is giving me a problem is below:

if [ $firefox_version -lt 2.0.0.5 ]

then

...

fi

So the question is, is there a way to do comparisons on numbers with decimals?

[519 byte] By [jagreenwooda] at [2007-11-27 11:44:13]
# 1

You can use awk to split the number into separate version, sub version, sub sub version etc. Then do arithmetic on the components.

robert.cohena at 2007-7-29 17:54:27 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...
# 2

You could also sed out the dots, making '2.0.0.5' 2005 and '2.0.0.4' 2004, that would make the expression:

if [ 2004 -lt 2005]; then ...

which would work fine.

This would of course break with a future release of 2.2, since 22 is less than 2005, but you could fix that by adding a check which adds trailing zeros if the string is less than two characters, hence making version 2.2 appear as 2200..

This little while-statement would ensure that $firefox_version is always (at least) four digits long:

while [ `/bin/echo "${firefox_version}\c" |wc -m` -lt 4 ]; do

tmp="${firefox_version}0"

firefox_version=$tmp

done

Hmm.. sorry, i think i just got a bit carried away ;)

.7/M.

mAbrantea at 2007-7-29 17:54:27 > top of Java-index,Solaris Operating System,Solaris Essentials - General Technical Questions...