### Command execution
## Execute command, possibly logging its execution.
#
# cmd $cmd...
#
# Fails if the command returns an error exit code.
function cmd {
log_cmd "$@"
"$@" || die "Failed: $@"
}
## Execute command as a test, logging its execution at log_cmd
#
# cmd_test $cmd... && ... || ...
#
# Fails if the command returns an error exit code.
function cmd_test {
log_cmd "$@"
"$@"
}
## Execute command, prefixing its output on stdout with given indent prefix.
#
# indent " " $cmd...
#
# Output is kept on stdout, exit status is that of the given command.
function cmd_indent () {
local indent="$1"; shift
"$@" | sed "s/^/$indent/"
return ${PIPESTATUS[0]}
}
## Execute a command as root, using sudo if required.
function cmd_sudo {
if [ $UID -eq 0 ]; then
cmd "$@"
else
cmd sudo "$@"
fi
}