Comparing RPM versions in the shell

June 30, 2014

Via Planet Debian I recently read Comparing version numbers in the shell, which sort of suggests using sort -V to compare package version numbers on RPM-derived systems. My reaction to this is kind of mixed, because how good an idea this is depends on what you're trying to do with this comparison.

If you just want to compare generic version numbers, 'sort -V' may do what you want. You should read the coreutils documentation for sort very carefully, especially Details about version sort, because how it works may not be quite what you expect or what you want. You probably want to test it.

If you want to compare version numbers in the same way that RPM does it, so that your idea of a 'newer version' specifically matches what eg 'rpm -U' or 'yum localupdate' would conclude, you should use the rpmdev-vercmp command (it's part of the rpmdevtools package). This uses RPM's actual version comparison code (as exposed through the RPM Python bindings). See 'rpmdev-vercmp -h' for basic usage and then experiment with it; note that it prints output and has somewhat odd status returns. You may also want to see 'rpmdev-sort'.

(If you just care about version numbers, you can ignore the epoch and the release parts of ordinary RPM version numbering; rpmdev-vercmp doesn't require you to supply them.)

If you want to compare the versions of actual RPM packages as rpm and yum do it, you really have to use rpmdev-vercmp and you need to extract full RPM version information about both packages, including their RPM epochs if any. It's a pity that there is no easy command to print out the full version information that rpmdev-vercmp requires (it's a bit complex and annoying for reasons covered here, although for most purposes you can apparently consider a missing epoch number to be equal to an epoch number of '0').

As it turns out, the best approximation of quickly printing out full RPM version information for rpmdev-vercmp purposes is something like:

rpm -q --qf '%{N} %{epochnum}:%{V}-%{R}\n' ...

Note that you should not accidentally give rpmdev-vercmp strings that have the package name in them; it will consider them to be the version and then interpret the version number as the first part of the release numbering (I think).

(I discovered rpmdev-vercmp via this stackoverflow question and its answer when I was researching this entry. Once again writing blog entries has proven usefully educational.)

Written on 30 June 2014.
« My .screenrc
An index of non-letter control characters »

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Jun 30 22:38:58 2014
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.