Linux下使用tee既在屏幕上显示输出,又把输出写进文件

    xiaoxiao2024-10-31  74

    Linux下的tee是一个很好用的工具,可以把重定向屏幕输出到文件的同时在屏幕上显示输出

    使用示例如下:

    command | tee stdout.log

    这里有一个需要注意的坑点,上面的命令只是把标准输出,也就是 STDOUT 写进文件,并没有处理错误输出,也就是 STDERR

    如果需要处理错误输出,命令如下:

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

    如果你想把标准输出和错误输出都重定向到一个文件,那么命令如下:

    command 2>&1 | tee -a log

    参考文章:linux - How do I write stderr to a file while using “tee” with a pipe? - Stack Overflow

    最新回复(0)