Re: versions comparison
On Sat, Feb 18, 2023 at 08:38:22AM +0800, winnie hw wrote:
> sorry this is maybe not related to debian directly.
> but how can I compare two versions of a package by programming?
> for instance, v1.24.0.1 should be later than v1.23.99.999.
Debian's dpkg(1) command has a --compare-versions option.
--compare-versions ver1 op ver2
Compare version numbers, where op is a binary operator. dpkg
returns true (0) if the specified condition is satisfied, and false
(1) otherwise.
unicorn:~$ if dpkg --compare-versions v1.24.0.1 gt v1.23.99.999; then echo true; else echo false; fi
dpkg: warning: version 'v1.24.0.1' has bad syntax: version number does not start with digit
dpkg: warning: version 'v1.23.99.999' has bad syntax: version number does not start with digit
true
I wonder if those were the actual version numbers you intended to compare.
Also, your example isn't particularly good, because a simple string
comparison also would have returned true in this case, even though it's
entirely the wrong approach.
A better example would be something like:
unicorn:~$ if dpkg --compare-versions 1.24.0.1 gt 1.9.99.999; then echo true; else echo false; fi
true
because a string comparison gives a different result here.
Also, you might be interested in the -V option of GNU sort(1). It
purports to do some kind of version string comparison. I have no idea
whether it handles all the possible Debian versions strings the same
way that dpkg --compare-versions does.
Reply to: