With zsh
, you can use it's n
umeric o
rdering (O
rdering in reverse) parameter expansion flags:
at_least() [[ $1 = ${${(On)@}[1]} ]]
And then:
if at_least $version 1.12; then
...
fi
Note that 1.12-pre1
or 1.12-alpha3
would be considered greater than 1.12. 1.012 sorts the same as 1.12 but since one has to come before the other and we do an equality comparison we'll end up considering it not at_least 1.12
.
zsh also comes with an autoload
able is-at-least
function aimed for that (intended to compare zsh version numbers but can be used with any version provided they use similar versioning scheme as zsh's):
#! /bin/zsh -
autoload is-at-least
if
version=$(gcc -dumpfullversion 2> /dev/null) &&
[[ -n $version ]] &&
is-at-least 7.2 $version
then
...
fi
(we need to check that $version
is n
on-empty as otherwise is-at-least
would compare zsh's own version).
Note that -dumpfullversion
was added in gcc 7.1.0. -dumpversion
has been available for decades but note that it may only give the major version number.
From info gcc dumpversion
in the manual from gcc-12.2.0:
'-dumpversion'
Print the compiler version (for example, '3.0', '6.3.0' or
'7')--and don't do anything else. This is the compiler version
used in filesystem paths and specs. Depending on how the compiler
has been configured it can be just a single number (major version),
two numbers separated by a dot (major and minor version) or three
numbers separated by dots (major, minor and patchlevel version).
'-dumpfullversion'
Print the full compiler version--and don't do anything else. The
output is always three numbers separated by dots, major, minor and
patchlevel version.
$ gcc --version
gcc (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -dumpversion
12
$ gcc -dumpfullversion
12.2.0
gcc -dumpversion
– Victor Lamoine Jan 04 '17 at 14:18