How do I write stderr to a file while using “tee” with a pipe?

前端 未结 10 948
独厮守ぢ
独厮守ぢ 2020-11-22 08:01

I know how to use tee to write the output (STDOUT) of aaa.sh to bbb.out, while still displaying it in the terminal:

10条回答
  •  终归单人心
    2020-11-22 08:41

    Like the accepted answer well explained by lhunath, you can use

    command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)
    

    Beware than if you use bash you could have some issue.

    Let me take the matthew-wilcoxson exemple.

    And for those who "seeing is believing", a quick test:

    (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)
    

    Personally, when I try, I have this result :

    user@computer:~$ (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)
    user@computer:~$ Test Out
    Test Err
    
    

    Both message does not appear at the same level. Why Test Out seem to be put like if it is my previous command ?
    Prompt is on a blank line, let me think the process is not finished, and when I press Enter this fix it.
    When I check the content of the files, it is ok, redirection works.

    Let take another test.

    function outerr() {
      echo "out"     # stdout
      echo >&2 "err" # stderr
    }
    
    user@computer:~$ outerr
    out
    err
    
    user@computer:~$ outerr >/dev/null
    err
    
    user@computer:~$ outerr 2>/dev/null
    out
    

    Trying again the redirection, but with this function.

    function test_redirect() {
      fout="stdout.log"
      ferr="stderr.log"
      echo "$ outerr"
      (outerr) > >(tee "$fout") 2> >(tee "$ferr" >&2)
      echo "# $fout content :"
      cat "$fout"
      echo "# $ferr content :"
      cat "$ferr"
    }
    

    Personally, I have this result :

    user@computer:~$ test_redirect
    $ outerr
    # stdout.log content :
    out
    out
    err
    # stderr.log content :
    err
    user@computer:~$
    

    No prompt on a blank line, but I don't see normal output, stdout.log content seem to be wrong, only stderr.log seem to be ok. If I relaunch it, output can be different...

    So, why ?

    Because, like explained here :

    Beware that in bash, this command returns as soon as [first command] finishes, even if the tee commands are still executed (ksh and zsh do wait for the subprocesses)

    So, if you use bash, prefer use the better exemple given in this other answer :

    { { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2
    

    It will fix the previous issues.

    Now, the question is, how to retrieve exit status code ?
    $? does not works.
    I have no found better solution than switch on pipefail with set -o pipefail (set +o pipefail to switch off) and use ${PIPESTATUS[0]} like this

    function outerr() {
      echo "out"
      echo >&2 "err"
      return 11
    }
    
    function test_outerr() {
      local - # To preserve set option
      ! [[ -o pipefail ]] && set -o pipefail; # Or use second part directly
      local fout="stdout.log"
      local ferr="stderr.log"
      echo "$ outerr"
      { { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2
      # First save the status or it will be lost
      local status="${PIPESTATUS[0]}" # Save first, the second is 0, perhaps tee status code.
      echo "==="
      echo "# $fout content :"
      echo "<==="
      cat "$fout"
      echo "===>"
      echo "# $ferr content :"
      echo "<==="
      cat "$ferr"
      echo "===>"
      if (( status > 0 )); then
        echo "Fail $status > 0"
        return "$status" # or whatever
      fi
    }
    
    user@computer:~$ test_outerr
    $ outerr
    err
    out
    ===
    # stdout.log content :
    <===
    out
    ===>
    # stderr.log content :
    <===
    err
    ===>
    Fail 11 > 0
    

提交回复
热议问题