lib/pvl/cmd.sh
author Tero Marttila <tero.marttila@aalto.fi>
Tue, 03 Mar 2015 11:39:07 +0200
changeset 709 4e0450dc57a9
parent 627 a81206440be2
permissions -rw-r--r--
lib/pvl/cmd: cmd_sudo
### 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
}