Is there any way to compare such strings on bash, e.g.: 2.4.5 and 2.8 and 2.4.5.1?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
||||
|
Here is a pure Bash version that doesn't require any external utilities:
Run the tests:
|
|||||||||||||||||||||
|
|
|||
|
|
|
Here is a simple Bash function that uses no external commands. It works for version strings that have up to three numeric parts in them - less than 3 is fine as well. It can easily be extended for more. It implements
Here is the test:
A subset of the test output:
|
||||
|
|
|
Another approach(modified version of @joynes) that compares dotted versions as asked in the question
example usage:
returns: 1 since 1.10.1 is bigger than 1.7
returns: 0 since 1.10.1 is lower than 1.11 |
|||
|
|
|
I implemented yet another comparator function. This one had two specific requirements: (i) I didn't want the function to fail by using
Feel free to comment and suggest improvements. |
|||
|
|
|
Here's a refinement of the top answer (Dennis's) that is more concise and uses a different return value scheme to make it easy to implement <= and >= with a single comparison. It also compares everything after the first character not in [0-9.] lexicographically, so 1.0rc1 < 1.0rc2.
|
||||
|
|
|
I implemented a function that returns the same results as Dennis Williamson's but uses fewer lines. It does perform a sanity check initially which causes
|
|||||||||||||
|
|
If you have coreutils-7 (in Ubuntu Karmic but not Jaunty) then your
|
|||||||||||||||||||||
|
Used as such:
|
|||||
|
|
Here's another pure bash version, rather smaller than the accepted answer. It only checks whether a version is less than or equal to a "minimum version", and it will check alphanumeric sequences lexicographically, which often gives the wrong result ("snapshot" is not later than "release", to give a common example). It will work fine for major/minor.
|
||||
|
|
|
if it's just about to know whether one version is lower than another I came up checking whether
|
|||
|
|
|
I'm using embedded Linux (Yocto) with BusyBox. BusyBox I've made the following (similar to Dennis Williamson's answer) to compare using a "natural sort" type of algorithm. It splits the string into numeric parts and non-numeric parts; it compares the numeric parts numerically (so
It can compare more complicated version numbers such as
Note that it doesn't return the same result for some of the corner-cases in Dennis Williamson's answer. In particular:
But those are corner cases, and I think the results are still reasonable. |
||||
|
|
|
This is for at most 4 fields in the version.
|
|||||||||||||||||
|
|
Thanks to Dennis's solution, we can extend it to allow comparison operators '>', '<', '=', '==', '<=', and '>='.
We can then use comparison operators in the expressions like:
and test only the true/false of the result, like:
|
|||
|
|
|
Here is another pure bash solution without any external calls:
And there is even more simple solution, if you are sure that the versions in question do not contain leading zeros after the first dot:
This will work for something like 1.2.3 vs 1.3.1 vs 0.9.7, but won't work with 1.2.3 vs 1.2.3.0 or 1.01.1 vs 1.1.1 |
|||||
|
|
Well if you know the number of fields you can use -k n,n and get a super-simple solution
|
|||||||||||||||||
|
|
|
|||||||||||||||||||||
|
|
How about this? Seems to work?
|
|||
|
|
It's pretty simple and small. |
|||||
|
|
I came across and solved this problem, to add an additional (and shorter and simpler) answer... First note, extended shell comparison failed as you may already know...
Using the sort -t'.'-g (or sort -V as mentioned by kanaka) to order versions and simple bash string comparison I found a solution. The input file contains versions in columns 3 and 4 which I want to compare. This iterates through the list identifying a match or if one is greater than the other. Hope this may still help anyone looking to do this using bash as simple as possible.
Thanks to Barry's blog for the sort idea... ref: http://bkhome.org/blog/?viewDetailed=02199 |
||||
|
|
|
For old version/busybox
This is escpecial useful on version which contains alpha symbols like
|
|||||
|
|
You can recursively split on
|
|||
|
|
|
There probably is no universally correct way to achieve this. If you are trying to compare versions in the Debian package system try |
|||||||||
|
echo '10.1 < 2' | bcfor up to two levels. – Ciro Santilli 华涌低端人口 六四事件 法轮功 Jul 10 '16 at 9:17