Last active
April 28, 2017 19:35
-
-
Save xenoscopic/11222029 to your computer and use it in GitHub Desktop.
Comparing Program Versions in POSIX Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://havoc.io/blog/post/comparing-program-versions-in-posix-shell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git --version | |
git version 1.8.5.2 (Apple Git-48) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git --version | cut -d ' ' -f3 | |
1.9.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git --version | |
git version 1.9.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ test '1.9.2' \< '1.9.12' | |
$ echo $? | |
1 # Comparison was false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ test '1.9.2' \< '1.9.3' | |
$ echo $? | |
0 # Comparison was true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sort -t "." -n -k1,1 -k2,2 -k3,3 -k4,4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently blog-semver url changed to https://havoc.io/post/shellsemver/