tee and exit status

前端 未结 8 1876
礼貌的吻别
礼貌的吻别 2021-02-07 10:43

is there an alternative to \"tee\" which captures STDOUT/STDERR of the command being executed and exits with the same exit status as the processed command. Something as followin

8条回答
  •  独厮守ぢ
    2021-02-07 11:26

    Here's an eet. Works with every Bash I can get my hands on, from 2.05b to 4.0.

    #!/bin/bash
    tee_args=()
    while [[ $# > 0 && $1 != -- ]]; do
        tee_args=("${tee_args[@]}" "$1")
        shift
    done
    shift
    # now ${tee_args[*]} has the arguments before --,
    # and $* has the arguments after --
    
    # redirect standard out through a pipe to tee
    exec | tee "${tee_args[@]}"
    
    # do the *real* exec of the desired program
    exec "$@"
    

    (pipefail and $PIPESTATUS are nice, but I recall them being introduced in 3.1 or thereabouts.)

提交回复
热议问题