Skip to content

Instantly share code, notes, and snippets.

@mpapis
Created January 5, 2012 21:44
Show Gist options
  • Select an option

  • Save mpapis/1567487 to your computer and use it in GitHub Desktop.

Select an option

Save mpapis/1567487 to your computer and use it in GitHub Desktop.
shell version compare - call me crazy
__rvm_version_compare()
{
typeset v1 v2 v1d v2d dots x counter IFS
typeset -a transformer
v1=$1
v2=$3
v1d=$( printf $v1 | grep -o '\.' | wc -l )
v2d=$( printf $v2 | grep -o '\.' | wc -l )
if [[ $v1d -ge $v2d ]]
then dots=$v1d
else dots=$v2d
fi
counter=1
for x in $( seq $dots 0 )
do
transformer+=( "$(( 256 ** x ))*\$${counter}" )
: $(( counter+=1 ))
done
IFS="+" # to put + in transformer ;)
eval "$(
printf '[[ ';
printf $1 | awk -F. "{ printf ${transformer[*]} }";
printf -- " $2 ";
printf $3 | awk -F. "{ printf ${transformer[*]} }";
printf ' ]]'
)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment