From c2b89df1343a905a1aff7625c74d08a05aa12fd5 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Tue, 7 Mar 2023 12:51:55 -0800 Subject: [PATCH] v1.1.3, added IP validation and version compare functions --- functions | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/functions b/functions index 996caaf..85351cc 100644 --- a/functions +++ b/functions @@ -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