lib/pvl/cmd.sh
author Tero Marttila <terom@paivola.fi>
Mon, 09 Mar 2015 21:17:06 +0200
changeset 736 75938aa0390b
parent 709 4e0450dc57a9
permissions -rw-r--r--
pvl.hosts.interfaces: remove junos-specifics
### 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
}