Skip to content

Instantly share code, notes, and snippets.

@honzahommer
Last active January 28, 2019 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzahommer/fb56db7192cc3d2dd7161d5ed121307c to your computer and use it in GitHub Desktop.
Save honzahommer/fb56db7192cc3d2dd7161d5ed121307c to your computer and use it in GitHub Desktop.
isversion.sh
#!/usr/bin/env bash
isversion() {
if [[ $# -lt 2 ]] ; then
local opd='`=`'
local ops='`<` or `>` or `=`'
echo "usage: $0 <versions> <versions> [operator=$opd]"
echo -e "\tversions - Versions to compare"
echo -e "\toperator - Operator to compare, $ops. Default to $opd."
exit 1
fi
local arg1=$(echo $1 | cut -d'(' -f1)
local arg2=$(echo $2 | cut -d'(' -f1)
local comp=${3:-=}
local ret=0
local op=-1
if [ "$arg1" != "$arg2" ]; then
local i
local IFS=.
local ver1=($arg1)
local ver2=($arg2)
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ $ret = 0 ]]; then
if [[ -z ${ver2[i]} ]]; then
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]})); then
ret=1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
ret=2
fi
fi
done
fi
case $ret in
0) op='=';;
1) op='>';;
2) op='<';;
esac
[[ $op != $comp ]] && return 1
return 0
}
if [[ $# -ne 0 ]] ; then
isversion $@
exit $?
fi
#!/usr/bin/env bash
this=$(basename $0)
func=${this%".test.sh"}
file="$(dirname $0)/$func.sh"
if [ ! -f "$file" ]; then
echo "$file: File not found"
exit 1
fi
\. "$file"
if [[ $(type -t $func) != function ]]; then
echo "$func: Function not found"
exit 0
fi
while read -r test; do
$func $test || exit 1
done << EOF
1 1 =
2.1 2.2 <
3.0.4.10 3.0.4.2 >
4.08 4.08.01 <
3.2.1.9.8144 3.2 >
3.2 3.2.1.9.8144 <
1.2 2.1 <
2.1 1.2 >
5.6.7 5.6.7 =
1.01.1 1.1.1 =
1.1.1 1.01.1 =
1 1.0 =
1.0 1 =
1.0.2.0 1.0.2 =
1..0 1.0 =
1.0 1..0 =
5.0 5.0.0(1)-release =
EOF
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment