gaboshlib/g_num_is_between.sh aktualisiert

This commit is contained in:
olli 2023-11-07 13:02:12 +01:00
parent 0702ecfeb5
commit 3e981f64dd

View File

@ -1,47 +1,54 @@
function g_num_is_between { function g_calc {
local f_num=$1 # Use bc in backround for multiple bc's running much faster
local f_between1=$2 if [ -z "${g_bc_running}" ]
local f_between2=$3 then
g_rnd=$RANDOM
mkdir -p ${g_tmp}/${g_rnd}
[ -p ${g_tmp}/${g_rnd}/bc-in ] || mkfifo ${g_tmp}/${g_rnd}/bc-in
[ -p ${g_tmp}/${g_rnd}/bc-out ] || mkfifo ${g_tmp}/${g_rnd}/bc-out
[ -p ${g_tmp}/${g_rnd}/bc-sed-in ] || mkfifo ${g_tmp}/${g_rnd}/bc-sed-in
[ -p ${g_tmp}/${g_rnd}/bc-sed-out ] || mkfifo ${g_tmp}/${g_rnd}/bc-sed-out
# Check for integer (can be done with bash itself) # bc stream channel
if [[ ${f_num} =~ ^[0-9]+$ ]] && [[ ${f_between1} =~ ^[0-9]+$ ]] && [[ ${f_between2} =~ ^[0-9]+$ ]] { bc -ql <${g_tmp}/${g_rnd}/bc-in 2>&1 >${g_tmp}/${g_rnd}/bc-out & } 2>/dev/null
then exec 3>${g_tmp}/${g_rnd}/bc-in 4<${g_tmp}/${g_rnd}/bc-out
# Check which is the low (from) and the high (to) number
if [ "${f_between1}" -lt "${f_between2}" ] # sed stream channel
then { sed -su 's/^\./0./; s/^-\./-0./' <${g_tmp}/${g_rnd}/bc-sed-in >${g_tmp}/${g_rnd}/bc-sed-out & } 2>/dev/null
local f_from=${f_between1} exec 5>${g_tmp}/${g_rnd}/bc-sed-in 6<${g_tmp}/${g_rnd}/bc-sed-out
local f_to=${f_between2}
else g_bc_running="true"
local f_from=${f_between2}
local f_to=${f_between1}
fi fi
# Check if given number is in or out range
if [ ${f_num} -lt ${f_from} ] || [ ${f_num} -gt ${f_to} ] # do bc
echo "scale=8; $@" >&3
local g_bc_out
if ! read -t 0.1 g_bc_out <&4
then then
g_traceback "$@"
( exec 3<&- ) 2>/dev/null
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
unset g_bc_running
return 1 return 1
else
return 0
fi
fi fi
# Check for valid number # do sed
g_num_valid_number "$f_num" "$f_between1" "$f_between2" || return 1 echo ${g_bc_out} >&5
local g_sed_out
# Check which is the low (from) and the high (to) number if ! read -t 0.1 g_sed_out <&6
if [ $(g_calc "${f_between1} < ${f_between2}") -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 [ $(g_calc "${f_num} < ${f_from}") -ne 0 ] || [ $(g_calc "${f_num} > ${f_to}") -ne 0 ]
then then
g_traceback "$@"
( exec 3<&- ) 2>/dev/null
( exec 4<&- ) 2>/dev/null
( exec 5<&- ) 2>/dev/null
( exec 6<&- ) 2>/dev/null
unset g_bc_running
return 1 return 1
else
return 0
fi fi
# echo result
echo ${g_sed_out}
} }