在csh中重定向stderr

PYP*_*YPL 9 linux shell csh tcsh

我正在执行一个将崩溃报告转储到STDERR我必须过滤一些必要信息的程序.问题是,我无法重定向STDERRSTDOUTPIPEgrep

command 2>&1 >/dev/null | grep "^[^-]" >& /tmp/fl
Run Code Online (Sandbox Code Playgroud)

得到错误: Ambiguous output redirect.

相同的命令在bash终端下工作.我应该改变什么来使它工作?

Fil*_*ves 12

csh在文件重定向方面比bash明显更有限.在csh,您可以重定向stdout与通常的>操作,您可以重定向都stdoutstderr>&运营商,可以通过管道stdoutstderr|&运营商,但没有一个统一的操作重定向stderr孤单.

通常的解决办法是在一个子shell来执行命令,重定向stdout在子shell到你想要的任何文件(/dev/null在这种情况下),然后使用|&操作重定向stdoutstderr子壳在接下来的命令主壳.

在你的情况下,这意味着:

( command >/dev/null ) |& grep "^[^-]" >&/tmp/fl
Run Code Online (Sandbox Code Playgroud)

因为stdout被重定向到/dev/null子shell内部,所以|&操作符最终将像2>&1bash一样 - 因为stdout在子shell中被丢弃,所写的stdout任何内容都不会到达管道.

  • 我认为通常的解决方法是调用`sh`:`sh -c "command 2>&1 > /dev/null"` (2认同)
  • @WilliamPursell 是的,当然。我以为他出于某种原因想留在 csh。 (2认同)

meu*_*euh 5

如果您不介意将stdout和stderr混合到您可以使用的管道中

command |& grep "^[^-]" >& /tmp/fl
Run Code Online (Sandbox Code Playgroud)

否则你可以做黑客:

(command >/dev/null) |& grep "^[^-]" >& /tmp/fl
Run Code Online (Sandbox Code Playgroud)

将stdout分隔为null,然后管道stdout和stderr只给stderr作为内容.