Created
February 19, 2012 00:13
-
-
Save livibetter/1861384 to your computer and use it in GitHub Desktop.
Version comparison in Bash
This file contains 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 | |
# 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. | |
VER_SPLIT_SED='s/\./ /g;s/\([[:digit:]]\)\([^[:digit:] ]\)/\1 \2/g;s/\([^[:digit:] ]\)\([[:digit:]]\)/\1 \2/g' | |
# Compare with one element of version components | |
_ver_cmp_1() { | |
[[ "$1" = "$2" ]] && return 0 | |
if [[ -z "${1//[[:digit:]]/}" ]] && [[ -z "${2//[[:digit:]]/}" ]]; then | |
# Both $1 and $2 are numbers | |
# Using arithmetic comparison | |
(( $1 > $2 )) && return 1 | |
(( $1 < $2 )) && return 2 | |
else | |
# Either or both are not numbers, containing non-digit characters | |
# Using string comparison | |
[[ "$1" > "$2" ]] && return 1 | |
[[ "$1" < "$2" ]] && return 2 | |
fi | |
# This should not be happening | |
exit 1 | |
} | |
ver_cmp() { | |
local A B i result | |
A=($(sed "$VER_SPLIT_SED" <<< "$1")) | |
B=($(sed "$VER_SPLIT_SED" <<< "$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 $? | |
} |
This file contains 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 | |
# 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() { | |
(( $1 == $2 )) && return 0 | |
(( $1 > $2 )) && return 1 | |
(( $1 < $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 $? | |
} |
This file contains 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 | |
# 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