Files
gaboshlib/gaboshlib/g_calc.sh
2024-06-25 12:10:19 +02:00

35 lines
919 B
Bash

function g_calc {
# function for calcul
# $g_fd_bc_in $fd_bc_out have to be global
unset g_calc_result
# check if bc is already running
if [[ -z "$g_fd_bc_in" || -z "$g_fd_bc_out" ]]
then
local bc_input="$g_tmp/$$-g_bc_input"
local bc_output="$g_tmp/$$-g_bc_output"
# create fifo pipe
[ -p "$bc_input" ] || mkfifo "$bc_input"
[ -p "$bc_output" ] || mkfifo "$bc_output"
# run bc in background und switch i/o to pipes
bc -ql < "$bc_input" > "$bc_output" 2>&1 &
# store in filedescriptiors
exec {g_fd_bc_in}> "$bc_input"
exec {g_fd_bc_out}< "$bc_output"
fi
# send calculation and read result
echo "$1" >&${g_fd_bc_in}
read -u ${g_fd_bc_out} g_calc_result
g_calc_result=${g_calc_result//-./-0.}
g_calc_result=${g_calc_result//./0.}
if ! g_num_valid_number $g_calc_result
then
echo "${FUNCNAME} $@" 1>&2
unset g_calc_result
return 1
fi
}