UpdateController/general_utils

127 lines
2.7 KiB
Bash
Executable File

#! /bin/bash -
# set -x
source ./log_helpers
if [ "${general_utils_sourced:-1}" = "1" ]; then # include only once
readonly general_utils_sourced=${BASH_SOURCE[0]}
# 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
}
exec_process_substitution () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
exec {fd}< <(eval "$@")
local __result_code=$?
local ps_pid=$! # remember pid of process substitution
local __result=""
while read __tmp <&$fd; do
if ! [ -z "$__tmp" ]; then
__result+=" $__tmp"
fi
done
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
}
# exec_process_substitution 'opkg --noaction list'
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
}
assert_s () {
# if [ -z ${!1} ]; then
# log_fatal "$1 not set"
# fi
# log_debug "$1=${!1}"
:
}
assert_d () {
# if [ ! -d ${!1} ]; then
# log_fatal "$1 does not exist"
# fi
# log_debug "$1=${!1}"
:
}
assert_f () {
# if [ ! -f ${!1} ]; then
# log_fatal "$1 does not exist"
# fi
#log_debug "$1=${!1}"
:
}
assert_a () {
local readonly __m="${1}[@]"
local readonly __n=(${!__m})
local __len=${#__n[@]}
# if [ $__len -eq 0 ]; then
# log_fatal "$1 not set"
# fi
# log_debug "$1=$__n"
:
}
empty_file () {
if [[ -f "$1" ]]; then
echo -n "" > $1
else
log_error "$1 does not exist"
fi
}
fi