Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Script to compare version numbers

Hi All,

I'm struggling with a script I've written to compare version numbers of software installed on my server. Bascially what the script needs to do is check the version of software currently installed, and if the version is smaller than the new version I'm trying to install, then it's OK to go ahead and install the new software. If the version on the server is greater than the software I'm trying to install it should notify me that the installation should not take place.

Version numbers could look like the following:
1.30.20
1.3.2
1.30
1.03
1.3

The normal way I would have tested:

if [ $CURRENTVER -lt $NEWVER ]

is not acceptable because it will only look at the first digit before the .
example:
The above piece of code "sees" 1.3.1 as "1" and therefore will tell me that a version 1.4.0 is equal to the version of 1.3.1 which obviously it's not.

I'm using ksh
Can anyone give me a hand here?
0
jnbkze
Asked:
jnbkze
1 Solution
 
liddlerCommented:
A few thoughts that may help
You could use something like:
sed 's/\.//g'
to strip the .'s out of your version string, and then compare the result. so 1.1.3 would be 113 which is greater than 1.1.0 (110).
Obviously this would only work if your version #'s are exactly a.b.c, so it would not work in all of the examples above.
hmm

Maybe loop through the string,
concatenate numbers before the first .
multiply by 10000
concatenate numbers before the second .
multiply by 100
concatenate numbers before the third .
add the numbers
0
 
HamdyHassanCommented:
I have the same idea as liddler.

you can use command cut
for first part      cut -f1 -d"."
for second part      cut -f2 -d"."
for third part      cut -f3 -d"."
....etc

also you can use command expr for maths ( add multiply..)

first_part=` echo $VERSION | cut -f1 -d"." `
first_part_math=`expr  $first_part * 10000`
echo $first_part_math

0
 
Vinit KainCommented:
Hi,

There is another way too,

before if command add the line below, in your script

WHICH_VER=`echo ""|awk -v OLD=$CURRENTVER -v NEW=$NEWVER '{if(OLD<NEW)print "NEW";else{print "OLD"}}'`
if [ "$WHICH_VER" == "OLD" ]
then
{
keep the old version
}
else
{
install new
}
fi
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jnbkzeAuthor Commented:
Hi All,

After taking a step back and re-thinking (and adittedly doing a lot of reading on C and scripting) and came up with the following method. Take a look at the script.

NEW_VER_LINE=$2
CURRENT_VER_LINE=`swlist -l product|grep $1|grep -v hpux`
set -- $CURRENT_VER_LINE
CURRENT_VER=`echo $2 | awk -F. 'BEGIN {}
{

for (i = 1; i <= NF; i++) printf "%02d",$i
} END {}'`
NEW_VER=`echo $NEW_VER_LINE | awk -F. 'BEGIN {}
{

for (i = 1; i <= NF; i++) printf "%02d",$i
} END {}'`
#----------------------------------------------
# Next 2 lines for testing purposes
echo "Current software version is $CURRENT_VER"
echo "New software version is $NEW_VER"
if [ $CURRENT_VER -lt $NEW_VER ]
then
        echo "New version is Higher than old version"
elif [ $CURRENT_VER -gt $NEW_VER ]
then
        echo "New version is Lower than old version"
else
        echo "The two versions are the same"
fi
0
 
liddlerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ and refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0
 
ChmodCommented:
PAQed, with points refunded (250)

Chmod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now