From 2454b86e5ee2bc1e05c754daf56a7ec27383c886 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Thu, 2 Jun 2022 17:26:55 +0200 Subject: [PATCH] source general_utils only once --- general_utils | 82 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/general_utils b/general_utils index 88531d2..ae39f21 100755 --- a/general_utils +++ b/general_utils @@ -3,47 +3,61 @@ source ./log_helpers -exec_process_substitution () { - local func="${FUNCNAME[0]}" - log_debug "$func:${LINENO} exec-ing [$*]" +if [ "${general_utils_sourced:-1}" = "1" ]; then # include only once + readonly general_utils_sourced=${BASH_SOURCE[0]} - exec {fd}< <(eval "$@") + exec_process_substitution () { + local func="${FUNCNAME[0]}" + log_debug "$func:${LINENO} exec-ing [$*]" - local __result_code=$? - local ps_pid=$! # remember pid of process substitution + exec {fd}< <(eval "$@") - local __result="" - while read __tmp <&$fd; do - if ! [ -z "$__tmp" ]; then - __result="${__result}$__tmp" - fi - done + local __result_code=$? + local ps_pid=$! # remember pid of process substitution + + local __result="" + while read __tmp <&$fd; do + if ! [ -z "$__tmp" ]; then + __result="${__result}$__tmp" + fi + done - exec {fd}>&- # close fd (i.e. process substitution) - wait $ps_pid # wait for the subshell to finish + exec {fd}>&- # close fd (i.e. process substitution) + 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" - printf '%s' "$__result" - return $__result_code -} + log_debug "$func:${LINENO} result=$__result" + printf '%s' "$__result" + return $__result_code + } -usage () { - echo "Usage: $PROGRAM [--file config] [--?] [--help] [--version] [--dbg]" - # UpdateController.conf" -} + usage () { + echo "Usage: $PROGRAM [--file config] [--?] [--help] [--version] [--dbg]" + # UpdateController.conf" + } -usage_and_exit () { - usage - exit $1 -} + usage_and_exit () { + usage + exit $1 + } -version () { - echo "$PROGRAM version $VERSION" -} + version () { + echo "$PROGRAM version $VERSION" + } -error () { - echo "$@" 1>&2 - usage_and_exit 1 -} + error () { + echo "$@" 1>&2 + usage_and_exit 1 + } + + alert () { + # usage: alert <$?> + if [ "$1" ne 0 ]; then + echo "WARNING: $2 did not complete successfully." >&2 + exit 1 + else + echo "INFO: $2 completed successfully" >&2 + fi + } +fi