0

I know this is a dumb error, but I could find the solution yet.
I have to compare the apache version. If the apache version is greater than 2.4.3 I have to instal apr in the system before installing apache.
But for some reason I get an arithmetic error in the comparison.
Basically, I get the apache version and I have to compare it to 2.4.3
This is the test script:

#!/bin/ksh

version="2.4.4"
echo "$version"

#if [ '2.4.3' == "$version" ] || [ '2.4.3' < "$version" ]
if [ '2.4.3' -gt '$version' ]

then
        print "Mayor or equal"
else
        print "Error"
fi

This is the output:

2.4.4
./test9.sh[9]: [: 2.4.3: arithmetic syntax error
Error

I would like to know why I can't make the comparison?
Thanks

radicaled
  • 2,369
  • 5
  • 30
  • 44

3 Answers3

2

Because 2.4.3 in NOT a number, hence you cannot use a NUMERIC comparison.

These are strings, and it's not a good idea to compare versions just like that. For instance:

  1.2.3 >= 1.0.0 
  but
  1.2.3 < 1.10.3

See the problem?

opalenzuela
  • 3,139
  • 21
  • 41
1
if [[ '2.4.3' > "$version" ]]
then
    echo  "Mayor or equal"
else
    echo "Error"
fi
michael501
  • 1,452
  • 9
  • 18
-1

In this case you could remove the decimals, but it is not a general solution. It would only work for your given example and not for version 1.10.5 or even 2.10...

 [ 244 -gt ${version//./} ] && echo True

If you want something extremely robust (and long) you could use the solution linked in the comments.

Community
  • 1
  • 1
beroe
  • 11,784
  • 5
  • 34
  • 79
  • 1
    Addressing only a (very) special case is not very useful. – chepner Oct 09 '13 at 15:25
  • Well, I expected your objection, but it matches a lot of cases, just not when the versions go beyone 9 and not when there are a variable number of sub-versions. Neither seems to be the case for [apache versions](http://httpd.apache.org/docs/). I made it clear this was just for the OPs case, and the robust solution is VERY long, and probably overkill for most uses. – beroe Oct 09 '13 at 18:04