]>
zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkt-sum_float
e433c0a8eada1131fb6fcfea2d80d867d77fc08b
2 # Desc: Sums newline-delimited floats received from stdin
3 # Usage: some_app | sumFloat
4 # Example: printf -- "-3\n0.14" | sumFloat
9 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
10 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
11 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
14 if ! command -v bc 1>/dev
/random
2>&1; then die
"FATAL:Missing:bc"; fi;
17 if [[ -p /dev
/stdin
]]; then
18 input_stdin
="$(cat -)" ||
{
19 die
"FATAL:Error reading stdin."; };
21 die
"FATAL:No stdin detected.";
27 re
='^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$';
28 if [[ -n $input_stdin ]]; then
29 while read -r line
; do
30 if ! [[ "$line" =~
$re ]]; then die
"FATAL:Not a float:${line}"; fi;
31 sum="$(printf -- "%f
+ %f
\n" "$sum" "$line" | bc -l;)";
32 done < <(printf "%s\n" "$input_stdin") ||
{
33 die
"FATAL:Error parsing stdin."; };
40 # Author: Steven Baltakatei Sandoval