UpdateController/general_utils

84 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-06-06 18:59:31 +02:00
#! /bin/bash -
2022-06-04 14:35:12 +02:00
# set -x
source ./log_helpers
2022-06-02 17:26:55 +02:00
if [ "${general_utils_sourced:-1}" = "1" ]; then # include only once
readonly general_utils_sourced=${BASH_SOURCE[0]}
2022-06-04 14:35:12 +02:00
2022-06-03 21:44:41 +02:00
# compare two strings
equal () {
case "$1" in
"$2")
return 0
;;
esac
return 1 # they don't match
}
# check if second string is contained in first string
contains () {
if grep -qE "$2" <<< $1; then
return 0
fi
return 1 # not contained
}
2022-06-02 17:26:55 +02:00
exec_process_substitution () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
2022-06-04 18:10:55 +02:00
2022-06-02 17:26:55 +02:00
exec {fd}< <(eval "$@")
2022-06-04 14:35:12 +02:00
2022-06-02 17:26:55 +02:00
local __result_code=$?
local ps_pid=$! # remember pid of process substitution
local __result=""
2022-06-06 18:59:31 +02:00
2022-06-02 17:26:55 +02:00
while read __tmp <&$fd; do
if ! [ -z "$__tmp" ]; then
2022-06-06 18:59:31 +02:00
__result+=" $__tmp"
2022-06-02 17:26:55 +02:00
fi
done
2022-06-06 18:59:31 +02:00
2022-06-02 17:26:55 +02:00
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
log_debug "$func:${LINENO} result=$__result"
printf '%s' "$__result"
return $__result_code
}
2022-06-06 18:59:31 +02:00
# exec_process_substitution 'opkg --noaction list'
2022-06-02 17:26:55 +02:00
usage () {
echo "Usage: $PROGRAM [--file config] [--?] [--help] [--version] [--dbg]"
# UpdateController.conf"
}
usage_and_exit () {
usage
exit $1
}
version () {
echo "$PROGRAM version $VERSION"
}
error () {
echo "$@" 1>&2
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