brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: dpkg --compare-versions tells a script which version is newer

Linux
~/tips/tip-dpkg-compare-versions-script

dpkg --compare-versions 1.10 gt 1.9 && echo newer

Compares two Debian version strings and answers with the exit code.

Comparing version numbers with string tools goes wrong quickly, because 1.10 is newer than 1.9. On Debian and Ubuntu, dpkg already has the right comparison built in.

dpkg --compare-versions 1.10 gt 1.9 && echo newer

The man page says dpkg --compare-versions ver1 op ver2 returns true (0) if the condition is satisfied and false (1) otherwise. The operators are lt le eq ne ge gt. Nothing is printed, so you use the exit code in an if.

dpkg --compare-versions 1.0~rc1 lt 1.0 && echo yes

On dpkg 1.22.6 this prints yes: with Debian rules a tilde sorts before everything, so a release candidate is older than the release. The same goes for 2:1.0 gt 3.0, which is true here because of the epoch before the colon.

A practical use is checking an installed package in a script:

v=$(dpkg-query -W -f='${Version}' openssl)
if dpkg --compare-versions "$v" ge 3.0; then echo ok; fi

The caveat is about empty versions. Operators lt le eq ne ge gt treat an empty version as earlier than any version, while lt-nl le-nl ge-nl gt-nl treat it as later. If dpkg-query prints nothing because the package is not installed, lt will say that it is older. Also avoid the old < and > operators: the docs call them obsolete because of their confusing semantics.

Further reading: dpkg(1): –compare-versions

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link