Created
October 27, 2014 17:47
-
-
Save devpuppy/6cf03fb4a5a8a36c6cf9 to your computer and use it in GitHub Desktop.
utilities for comparing version strings on OS X
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
#!/bin/bash | |
# accepts as many version strings as you like | |
# returns them in order, one per line, highest first | |
function vseq() | |
{ | |
# collect arbitrary number of args | |
while [ -n "$1" ]; do | |
args="$args$1," | |
shift | |
done | |
echo $args | tr ',' '\n' | sort -t. -k1,1nr -k2,2nr -k3,3nr | |
} | |
# first argument is test value | |
# second argument is minimum version required | |
# eg vreq `ruby -v` 1.9 | |
function vreq() | |
{ | |
max=`vseq $1 $2 | head -1` | |
if [[ $1 == $max ]]; then | |
echo true; return | |
fi | |
} | |
### using the functions | |
if [[ `vreq 1.2 1.2` ]]; then | |
echo "1.2 is good" | |
fi | |
if [[ ! `vreq 1.2.1 1.20` ]]; then | |
echo "1.2.1 is too low" | |
fi | |
if [[ `vreq 0.90.a 0.9.5` ]]; then | |
echo '0.90.a is sufficient' | |
fi | |
echo "let's order some versions..." | |
vseq 1.2.100.4 1.2.3.4 10.1.2.3 9.1.2.3 | |
# 10.1.2.3 | |
# 9.1.2.3 | |
# 1.2.100.4 | |
# 1.2.3.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment