Skip to content

Instantly share code, notes, and snippets.

@devpuppy
Created October 27, 2014 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devpuppy/6cf03fb4a5a8a36c6cf9 to your computer and use it in GitHub Desktop.
Save devpuppy/6cf03fb4a5a8a36c6cf9 to your computer and use it in GitHub Desktop.
utilities for comparing version strings on OS X
#!/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