revert gaboshlib/g_num_is_between.sh aktualisiert
This commit is contained in:
olli 2023-11-07 13:19:09 +01:00
parent c335791caa
commit 5a06c3364b

View File

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