source general_utils only once

This commit is contained in:
Gerhard Hoffmann 2022-06-02 17:26:55 +02:00
parent 7d9fc7cb23
commit 2454b86e5e

View File

@ -3,47 +3,61 @@
source ./log_helpers source ./log_helpers
exec_process_substitution () { if [ "${general_utils_sourced:-1}" = "1" ]; then # include only once
local func="${FUNCNAME[0]}" readonly general_utils_sourced=${BASH_SOURCE[0]}
log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@") exec_process_substitution () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
local __result_code=$? exec {fd}< <(eval "$@")
local ps_pid=$! # remember pid of process substitution
local __result="" local __result_code=$?
while read __tmp <&$fd; do local ps_pid=$! # remember pid of process substitution
if ! [ -z "$__tmp" ]; then
__result="${__result}$__tmp" local __result=""
fi while read __tmp <&$fd; do
done if ! [ -z "$__tmp" ]; then
__result="${__result}$__tmp"
fi
done
exec {fd}>&- # close fd (i.e. process substitution) exec {fd}>&- # close fd (i.e. process substitution)
wait $ps_pid # wait for the subshell to finish wait $ps_pid # wait for the subshell to finish
__result=${__result//[$'\r\n\t']/ } # remove \r\n\t from __result __result=${__result//[$'\r\n\t']/ } # remove \r\n\t from __result
log_debug "$func:${LINENO} result=$__result" log_debug "$func:${LINENO} result=$__result"
printf '%s' "$__result" printf '%s' "$__result"
return $__result_code return $__result_code
} }
usage () { usage () {
echo "Usage: $PROGRAM [--file config] [--?] [--help] [--version] [--dbg]" echo "Usage: $PROGRAM [--file config] [--?] [--help] [--version] [--dbg]"
# UpdateController.conf" # UpdateController.conf"
} }
usage_and_exit () { usage_and_exit () {
usage usage
exit $1 exit $1
} }
version () { version () {
echo "$PROGRAM version $VERSION" echo "$PROGRAM version $VERSION"
} }
error () { error () {
echo "$@" 1>&2 echo "$@" 1>&2
usage_and_exit 1 usage_and_exit 1
} }
alert () {
# usage: alert <$?> <object>
if [ "$1" ne 0 ]; then
echo "WARNING: $2 did not complete successfully." >&2
exit 1
else
echo "INFO: $2 completed successfully" >&2
fi
}
fi