dotfiles/sh/.local/bin/version_at_least
Marty Oehme cf85357f5c
sh: Add minimum version checker
Added another building block program. This program will be required
going forward in other modules. `version_at_least` takes two version
numbers and returns true if the second is higher than the first.

That means primarily it is useful to check if a program fulfills a
minimum required version, e.g.: `version_at_least 2.1 $(my_program -v)`.
Will be primarily useful for git version checking in the near future.

It has a second possibility of checking for ordered version numbers
passed in but this is of less use. Check source for more information.
2021-11-23 10:30:57 +01:00

19 lines
800 B
Bash
Executable file

#!/usr/bin/env sh
# check for minimum version being fulfilled
# Argument $1 must be the target version
# Argument $2 must be the program version to check for
#
# Versions must be stripped of any leading labels or 'v', 'version' prefix
# and will only be checked for their numeric contents.
# 1.0 2.13 5.1.25 are all valid version numbers.
#
# Can also be used to check the correct sorting of an arbitrary number of
# versions, if invoked like:
# version_at_least 1.0 2.0 2.2 3.13
# If versions are out of order will return false.
#
# Further reading:
# https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script
# https://stackoverflow.com/questions/4493205/unix-sort-of-version-numbers
printf '%s\n' "$@" | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 --check=quiet