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

0

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

1
if [[ '2.4.3' > "$version" ]]
then
    echo  "Mayor or equal"
else
    echo "Error"
fi

2 Comments

Perhaps an explanation of the difference between [[ and [?
-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.

2 Comments

Addressing only a (very) special case is not very useful.
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. I made it clear this was just for the OPs case, and the robust solution is VERY long, and probably overkill for most uses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.