1

I have a bash script for updating a tool which should compare the currently installed version with the latest available version. This always worked until now, where a new version was released, called "4.10.0".

I have tried the following two if statements and both returned the older version (4.9.4) as the newer one:

if [[ $CURRENT_GHOST > $PACKAGE_VERSION_OLD ]]
if [ "$(printf '%s\n' "$CURRENT_GHOST" "$PACKAGE_VERSION_OLD" | sort -V | head -n1)" = "$CURRENT_GHOST" ];

How can I improve it so that even the 4.10.0 gets detected as a newer (higher) version than the older (lower) version 4.9.4?

Peleke
  • 111
  • 1
  • AFAIK `sort -V` sorts from oldest to newest version - are you sure you don't want `tail -n1` here? – steeldriver Jul 20 '21 at 20:26
  • Ah, my mistake, thanks for the quick and easy solution! – Peleke Jul 20 '21 at 20:31
  • On Debian and Debian-based systems you could also use `dpkg --compare-versions 4.10.0 gt 4.9.4`, but then note that there may be differences between the various versioning rules, e.g. in Debian's system `1.2.3~xyz` sorts as _older than_ `1.2.3`, or `1.2.3-abc`. But then, `sort -V` seems to also follow that rule. Interesting. – ilkkachu Jul 20 '21 at 20:44

0 Answers0