?
Solved

Script to compare version numbers

Posted on 2003-03-07
6
Medium Priority
?
445 Views
Last Modified: 2013-12-26
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
Comment
Question by:jnbkze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 8087803
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8088022
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
 
LVL 4

Expert Comment

by:Vinit Kain
ID: 8093302
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!

 
LVL 3

Author Comment

by:jnbkze
ID: 8127385
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
 
LVL 18

Expert Comment

by:liddler
ID: 9845831
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
 

Accepted Solution

by:
Chmod earned 0 total points
ID: 9892007
PAQed, with points refunded (250)

Chmod
Community Support Moderator
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question