Comparing dotted version numbers

I want to compare version numbers in a shell script. I was uncertain about the simplest way to do this. I found this Bash function:

http://lists.us.dell.com/pipermail/dkms-devel/2004-July/000142.html

Um, but no, as far as I can see, a simpler way to do it is this:

# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.' -g
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1


So if you wanted the highest version number in that list:

# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.' -g | tail -n 1
2.1


The '-t' option is the "key", '-g' means numeric sort.

It seems to work alright. Anyone see anything wrong with it?


Posted on 29 Mar 2011, 19:52


Comments:

Posted on 29 Mar 2011, 24:35 by broomdodger
vnc client ?
Is there a VNC client for Puppy?

I tried to run the Linux version of RealVNC but some libs where not available in Puppy.

-Bill



Posted on 30 Mar 2011, 3:18 by L18L
right or wrong? different
I am using using wary503q
# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.' -g
0.01.6788
0.0.45a
0.0.45b
0.1.6789.9999
01.0.345
2.1
#
head will be different.



Posted on 30 Mar 2011, 7:44 by steev
bash extended test command
The bash extended test command can compare dotted version numbers.


VER1=2.6.33.2
VER2=2.6.38
if [[ $VER1 > $VER2 ]]; then
echo "$VER1 is greater than $VER2"
elif [[ $VER1 == $VER2 ]]; then
echo "$VER1 is equal to $VER2"
else
echo "$VER1 is less than $VER2"
fi


though what you have seems to be better for a large list of numbers.


Posted on 30 Mar 2011, 16:36 by BarryK
wrong sort
L18L,
That's weird, your sort is wrong.

My 'sort' utility is from 'coreutils' version 6.9, in Wary/Quirky. What version is in Luci? this will tell you:

# sort --version



Posted on 30 Mar 2011, 16:46 by BarryK
coreutils problem?
Busybox sort is correct:

# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | busybox sort -t '.' -g
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1


I think that Luci has coreutils 7.x. I reported sometime ago why I stayed with the 6.x series:
http://bkhome.org/blog/?viewDetailed=00660
...although, I think that ttuuxxx reported that there is an updated 'installwatch' that works with coreutils 7.x.

So, perhaps we have found another problem.

Anyway, I know that the Busybox sort works, so I will use that.

Note, I want my script to work with Busybox Ash, and don't know if the extended compare will work.



Posted on 30 Mar 2011, 16:51 by 01micko
sort
lupu-251(rc)
# sort --version
sort (GNU coreutils) 7.4
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.


The result of your command is identical in luci/lupu



Posted on 30 Mar 2011, 16:55 by L18L
wrong sort
That's weird, indeed.

# sort --version
sort (GNU coreutils) 6.9
Copyright 2007 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Geschrieben von Mike Haertel und Paul Eggert.
#
Language dependent?
No

LANG=C echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.' -g
0.01.6788
0.0.45a
0.0.45b
0.1.6789.9999
01.0.345
2.1
#
same weird result.(wary, luci not installed)
Sure that won't help.
Some mysteries remain.



Posted on 30 Mar 2011, 17:00 by L18L
right sort
Oh, I see now
coreutils version 7.4 will do it right
Thanks 01micko


Posted on 30 Mar 2011, 17:11 by 01micko
busybox sort (lupu)
I renamed the sort exec to sortORIG and renamed the busy box sort-NOT-USED to sort and tried the command>

echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.' -g
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1

Looks ok from here ;). So lupu works with original coreutils sort and busybox sort.

HTH


Posted on 31 Mar 2011, 6:52 by ttuuxxx
sort
Hi Barry and others in 2.14-top7 I tried the above command and the output was
sh-3.00# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.'
0.01.6788
0.0.45a
0.0.45b
01.0.345
0.1.6789.9999
2.1
sh-3.00#
and

sh-3.00# sort --version
sort (GNU coreutils) 6.12

Then I compiled the latest 8.10 and just replaced the sort with latest
sh-3.00# echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | sort -t '.'
0.01.6788
0.0.45a
0.0.45b
01.0.345
0.1.6789.9999
2.1
sh-3.00# sort --version
sort (GNU coreutils) 8.10

same results



Posted on 31 Mar 2011, 9:09 by broomdodger
sort BusyBox v1.18.4
180302 ./busybox sort --help
BusyBox v1.18.4 (2011-03-30 17:53:43 PDT) multi-call binary.

Usage: sort [-nrugMcszbdfimSTokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR] [FILE]...

Sort lines of text

Options:
-b Ignore leading blanks
-c Check whether input is sorted
-d Dictionary order (blank or alphanumeric only)
-f Ignore case
-g General numerical sort
-i Ignore unprintable characters
-k Sort key
-M Sort month
-n Sort numbers
-o Output to file
-k Sort by key
-t CHAR Key separator
-r Reverse sort order
-s Stable (don't sort ties alphabetically)
-u Suppress duplicate lines
-z Lines are terminated by NUL, not newline
-mST Ignored for GNU compatibility

180111 echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | ./busybox sort -t '.' -g
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1

180315 echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | ./busybox sort -t '.' -n
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1

180332 echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | ./busybox sort -g
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1

180817 echo -e '01.0.345\n0.1.6789.9999\n0.01.6788\n2.1\n0.0.45b\n0.0.45a' | ./busybox sort -n
0.0.45a
0.0.45b
0.01.6788
0.1.6789.9999
01.0.345
2.1



Posted on 31 Mar 2011, 18:17 by scsijon
sort comment
looks like, to me, that in the wrong ones it's sorting from the left place of a decimal point before displaying, while the correct ones are sorting from the right place first before display.


Posted on 1 Apr 2011, 10:46 by scsijon
sort comment2
while at a networking site doing "mail" and "puppy looking" while awaiting that a problem fix has worked.

networking guy here says what we call "wrong" is doing an ip-sort (left to right ie a.b.c.d) while "right" is doing a number-sort (right to left ie d.c.b.a). he has no idea why!

hope it helps


Posted on 1 Apr 2011, 13:57 by L18L
sort left is allright
right is not right
your network was missing the question or was it his way of making a joke? 1st of April?

0.01.6788
0.0.45a
0.0.45b
and
0.0.45a
0.0.45b
0.01.6788
both are left one of them is right(opposite of wrong) the other one is wrong (not true)

some sorts of coreutils do it wrong
busybox does it right using just -g or -n

That's the way how I do see it now
Hope that helps