From 1653a6da57d01ff7bbf6bb82737ccb0c6b641da2 Mon Sep 17 00:00:00 2001 From: deajan Date: Fri, 3 Feb 2017 12:00:03 +0100 Subject: [PATCH] Added vercomp function --- dev/ofunctions.sh | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index f2b8dbd..af49b4c 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -3,7 +3,7 @@ #### OFUNCTIONS MINI SUBSET #### _OFUNCTIONS_VERSION=2.1-RC1+dev -_OFUNCTIONS_BUILD=2017010401 +_OFUNCTIONS_BUILD=2017020301 #### _OFUNCTIONS_BOOTSTRAP SUBSET #### _OFUNCTIONS_BOOTSTRAP=true #### _OFUNCTIONS_BOOTSTRAP SUBSET END #### @@ -1790,4 +1790,37 @@ function ParentPid { fi } +# Neat version compare function found at http://stackoverflow.com/a/4025065/2635443 +# Returns 0 if equal, 1 if $1 > $2 and 2 if $1 < $2 +vercomp () { + if [[ $1 == $2 ]] + then + return 0 + fi + local IFS=. + local i ver1=($1) ver2=($2) + # fill empty fields in ver1 with zeros + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) + do + ver1[i]=0 + done + for ((i=0; i<${#ver1[@]}; i++)) + do + if [[ -z ${ver2[i]} ]] + then + # fill empty fields in ver2 with zeros + ver2[i]=0 + fi + if ((10#${ver1[i]} > 10#${ver2[i]})) + then + return 1 + fi + if ((10#${ver1[i]} < 10#${ver2[i]})) + then + return 2 + fi + done + return 0 +} + #### OFUNCTIONS FULL SUBSET END ####