How to programmatically determine the highest version kernel RPM installed?
TL;DR
The 3rd attempt actually works! I'm leaving the first 2 attempts so that others that may come across this Q&A in the future will hopefully gain some insight into how non-trivial a problem it is to parse RPM version information and determine the lineage of which came first, second, etc.
Attempt #1 (OP said didn't work)
This command will sort the output and give you them in version order:
$ rpm -q kernel --queryformat "%{VERSION} %{RELEASE}\n"|sort -n
2.6.18 238.12.1.el5
2.6.18 238.19.1.el5
2.6.18 274.12.1.el5
2.6.18 308.8.2.el5
WHY IT DIDN'T WORK: A naive person would think that you can use some variant of the sort
command to perform this task, but there is enough variability and inconsistency in the formatting of the actual version information for a given RPM that it just isn't up to the task.
Attempt #2 (OP said didn't work)
$ rpm -q --last kernel | head -n 1 | cut -d' ' -f1
kernel-2.6.35.14-106.fc14
WHY IT DIDN'T WORK: I had high hopes that this approach would yield the results the OP was looking for, but the issue with this one as @Joel pointed out in the comments, is that the --last
switch is merely returning the results sorted by the date the RPMs were installed.
Attempt #3
This one will definitely do the job. I found a suite of tools called RPM Development Tools. There are 2 tools in this suite that will give you the capability to determine whether one version of a RPM is newer or older than another.
If the RPM isn't already installed you can do so as follows:
yum install rpmdevtools
The first tool that's useful is called rpmdev-vercmp
. This tool can compare 2 names of RPMs and tell you which one is newer. For example:
$ rpmdev-vercmp kernel-2.6.35.14-100.fc14.x86_64 kernel-2.6.35.14-103.fc14.x86_64
0:kernel-2.6.35.14-103.fc14.x86_64 is newer
After finding this I was all set to put together a shell script but then realized, man I'm lazy, so I poked a few minutes more and found another tool in the suite called rpmdev-sort
.
This tool is pay-dirt. You can use it as follows:
$ rpm -q kernel | rpmdev-sort
kernel-2.6.35.14-100.fc14.x86_64
kernel-2.6.35.14-103.fc14.x86_64
kernel-2.6.35.14-106.fc14.x86_64
There are a lot of tools in RPM Development Tools that might be worth a look for others so I'm listing them here for future reference.
$ rpm -q --queryformat '[%{NAME} %{FILEMODES:perms} %{FILENAMES}\n]' rpmdevtools \
| grep -E "^.* -..x..x..x " \
| awk '{print $3}' \
| sed 's#/usr/bin/##' \
| paste - - - \
| column -t
annotate-output checkbashisms licensecheck
manpage-alert rpmargs rpmdev-bumpspec
rpmdev-checksig rpmdev-cksum rpmdev-diff
rpmdev-extract rpmdev-md5 rpmdev-newinit
rpmdev-newspec rpmdev-packager rpmdev-rmdevelrpms
rpmdev-setuptree rpmdev-sha1 rpmdev-sha224
rpmdev-sha256 rpmdev-sha384 rpmdev-sha512
rpmdev-sort rpmdev-sum rpmdev-vercmp
rpmdev-wipetree rpmelfsym rpmfile
rpminfo rpmls rpmpeek
rpmsodiff rpmsoname spectool
An alternative to #3
An alternative that the OP mentioned in the comments is to use sort -V
. That's a capital -V
. I'd never heard of this switch either. From the sort
man page:
-V, --version-sort
natural sort of (version) numbers within text
As it turns out sort
does provide a facility to sort version numbers so you could also perform the sorting like so:
$ rpm -q kernel | sort -V
kernel-2.6.35.14-100.fc14.x86_64
kernel-2.6.35.14-103.fc14.x86_64
kernel-2.6.35.14-106.fc14.x86_64