Add err()/err_if_not_set()

* err() and err_if_not_set() do error-like reporting without aborting the script
* die_if_not_set() now properly dies
* add is_running() from Grenade

Change-Id: I38b88112415a3c07e35bbc2dc65ad839c4d63fce
This commit is contained in:
Dean Troyer 2013-04-05 15:02:01 -05:00
parent 55a08ad2a8
commit 896eb666e8

View File

@ -57,15 +57,12 @@ function cp_it {
# die $LINENO "message"
function die() {
local exitcode=$?
set +o xtrace
local line=$1; shift
if [ $exitcode == 0 ]; then
exitcode=1
fi
set +o xtrace
local msg="[ERROR] $0:$1 $2"
echo $msg 1>&2;
if [[ -n ${SCREEN_LOGDIR} ]]; then
echo $msg >> "${SCREEN_LOGDIR}/error.log"
fi
err $line "$*"
exit $exitcode
}
@ -75,14 +72,49 @@ function die() {
# NOTE: env-var is the variable name without a '$'
# die_if_not_set $LINENO env-var "message"
function die_if_not_set() {
(
local exitcode=$?
FXTRACE=$(set +o | grep xtrace)
set +o xtrace
local evar=$2; shift
local line=$1; shift
local evar=$1; shift
if ! is_set $evar || [ $exitcode != 0 ]; then
die $@
die $line "$*"
fi
)
$FXTRACE
}
# Prints line number and "message" in error format
# err $LINENO "message"
function err() {
local exitcode=$?
errXTRACE=$(set +o | grep xtrace)
set +o xtrace
local msg="[ERROR] $0:$1 $2"
echo $msg 1>&2;
if [[ -n ${SCREEN_LOGDIR} ]]; then
echo $msg >> "${SCREEN_LOGDIR}/error.log"
fi
$errXTRACE
return $exitcode
}
# Checks an environment variable is not set or has length 0 OR if the
# exit code is non-zero and prints "message"
# NOTE: env-var is the variable name without a '$'
# err_if_not_set $LINENO env-var "message"
function err_if_not_set() {
local exitcode=$?
errinsXTRACE=$(set +o | grep xtrace)
set +o xtrace
local line=$1; shift
local evar=$1; shift
if ! is_set $evar || [ $exitcode != 0 ]; then
err $line "$*"
fi
$errinsXTRACE
return $exitcode
}
@ -538,6 +570,7 @@ function inicomment() {
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
}
# Uncomment an option in an INI file
# iniuncomment config-file section option
function iniuncomment() {
@ -559,6 +592,7 @@ function iniget() {
echo ${line#*=}
}
# Determinate is the given option present in the INI file
# ini_has_option config-file section option
function ini_has_option() {
@ -570,6 +604,7 @@ function ini_has_option() {
[ -n "$line" ]
}
# Set an option in an INI file
# iniset config-file section option value
function iniset() {
@ -592,6 +627,7 @@ $option = $value
fi
}
# Get a multiple line option from an INI file
# iniget_multiline config-file section option
function iniget_multiline() {
@ -603,6 +639,7 @@ function iniget_multiline() {
echo ${values}
}
# Set a multiple line option in an INI file
# iniset_multiline config-file section option value1 value2 valu3 ...
function iniset_multiline() {
@ -632,6 +669,7 @@ $option = $v
done
}
# Append a new option in an ini file without replacing the old value
# iniadd config-file section option value1 value2 value3 ...
function iniadd() {
@ -643,6 +681,17 @@ function iniadd() {
iniset_multiline $file $section $option $values
}
# Find out if a process exists by partial name.
# is_running name
function is_running() {
local name=$1
ps auxw | grep -v grep | grep ${name} > /dev/null
RC=$?
# some times I really hate bash reverse binary logic
return $RC
}
# is_service_enabled() checks if the service(s) specified as arguments are
# enabled by the user in ``ENABLED_SERVICES``.
#