aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/fnl.bash
blob: b63d64b1d8939b1a32db296fd1a9172a8652a271 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
# Run a command and push its stdout and stderr into temporary files, printing
# the names of the files once done, and saving them into two variables. Return
# the exit status of the command.
#
#     $ fnl grep foo /bar
#     declare -p fnl_stdout="/tmp/fnl.xQmhe/stdout"
#     declare -p fnl_stderr="/tmp/fnl.xQmhe/stderr"
#
fnl() {

    # Must be called with at least one command argument
    if ! (($#)) ; then
        printf 'bash: %s: usage: %s COMMAND [ARG1 ...]\n' \
            "$FUNCNAME" "$FUNCNAME" >&2
        return 2
    fi

    # Try to stop infinitely recursive calls
    if [[ $1 == "$FUNCNAME" ]] ; then
        printf 'bash: %s: Cannot nest calls\n' \
            "$FUNCNAME" >&2
        return 2
    fi

    # Create a temporary directory or bail
    local dirname template
    template=$FUNCNAME.$1.XXXXXX
    if ! dirname=$(mktemp -dt -- "$template") ; then
        return
    fi

    # Run the command and save its exit status
    local ret
    "$@" >"$dirname"/stdout 2>"$dirname"/stderr
    ret=$?

    # Note these are *not* local variables
    # shellcheck disable=SC2034
    fnl_stdout=$dirname/stdout fnl_stderr=$dirname/stderr
    declare -p fnl_std{out,err}

    # Return the exit status of the command, not the declare builtin
    return "$ret"
}