site  contact  subhomenews

Comparing dotted version numbers

March 29, 2011 — BarryK
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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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?

Comments

vnc client ?
Username: broomdodger
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

right or wrong? different
Username: L18L
"I am using using wary503q # echo -e '01.0.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.

bash extended test command
Username: steev
"The bash extended test command can compare dotted version numbers. [code] 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 [/code] though what you have seems to be better for a large list of numbers.

wrong sort
Username: BarryK
"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

coreutils problem?
Username: BarryK
"Busybox sort is correct: [i]# echo -e '01.0.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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] I think that Luci has coreutils 7.x. I reported sometime ago why I stayed with the 6.x series: http://bkhome.org/archive/blog2/200904/installwatch-was-broken.html ...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.

sort
Username: 01micko
"lupu-251(rc) [i]# 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.[/i] The result of your command is identical in luci/lupu

wrong sort
Username: L18L
"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. [b]Geschrieben von Mike Haertel und Paul Eggert[/b]. # [b]Language dependent? No[/b] LANG=C echo -e '01.0.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.([b]wary[/b], luci not installed) Sure that won't help. Some mysteries remain.

right sort
Username: L18L
"Oh, I see now coreutils version 7.4 will do it right Thanks 01micko

busybox sort (lupu)
Username: 01micko
"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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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

sort
Username: ttuuxxx
"Hi Barry and others in 2.14-top7 I tried the above command and the output was sh-3.00# echo -e '01.0.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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

sort BusyBox v1.18.4
Username: broomdodger
"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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.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.345n0.1.6789.9999n0.01.6788n2.1n0.0.45bn0.0.45a' | ./busybox sort -n 0.0.45a 0.0.45b 0.01.6788 0.1.6789.9999 01.0.345 2.1

sort comment2
Username: scsijon
"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

sort left is allright
Username: L18L
"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


Tags: puppy