Save stdout, stderr and stdout+stderr synchronously

前端 未结 3 853
轻奢々
轻奢々 2020-12-16 03:40

For testing purposes, I would like to save stdout and stderr separately for inspection by subsequent code. For example, a test run with erroneous input should resul

3条回答
  •  独厮守ぢ
    2020-12-16 04:14

    For the last iteration, where the value of bar is set to the contents of stderr, try:

    # restore original stdout & stderr at the end by adding: 1>&2 2>&1
    bar="$(
    {
       {
          echo foo >&2 | tee stdout.log 2>&3 3>&-
       } 2>&1 >&4 4>&- | tee stderr.log 2>&3 3>&-
    } 3>&2 4>&1 1>&2 2>&1
    )"
    
    echo "$bar"
    

提交回复
热议问题