File: quiet

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (32 lines) | stat: -rwxr-xr-x 720 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash

# use "quiet foo" when you expect "foo" to produce a lot of output
# that isn't useful unless foo itself fails.
quiet() (
    # note this is a subshell (parens instead of braces around the function)
    # so this set only affects this function and not the caller
    { set +x; } >&/dev/null

    # not strictly needed because it's a subshell, but good practice
    local tf retval

    tf="$(mktemp)"

    set +e
    "$@" >& "$tf"
    retval=$?
    set -e

    if [ "$retval" != "0" ]; then
        echo "quiet: $*" >&2
        echo "quiet: exit status $retval. Output follows:" >&2
        cat "$tf" >&2
        echo "quiet: end of output." >&2
    fi

    rm -f -- "$tf"

    return $retval
)

quiet "$@"