Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing version strings in Zsh

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
like image 901
Josh Pinto Avatar asked Dec 22 '16 02:12

Josh Pinto


1 Answers

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 ...
like image 76
just somebody Avatar answered Nov 17 '22 18:11

just somebody