Redirecting stderr in csh

北战南征 提交于 2019-12-05 02:13:07

csh is significantly more limited than bash when it comes to file redirection. In csh, you can redirect stdout with the usual > operator, you can redirect both stdout and stderr with the >& operator, you can pipe stdout and stderr with the |& operator, but there is no single operator to redirect stderr alone.

The usual workaround is to execute the command in a sub-shell, redirecting stdout in that sub-shell to whatever file you want (/dev/null in this case), and then use the |& operator to redirect stdout and stderr of the sub-shell to the next command in the main shell.

In your case, this means something like:

( command >/dev/null ) |& grep "^[^-]" >&/tmp/fl

Because stdout is redirected to /dev/null inside the sub-shell, the |& operator will end up acting as 2>&1 in bash - since stdout is discarded in the sub-shell, nothing written to stdout will ever reach the pipe.

If you dont mind mixing stdout and stderr into the pipe you can use

command |& grep "^[^-]" >& /tmp/fl

Otherwise you can do the hack:

(command >/dev/null) |& grep "^[^-]" >& /tmp/fl

which separates out stdout to null, then piping stdout and stderr just gives stderr as content.

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