Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

compare versions.

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-11-2011
successlin successlin is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 15 June 2012, 10:03 AM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
compare versions.

Hi ,
I have versions something like 1.10.0 and 1.9.1 and i want to compare them.
I wrote sample program like below.


Code:
#!/usr/bin/perl
my $var1 = "1.10.0";
my $var2 = "1.9.0";
if ($var1 eq $var2)
{
print "EQUAL\n";
}
if ($var1 gt $var2)
{
print "GREATER $var1 $var2\n";
}
if ($var1 lt $var2)
{
print "LOWER $var1 $var2\n";
}

This is printing LOWER ene though 1.10.0 is greater than 1.9.1.
Please help me how to compare these type of numbers.

Last edited by pludi; 07-11-2011 at 09:48 AM..
Sponsored Links
    #2  
Old Unix and Linux 07-11-2011
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 28 July 2015, 6:52 PM EDT
Location: South Carolina, USA
Posts: 902
Thanks: 30
Thanked 286 Times in 267 Posts
They look like strings to me. Linux

You may have to split them at the decimal and compare.
Sponsored Links
    #3  
Old Unix and Linux 07-11-2011
pludi's Unix or Linux Image
pludi pludi is offline Forum Advisor  
Cat herder
 
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,522
Thanks: 38
Thanked 335 Times in 308 Posts
Since they're not pure numbers, Perl is treating them like strings, and then the comparison follows the rules of the strcmp function. The first and second character in both are the same, but the third is different, and 1 comes before 9, and is thus considered lower.
    #4  
Old Unix and Linux 07-11-2011
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
A ksh solution :
Code:
#!/usr/bin/ksh
# Usage: $0 version1 version2

set -o nounset

oIFS="$IFS" ;IFS='.'
set -A v1 $1
set -A v2 $2
IFS="${oIFS}"

cnt1=${#v1[*]}
cnt2=${#v2[*]}
(( cnt1 > cnt2 )) && cnt=$cnt1 || cnt=$cnt2;

i=0
result="EQUAL"
while (( i <= cnt ))
do
    (( n1 = ${v1[$i]-} + 0 ))
    (( n2 = ${v2[$i]-} + 0 ))
    if (( n1 > n2 ))
    then
        result="GREATER"
        break
    elif (( n1 < n2 ))
    then
        result="LOWER"
        break
    fi
    (( i += 1 ))
done

print ${result} $1 $2

Examples:
Code:
$ ./cmpv.ksh 1.2.3 1.2.3
EQUAL 1.2.3 1.2.3
$ ./cmpv.ksh 1.2.4 1.2.3
GREATER 1.2.4 1.2.3
$ ./cmpv.ksh 1.2.4 1.2.4.1
LOWER 1.2.4 1.2.4.1

Jean-Pierre.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Require compare command to compare 4 files nehashine Shell Programming and Scripting 6 08-27-2010 07:29 AM
Versions of HP-UX or AIX kurt1978 UNIX for Dummies Questions & Answers 5 05-31-2007 11:09 AM
Is there any way to get old HP-UX versions? dlundh HP-UX 4 05-29-2007 07:35 AM
File Versions (GDG) Bizcut UNIX for Dummies Questions & Answers 0 07-27-2005 09:50 AM
different versions? deeptia Shell Programming and Scripting 1 03-25-2002 12:35 PM



All times are GMT -4. The time now is 12:49 AM.