3

How can I redirect the output of a command such that both stdout and stderr gets logged in a file, AND I still want stderr to appear as output.

I also don't want to use bash for doing this. Is there such a way?

4

1 回答 1

3

这很容易:

$ ( ./command.sh >> log ) 2>&1 | tee -a log

您将命令的标准输出写入log子外壳中的文件;比您将 stderr 写入管道;然后,我的方法是tee将其保存到 并将其log复制到控制台。

使用示例:

$ cat command.sh               
#!/bin/sh

echo out
echo err > /dev/stderr

$ ( ./command.sh >> log ) 2>&1 | tee -a log
err
$ cat log
out
err
于 2013-08-26T07:22:31.007 回答