Skip to content

Instantly share code, notes, and snippets.

@xenoscopic
Last active April 28, 2017 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xenoscopic/11222029 to your computer and use it in GitHub Desktop.
Save xenoscopic/11222029 to your computer and use it in GitHub Desktop.
Comparing Program Versions in POSIX Shell
http://havoc.io/blog/post/comparing-program-versions-in-posix-shell
# Check that the version of Git installed is supported
GIT_VERSION=$(git --version | cut -d ' ' -f3)
MIN_GIT_VERSION="1.7.6"
LOWEST_GIT_VERSION=$(printf "$GIT_VERSION\n$MIN_GIT_VERSION" \
| sort -t "." -n -k1,1 -k2,2 -k3,3 -k4,4 \
| head -n 1)
if [ "$LOWEST_GIT_VERSION" != "$MIN_GIT_VERSION" ]; then
echo "error: Git version ($GIT_VERSION) too old, need $MIN_GIT_VERSION+"
exit 1
fi
$ git --version
git version 1.8.5.2 (Apple Git-48)
$ git --version | cut -d ' ' -f3
1.9.2
$ git --version
git version 1.9.2
$ test '1.9.2' \< '1.9.12'
$ echo $?
1 # Comparison was false
$ test '1.9.2' \< '1.9.3'
$ echo $?
0 # Comparison was true
sort -t "." -n -k1,1 -k2,2 -k3,3 -k4,4
@behcet
Copy link

behcet commented Dec 24, 2016

Apparently blog-semver url changed to https://havoc.io/post/shellsemver/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment