gaboshlib/g_calc.sh aktualisiert

This commit is contained in:
olli 2023-11-06 17:59:12 +01:00
parent fa408ca67f
commit bf4c178ea0

View File

@ -1,34 +1,37 @@
function g_calc {
# Use bc in backround for multiple bc's running much faster
if [ -z "${g_bc_running}" ]
then
[ -p ${g_tmp}/bc-in ] || mkfifo ${g_tmp}/bc-in
[ -p ${g_tmp}/bc-out ] || mkfifo ${g_tmp}/bc-out
[ -p ${g_tmp}/bc-sed-in ] || mkfifo ${g_tmp}/bc-sed-in
[ -p ${g_tmp}/bc-sed-out ] || mkfifo ${g_tmp}/bc-sed-out
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
# bc stream channel
bc -ql <${g_tmp}/bc-in >${g_tmp}/bc-out &
exec 3>${g_tmp}/bc-in 4<${g_tmp}/bc-out
bc -ql <${g_tmp}/${g_rnd}/bc-in >${g_tmp}/${g_rnd}/bc-out &
exec 3>${g_tmp}/${g_rnd}/bc-in 4<${g_tmp}/${g_rnd}/bc-out
# sed stream channel
sed -su 's/^\./0./; s/^-\./-0./' <${g_tmp}/bc-sed-in >${g_tmp}/bc-sed-out &
exec 5>${g_tmp}/bc-sed-in 6<${g_tmp}/bc-sed-out
sed -su 's/^\./0./; s/^-\./-0./' <${g_tmp}/${g_rnd}/bc-sed-in >${g_tmp}/${g_rnd}/bc-sed-out &
exec 5>${g_tmp}/${g_rnd}/bc-sed-in 6<${g_tmp}/${g_rnd}/bc-sed-out
g_bc_running="true"
fi 2>/dev/null
# do bc
echo "scale=8; $@" >&3
local g_bc_out
read g_bc_out <&4
# do sed
echo ${g_bc_out} >&5
local g_sed_out
read g_sed_out <&6
# echo result
echo ${g_sed_out}
}