How to redirect stdout+stderr to one file while keeping streams separate?

后端 未结 5 1791
臣服心动
臣服心动 2020-12-28 19:39

Redirecting stdout+stderr such that both get written to a file while still outputting to stdout is simple enough:

cmd 2>&1 | tee output_file
         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 20:07

    { { cmd | tee out >&3; } 2>&1 | tee err >&2; } 3>&1
    

    Or, to be pedantic:

    { { cmd 3>&- | tee out >&3 2> /dev/null; } 2>&1 | tee err >&2 3>&- 2> /dev/null; } 3>&1
    

    Note that it's futile to try and preserve order. It is basically impossible. The only solution would be to modify "cmd" or use some LD_PRELOAD or gdb hack,

提交回复
热议问题