История изменений
Исправление vodz, (текущая версия) :
Взгруснулось. Аж на комментарии хватило энтузиазма.
#!/usr/bin/env bash
# compare version from $1 and $2
# parse the format "(programm_name[- ])?[[:num:]+](\.[[:num:]]+)*(-suffix)?"
# ignore comparing a programm name and a suffix
# returns: 0 - $1=$2, 1 - $1>$2, 2 - $1<$2,
# 3 - $1 format error, 4 - $2 format error
#
# Copyright (c) 2019 may safely be consumed by a BSD or GPL license.
# Written by: Vladimir Oleynik <dzo@simtreas.ru>
vercmp() {
local -a va1 va2
local ps='^(..*[- ])?([0-9][0-9]*(.[0-9][0-9]*)*)(-..*)?$' IFS=.
# test the format and remove if present programm_name[- ] and -suffix
[[ $1 =~ $ps ]] || return 3
# set array=(ver_major ver_minor ver_patch...)
va1=(${BASH_REMATCH[2]})
# for $2 too
[[ $2 =~ $ps ]] || return 4
va2=(${BASH_REMATCH[2]})
local -i i=0 c1 c2
while [[ "${va1[i]}${va2[i]}" ]]; do
# Removing leading zeroes, set empty as 0
c1=$((10#${va1[i]}))
c2=$((10#${va2[i]}))
# compare a version item
[[ c1 -lt c2 ]] && return 2
[[ c1 -ne c2 ]] && return 1
i=i+1
done
return 0
}
# testing
tests() {
vercmp "$1" "$2"
echo "'$1' <=> '$2' $? returned"
}
tests 1 0
tests 0 1
tests 1 1
tests 1.1 0
tests 0 1.1
tests 1 1.1
tests 1.1 1.1
tests 1.1 1
tests 1.2
tests a-1.3 b-1.3-suff
tests a-1.3 a-1.25
tests "b0 "
tests "c0 1" "c0 0.1"
tests 1.2.8-suff 1.2.010-suff
tests "p 1" "x 1.0"
tests "p 1." "x 1.0"
tests "p.1." "x.1.0"
tests "p.1" "x.1.0"
Исправление vodz, :
Взгруснулось. Аж на комментарии хватило энтузиазма.
#!/usr/bin/env bash
# compare version from $1 and $2
# parse the format "(programm_name[- ])?[[:num:]+](\.[[:num:]]+)*(-suffix)*"
# ignore comparing a programm name and a suffix
# returns: 0 - $1=$2, 1 - $1>$2, 2 - $1<$2,
# 3 - $1 format error, 4 - $2 format error
#
# Copyright (c) 2019 may safely be consumed by a BSD or GPL license.
# Written by: Vladimir Oleynik <dzo@simtreas.ru>
vercmp() {
local -a va1 va2
local ps='^(..*[- ])?([0-9][0-9]*(.[0-9][0-9]*)*)(-..*)?$' IFS=.
# test the format and remove if present programm_name[- ] and -suffix
[[ $1 =~ $ps ]] || return 3
# set array=(ver_major ver_minor ver_patch...)
va1=(${BASH_REMATCH[2]})
# for $2 too
[[ $2 =~ $ps ]] || return 4
va2=(${BASH_REMATCH[2]})
local -i i=0 c1 c2
while [[ "${va1[i]}${va2[i]}" ]]; do
# Removing leading zeroes, set empty as 0
c1=$((10#${va1[i]}))
c2=$((10#${va2[i]}))
# compare a version item
[[ c1 -lt c2 ]] && return 2
[[ c1 -ne c2 ]] && return 1
i=i+1
done
return 0
}
# testing
tests() {
vercmp "$1" "$2"
echo "'$1' <=> '$2' $? returned"
}
tests 1 0
tests 0 1
tests 1 1
tests 1.1 0
tests 0 1.1
tests 1 1.1
tests 1.1 1.1
tests 1.1 1
tests 1.2
tests a-1.3 b-1.3-suff
tests a-1.3 a-1.25
tests "b0 "
tests "c0 1" "c0 0.1"
tests 1.2.8-suff 1.2.010-suff
tests "p 1" "x 1.0"
tests "p 1." "x 1.0"
tests "p.1." "x.1.0"
tests "p.1" "x.1.0"
Исходная версия vodz, :
Взгруснулось. Аж на комментарии хватило энтузиазма.
#!/usr/bin/env bash
# compare version from $1 and $2
# parse the format "(programm_name[- ])?[[:num:]+](\.[[:num:]]+)*(-suffix)*"
# ignore comparing a programm name and a suffix
# returns: 0 - $1=$2, 1 - $1>$2, 2 - $1<$2,
# 3 - $1 format error, 4 - $2 format error
#
# Copyright (c) 2019 may safely be consumed by a BSD or GPL license.
# Written by: Vladimir Oleynik <dzo@simtreas.ru>
vercmp() {
local -a va1 va2
local ps='^(..*[- ])?([0-9][0-9]*(.[0-9][0-9]*)*)(-..*)?$' IFS=.
# test the format and remove if present programm_name[- ] and -suffix
[[ $1 =~ $ps ]] || return 3
# set array=(ver_major ver_minor ver_path...)
va1=(${BASH_REMATCH[2]})
# for $2 too
[[ $2 =~ $ps ]] || return 4
va2=(${BASH_REMATCH[2]})
local -i i=0 c1 c2
while [[ "${va1[i]}${va2[i]}" ]]; do
# Removing leading zeroes, set empty as 0
c1=$((10#${va1[i]}))
c2=$((10#${va2[i]}))
# compare a version item
[[ c1 -lt c2 ]] && return 2
[[ c1 -ne c2 ]] && return 1
i=i+1
done
return 0
}
# testing
tests() {
vercmp "$1" "$2"
echo "'$1' <=> '$2' $? returned"
}
tests 1 0
tests 0 1
tests 1 1
tests 1.1 0
tests 0 1.1
tests 1 1.1
tests 1.1 1.1
tests 1.1 1
tests 1.2
tests a-1.3 b-1.3-suff
tests a-1.3 a-1.25
tests "b0 "
tests "c0 1" "c0 0.1"
tests 1.2.8-suff 1.2.010-suff
tests "p 1" "x 1.0"
tests "p 1." "x 1.0"
tests "p.1." "x.1.0"
tests "p.1" "x.1.0"