简体   繁体   English

在csh中重定向stderr

[英]Redirecting stderr in csh

I'm executing a program that dumps crash report into STDERR from where I have to filter some necessary information. 我正在执行一个程序,将崩溃报告转储到STDERR ,我必须从中过滤一些必要的信息。 The problem is that I'm unable to redirect STDERR to STDOUT and PIPE it with grep 问题是我无法将STDERR重定向到STDOUT并使用grepPIPE

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

Getting error: Ambiguous output redirect. 获取错误: Ambiguous output redirect.

Same command works under bash terminal. 相同的命令在bash终端下工作。 What should I change to make it work ? 我应该改变什么来使它工作?

csh is significantly more limited than bash when it comes to file redirection. 在文件重定向方面, csh比bash明显更有限。 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. csh ,你可以使用通常的>运算符重定向stdout ,你可以使用>&运算符重定向stdoutstderr ,你可以使用|&运算符管道stdoutstderr ,但是没有单个运算符可以单独重定向stderr

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. 通常的解决方法是在子shell中执行命令,将该子shell中的stdout重定向到您想要的任何文件(在这种情况下为/dev/null ),然后使用|&运算符重定向stdoutstderr子shell到主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. 因为stdout被重定向到子shell内的/dev/null|&运算符最终将在bash中作为2>&1 - 因为stdout在子shell中被丢弃,所以写入stdout任何内容都不会到达管道。

If you dont mind mixing stdout and stderr into the pipe you can use 如果您不介意将stdout和stderr混合到您可以使用的管道中

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. 将stdout分隔为null,然后管道stdout和stderr只给stderr作为内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM