v1.1.3, added IP validation and version compare functions

This commit is contained in:
pyr0ball 2023-03-07 12:51:55 -08:00
parent 83c236c74c
commit c2b89df134

View file

@ -2,12 +2,13 @@
# pyr0ball script functions library
# Initial Vars
functionsrev=1.1.2
functionsrev=1.1.3
scriptname="${0##*/}"
rundir="${0%/*}"
rundir_absolute=$(cd `dirname $0` && pwd)
runuser="$(whoami)"
pretty_date="$(date +%Y-%m-%d_%H-%M-%S)"
short_date="$(date +%Y-%m-%d)"
# Escape characters (if your shell uses a different one, you can modify it here)
# By default this is using the usual bash escape code
@ -794,4 +795,75 @@ multiselect(){
# ((idx++))
#done
### IP validation function
valid-ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# Version comparison function
# usage:
# vercomp [first version] [second version]
# output:
# 0 =
# 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
}
compare-versions() {
vercomp $1 $2
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [[ $op != $3 ]]
then
echo "Fail: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"
else
echo "Pass: '$1 $op $2'"
fi
}
set-boxtype