Skip to content

Instantly share code, notes, and snippets.

@arvenil
Forked from livibetter/vercmp.sh
Last active August 29, 2015 14:04
Show Gist options
  • Save arvenil/ed695ef374d5df701074 to your computer and use it in GitHub Desktop.
Save arvenil/ed695ef374d5df701074 to your computer and use it in GitHub Desktop.
Fix for "08: value too great for base", works for versions with numbers only (no letters, no rc suffixes)
#!/bin/bash
# Copyright (c) 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Compare with one element of version components
_ver_cmp_1() {
(( "10#$1" == "10#$2" )) && return 0
(( "10#$1" > "10#$2" )) && return 1
(( "10#$1" < "10#$2" )) && return 2
# This should not be happening
exit 1
}
ver_cmp() {
local A B i result
A=(${1//./ })
B=(${2//./ })
i=0
while (( i < ${#A[@]} )) && (( i < ${#B[@]})); do
_ver_cmp_1 "${A[i]}" "${B[i]}"
result=$?
[[ $result =~ [12] ]] && return $result
let i++
done
# Which has more, then it is the newer version
_ver_cmp_1 "${#A[i]}" "${#B[i]}"
return $?
}
#!/bin/bash
# Copyright (c) 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
source "$1"
##################################################
test_vc_1() {
local result
_ver_cmp_1 "$1" "$2"
result=$?
if (( $result == $3 )); then
echo "PASS: $1 $2 => $3"
else
echo "FAIL: $1 $2 => $3 != $result"
fi
}
test_vc() {
local result
( ver_cmp "$1" "$2" )
result=$?
if (( $result == $3 )); then
echo "PASS: $1 $2 => $3"
else
echo "FAIL: $1 $2 => $3 != $result"
fi
}
test_vc 1.0 0.9 1
# Test cases from
# http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
test_vc 1 1 0
test_vc 2.1 2.2 2
test_vc 3.0.4.10 3.0.4.2 1
test_vc 4.08 4.08.01 2
test_vc 3.2.1.9.8144 3.2 1
test_vc 3.2 3.2.1.9.8144 2
test_vc 1.2 2.1 2
test_vc 2.1 1.2 1
test_vc 1.0.9 1.0.12 2
test_vc 1.0.12 1.0.9 1
#test_vc 1.0 1.0rc1 1
#test_vc 1.0a 1.0rc1 2
#test_vc 1.0a 1.0a2 2
#test_vc 1.0rc12 1.0rc9 1
test_vc_1 1 2 2
test_vc_1 2 1 1
test_vc_1 1 1 0
#test_vc_1 1 a 2
#test_vc_1 a 1 1
test_vc_1 9 21 2
test_vc_1 21 9 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment