2013-08-25 177 views
3

如何重定向命令的輸出,以使stdout和stderr都登錄到文件中,並且我仍然希望將stderr顯示爲輸出。如何將stdout和stderr重定向到文件AND仍只輸出stderr?

我也不想使用bash來做這件事。有沒有這樣的方式?

+0

通常貝殼做重定向。所以當你說你不想使用bash時,你想使用什麼shell。 – dcaswell

+0

有關bash的信息,請參閱[將stdout和stderr重定向到文件並將stderr重定向到stdout](http://stackoverflow.com/q/24001720/905686)。 – user905686

回答

2

這是非常容易的:

$ (./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 
相關問題