UpdateController/general_utils

127 lines
2.7 KiB
Plaintext
Raw Permalink 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
}
2022-06-05 21:41:22 +02:00
assert_s () {
# if [ -z ${!1} ]; then
# log_fatal "$1 not set"
# fi
2022-06-16 15:48:11 +02:00
# log_debug "$1=${!1}"
:
2022-06-05 21:41:22 +02:00
}
assert_d () {
# if [ ! -d ${!1} ]; then
# log_fatal "$1 does not exist"
# fi
2022-06-16 15:48:11 +02:00
# log_debug "$1=${!1}"
:
2022-06-05 21:41:22 +02:00
}
assert_f () {
# if [ ! -f ${!1} ]; then
# log_fatal "$1 does not exist"
# fi
2022-06-16 15:48:11 +02:00
#log_debug "$1=${!1}"
:
2022-06-05 21:41:22 +02:00
}
assert_a () {
local readonly __m="${1}[@]"
local readonly __n=(${!__m})
local __len=${#__n[@]}
# if [ $__len -eq 0 ]; then
# log_fatal "$1 not set"
# fi
2022-06-16 15:48:11 +02:00
# log_debug "$1=$__n"
:
2022-06-05 21:41:22 +02:00
}
empty_file () {
if [[ -f "$1" ]]; then
echo -n "" > $1
else
log_error "$1 does not exist"
fi
}
2022-06-02 17:26:55 +02:00
fi