gaboshlib/g_num_is_between.sh hinzugefügt

This commit is contained in:
olli 2023-10-31 17:30:27 +01:00
parent 44afe2cdcd
commit e1d7651100

View File

@ -0,0 +1,47 @@
function g_num_is_between {
local f_num=$1
local f_between1=$2
local f_between2=$3
# Check for integer (can be done with bash itself)
if [[ ${f_num} =~ ^[0-9]+$ ]] && [[ ${f_between1} =~ ^[0-9]+$ ]] && [[ ${f_between2} =~ ^[0-9]+$ ]]
then
# Check which is the low (from) and the high (to) number
if [ "${f_between1}" -lt "${f_between2}" ]
then
local f_from=${f_between1}
local f_to=${f_between2}
else
local f_from=${f_between2}
local f_to=${f_between1}
fi
# Check if given number is in or out range
if [ ${f_num} -lt ${f_from} ] || [ ${f_num} -gt ${f_to} ]
then
return 1
else
return 0
fi
fi
# Check for valid number
g_num_valid_number "$f_num" "$f_between1" "$f_between2" || return 1
# Check which is the low (from) and the high (to) number
if [ $(echo "${f_between1} < ${f_between2}" | bc -l) -ne 0 ]
then
local f_from=${f_between1}
local f_to=${f_between2}
else
local f_from=${f_between2}
local f_to=${f_between1}
fi
# Check if given number is in or out range
if [ $(echo "${f_num} < ${f_from}" | bc -l) -ne 0 ] || [ $(echo "${f_num} > ${f_to}" | bc -l) -ne 0 ]
then
return 1
else
return 0
fi
}