6

I am using this script:

if [[ -f /usr/bin/atom ]]; then
  ATOM_INSTALLED_VERSION=$(rpm -qi atom | grep "Version" |  cut -d ':' -f 2 | cut -d ' ' -f 2)
else
  ATOM_INSTALLED_VERSION=""
fi
ATOM_LATEST_VERSION=$(wget -q "https://api.github.com/repos/atom/atom/releases/latest"  -O - | grep -E "https.*atom-amd64.tar.gz" | cut -d '"' -f 4 | cut -d '/' -f 8 | sed 's/v//g')

if [[ "$ATOM_INSTALLED_VERSION" -lt "$ATOM_LATEST_VERSION" ]]; then
  sudo dnf install -y https://github.com/atom/atom/releases/download/v${ATOM_LATEST_VERSION}/atom.x86_64.rpm
fi

to check for Atom upgrades and install them if available. The problem is that the test:

[[ "$ATOM_INSTALLED_VERSION" -lt "$ATOM_LATEST_VERSION" ]]

returns:

zsh: bad floating point constant

where (showing input and output):

$ printf $ATOM_INSTALLED_VERSION
1.8.0%
$ printf $ATOM_LATEST_VERSION
1.12.7%

how do I write a test that will work? I have tried using (( $ATOM_INSTALLED_VERSION < $ATOM_LATEST_VERSION )) but that also failed giving:

zsh: bad floating point constant
just somebody
  • 18,602
  • 6
  • 51
  • 60
Josh Pinto
  • 1,453
  • 4
  • 20
  • 37
  • POSIX-like shells such as Zsh only support integer arithmetic. That said, you really need custom, component-by-component logic to properly compare _version numbers_. – mklement0 Dec 22 '16 at 02:53
  • [This answer](http://stackoverflow.com/a/4025065/45375) has a Bash function that properly compares version numbers; it may work as-is, or you should be be able to make it work with only a few tweaks. – mklement0 Dec 22 '16 at 02:55
  • `zsh` supports floating-point arithmetic, but the point remains that version numbers are not floating-point values. – chepner Dec 22 '16 at 14:59
  • `rpm -qi atom` will work *regardless* of where `atom` is installed; you don't need to check for the existence of a specific path (which might be wrong) first. – chepner Dec 22 '16 at 15:00
  • You can get the version number with `rpm` alone: `rpm -q --qf '%{VERSION}' atom`, and you can check the exit status of the command to determine if `atom` is installed anywhere on your system. – chepner Dec 22 '16 at 15:13

1 Answers1

8

zsh comes equipped with a function for version string comparisons, see zshcontrib(1).

installed=$(rpm -q --qf '%{VERSION}' atom)
latest=$(wget -q ...)
autoload is-at-least
is-at-least $latest ${installed:-0} || sudo dnf install -y ...
just somebody
  • 18,602
  • 6
  • 51
  • 60