|
| 1 | +#!/bin/bash |
| 2 | +# version_compare.sh - compares numerical versions and versions with ascii remainders |
| 3 | +# |
| 4 | +# Copyright (C) 2019 Basler AG |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# This software may be modified and distributed under the terms |
| 8 | +# of the CreativeCommons BY-SA 4.0. license. |
| 9 | +# See the https://creativecommons.org/licenses/by-sa/4.0/ file for details. |
| 10 | + |
| 11 | +#----------------- |
| 12 | +# this base snippet is by https://stackoverflow.com/users/60075/craig-mcqueen |
| 13 | +# at fetched from https://stackoverflow.com/a/31258615 |
| 14 | +# for compression purposes the debug stuff was removed |
| 15 | + |
| 16 | +ascii_frag() { |
| 17 | + expr match "$1" "\([^[:digit:]]*\)" |
| 18 | +} |
| 19 | + |
| 20 | +ascii_remainder() { |
| 21 | + expr match "$1" "[^[:digit:]]*\(.*\)" |
| 22 | +} |
| 23 | + |
| 24 | +numeric_frag() { |
| 25 | + expr match "$1" "\([[:digit:]]*\)" |
| 26 | +} |
| 27 | + |
| 28 | +numeric_remainder() { |
| 29 | + expr match "$1" "[[:digit:]]*\(.*\)" |
| 30 | +} |
| 31 | + |
| 32 | +vercomp_debug() { |
| 33 | + OUT="$1" |
| 34 | + #echo "${OUT}" |
| 35 | +} |
| 36 | + |
| 37 | +# return 1 for $1 > $2 |
| 38 | +# return 2 for $1 < $2 |
| 39 | +# return 0 for equal |
| 40 | +vercomp() { |
| 41 | + local WORK1="$1" |
| 42 | + local WORK2="$2" |
| 43 | + local NUM1="", NUM2="", ASCII1="", ASCII2="" |
| 44 | + |
| 45 | + while true; do |
| 46 | + vercomp_debug "ASCII compare" |
| 47 | + ASCII1=`ascii_frag "${WORK1}"` |
| 48 | + ASCII2=`ascii_frag "${WORK2}"` |
| 49 | + WORK1=`ascii_remainder "${WORK1}"` |
| 50 | + WORK2=`ascii_remainder "${WORK2}"` |
| 51 | + |
| 52 | + if [ "${ASCII1}" \> "${ASCII2}" ]; then |
| 53 | + return 1 |
| 54 | + elif [ "${ASCII1}" \< "${ASCII2}" ]; then |
| 55 | + return 2 |
| 56 | + fi |
| 57 | + |
| 58 | + NUM1=`numeric_frag "${WORK1}"` |
| 59 | + NUM2=`numeric_frag "${WORK2}"` |
| 60 | + WORK1=`numeric_remainder "${WORK1}"` |
| 61 | + WORK2=`numeric_remainder "${WORK2}"` |
| 62 | + |
| 63 | + if [ -z "${NUM1}" -a -z "${NUM2}" ]; then |
| 64 | + return 0 |
| 65 | + elif [ -z "${NUM1}" -a -n "${NUM2}" ]; then |
| 66 | + return 2 |
| 67 | + elif [ -n "${NUM1}" -a -z "${NUM2}" ]; then |
| 68 | + return 1 |
| 69 | + fi |
| 70 | + |
| 71 | + if [ "${NUM1}" -gt "${NUM2}" ]; then |
| 72 | + return 1 |
| 73 | + elif [ "${NUM1}" -lt "${NUM2}" ]; then |
| 74 | + return 2 |
| 75 | + fi |
| 76 | + done |
| 77 | +} |
| 78 | +# base snippet end |
| 79 | +#------------------ |
| 80 | +compare_versions() |
| 81 | +{ |
| 82 | + local INPUT_ONE="", INPUT_ONE_VERSION="", INPUT_ONE_SUFFIX="" |
| 83 | + local INPUT_TWO="", INPUT_TWO_VERSION="", INPUT_TWO_SUFFIX="" |
| 84 | + local VERSION_COMPARE=0 |
| 85 | + |
| 86 | + local STABLE_EQ=0 |
| 87 | + local STABLE_ONE=1 |
| 88 | + local STABLE_TWO=2 |
| 89 | + |
| 90 | + local UNSTABLE_EQ=3 |
| 91 | + local UNSTABLE_ONE=4 |
| 92 | + local UNSTABLE_TWO=5 |
| 93 | + |
| 94 | + INPUT_ONE="$1" |
| 95 | + INPUT_TWO="$2" |
| 96 | + |
| 97 | + |
| 98 | + INPUT_ONE_VERSION=$(echo "${INPUT_ONE}" | cut -d'-' -f1) |
| 99 | + if [[ "${INPUT_ONE}" =~ "-" ]]; then |
| 100 | + INPUT_ONE_SUFFIX=$(echo "${INPUT_ONE}" | cut -d'-' -f2) |
| 101 | + fi |
| 102 | + |
| 103 | + INPUT_TWO_VERSION=$(echo "${INPUT_TWO}" | cut -d'-' -f1) |
| 104 | + if [[ "${INPUT_TWO}" =~ "-" ]]; then |
| 105 | + INPUT_TWO_SUFFIX=$(echo "${INPUT_TWO}" | cut -d'-' -f2) |
| 106 | + fi |
| 107 | + |
| 108 | + vercomp "${INPUT_ONE_VERSION}" "${INPUT_TWO_VERSION}" |
| 109 | + |
| 110 | + VERSION_COMPARE=$? |
| 111 | + |
| 112 | + if [ "${VERSION_COMPARE}" = "0" ]; then |
| 113 | + if [ -z "${INPUT_ONE_SUFFIX}" ] && [ -z "${INPUT_TWO_SUFFIX}" ]; then |
| 114 | + return ${STABLE_EQ}; |
| 115 | + elif [ -z "${INPUT_ONE_SUFFIX}" ] && [ -n "${INPUT_TWO_SUFFIX}" ]; then |
| 116 | + return ${STABLE_ONE}; |
| 117 | + elif [ -n "${INPUT_ONE_SUFFIX}" ] && [ -z "${INPUT_TWO_SUFFIX}" ]; then |
| 118 | + return ${STABLE_TWO}; |
| 119 | + elif [ "${INPUT_ONE_SUFFIX}" = "${INPUT_TWO_SUFFIX}" ]; then |
| 120 | + return ${UNSTABLE_EQ}; |
| 121 | + else |
| 122 | + vercomp "${INPUT_ONE_SUFFIX}" "${INPUT_TWO_SUFFIX}" |
| 123 | + return $((UNSTABLE_EQ+$?)) |
| 124 | + fi |
| 125 | + elif [ "${VERSION_COMPARE}" = "1" ]; then |
| 126 | + if [ -z "${INPUT_ONE_SUFFIX}" ]; then |
| 127 | + return ${STABLE_ONE}; |
| 128 | + else |
| 129 | + return ${UNSTABLE_ONE} |
| 130 | + fi |
| 131 | + elif [ "${VERSION_COMPARE}" = "2" ]; then |
| 132 | + if [ -z "${INPUT_TWO_SUFFIX}" ]; then |
| 133 | + return ${STABLE_TWO}; |
| 134 | + else |
| 135 | + return ${UNSTABLE_TWO} |
| 136 | + fi |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +compare_versions $1 $2 |
| 141 | + |
| 142 | +echo $? |
0 commit comments