How to redirect stderr to a file for the whole pipe?

假如想象 提交于 2019-12-10 13:58:37

问题


I am running a command like this:

mycmd1 | mycmd2 | mycmd3 | lp

Is there a way to redirect stderr to a file for the whole pipe instead of repeating it for each command?

That is to say, I'd rather avoid doing this:

mycmd1 2>/myfile | mycmd2 2>/myfile | mycmd3 2>/myfile | lp 2>/myfile

回答1:


Either

{ mycmd1 | mycmd2 | mycmd3 | lp; } 2>> logfile

or

( mycmd1 | mycmd2 | mycmd3 | lp ) 2>> logfile

will work. (The first version might be have a slightly faster (~1ms) startup time depending on the shell).




回答2:


I tried the following, and it seems to work:

(mycmd1 | mycmd2 | mycmd3 | lp) 2>>/var/log/mylogfile.log

I use >> because I want to append to the logfile rather than overwriting it every time.



来源:https://stackoverflow.com/questions/40823769/how-to-redirect-stderr-to-a-file-for-the-whole-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!