【问题标题】:How to pipe (in bash) standard output to a variable, and stderr to a command如何将(在 bash 中)标准输出通过管道传输到变量,并将标准错误传输到命令
【发布时间】:2013-04-16 09:42:37
【问题描述】:

我有这样的管道:

VAR=`command1 | command2 | etc...`

我希望,如果管道成功完成,输出将保存在VAR 中,否则,如果管道在管道的任何点失败,则将错误发送到命令。然后该命令以某种格式将错误写入某个文件。类似的东西(但这不起作用):

VAR=`{ command1 | command2 | etc... ; } 2> err_command`

我尝试过其他组合:

|& err_command
2>&1 | err_command

但它们不起作用,我不知道为什么。

【问题讨论】:

    标签: bash stdout pipe stderr


    【解决方案1】:

    尝试使用进程替换。

    VAR=`{ command1 | command2 | etc... ; } 2> >(err_command)`
    

    【讨论】:

    • 工作得很好。我发誓我以前试过这个:S呵呵。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2022-07-05
    相关资源
    最近更新 更多