关于写标准错误输出到文件的问题


如下,使用该命令将 example.sh 的输出打印到屏幕上,同时也会写标准输出到 stdout.out 打印到屏幕。而我还想在此同时,写标准错误输出到名为 error.out 的文件中。


 ./example.sh | tee ./stdout.out

请问要如何修改这段命令才可以做到上面的要求?

Linux pipe bash

妄想少年J 10 years, 8 months ago

./example.sh 2> error.out | tee ./stdout.out

螺螺螺螺螺螺螺 answered 10 years, 8 months ago

如果你不需要输出标准错误的内容到终端的话:

./example.sh 2> ./error.out | tee ./stdout.out
将标准错误重定向到 error.out 中,标准输出正常显示

如果还需要输出标准错误的内容:

./example.sh > >(tee ./stdout.out) 2> >(tee ./error.out)

解释:
> 2> 就是重定向,前者重定向标准输出,后者重定向标准错误(2是标准错误的文件描述符)
>(...) 是创建一个带FIFO(一种进程间通信的方式)的子进程,让这个子进程接收前面的命令的输出。

马克的山羊须 answered 10 years, 8 months ago

Your Answer