shoestrap/assets/default/dotfiles/bash_funcs

61 lines
1.3 KiB
Bash
Raw Normal View History

#! /bin/bash
# Version compare based on
# http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
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
}
vertest() {
local ver1=($1) op=($2) ver2=($3)
vercomp $ver1 $ver2
case $op in
'=')
[ $? -eq 0 ] && return 0 || return 1
;;
'!=')
[ $? -ne 0 ] && return 0 || return 1
;;
'<')
[ $? -eq 2 ] && return 0 || return 1
;;
'<=')
[ $? -eq 2 -o $? -eq 0 ] && return 0 || return 1
;;
'>')
[ $? -eq 1 ] && return 0 || return 1
;;
'>=')
[ $? -eq 1 -o $? -eq 0 ] && return 0 || return 1
;;
esac
return -1
}