在csh中重定向stderr [英] Redirecting stderr in csh

查看:679
本文介绍了在csh中重定向stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个程序,该程序将崩溃报告转储到STDERR中,在这里我必须过滤一些必要的信息.问题是我无法将STDERR重定向到STDOUT并使用grep

I'm executing a program that dumps crash report into STDERR from where I have to filter some necessary information. The problem is that I'm unable to redirect STDERR to STDOUT and PIPE it with grep

command 2>&1 >/dev/null | grep "^[^-]" >& /tmp/fl

获取错误:Ambiguous output redirect.

相同的命令在bash终端下有效. 我应该进行哪些更改才能使其正常工作?

Same command works under bash terminal. What should I change to make it work ?

推荐答案

csh的局限性远大于bash.在csh中,可以使用常规的>运算符重定向stdout,可以使用>&运算符重定向stdoutstderr,可以使用|&运算符,但是没有一个运算符可以单独重定向stderr.

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.

通常的解决方法是在子外壳中执行命令,将该子外壳中的stdout重定向到所需的任何文件(在本例中为/dev/null),然后使用|&运算符进行重定向子外壳的stdoutstderr到主外壳中的下一个命令.

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

因为stdout被重定向到子外壳中的/dev/null,所以|&运算符最终将在bash中充当2>&1-因为stdout被丢弃在子外壳中,所以没有写入任何内容stdout将永远到达管道.

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.

这篇关于在csh中重定向stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆