【问题标题】:Suppress just bash job control messages in scripts (Aborted, Broken pipe)仅在脚本中禁止 bash 作业控制消息(已中止,管道损坏)
【发布时间】:2021-07-12 04:40:37
【问题描述】:

我有以下设置:

aborter.cpp

#include <cassert>
int main(int argc, char** argv) { assert(argc >= 2); return 0; }
// compiled to 'aborter'

test.sh

#!/usr/bin/bash

function runpipe() {
    yes | aborter "$@"
    test $? -ne 0 || echo OK
}
runpipe "$@"

运行test.sh 123 仅打印OK。仅运行 test.sh 会输出以下内容:

aborter: aborter.cpp:2: int main(int, char**): Assertion `argc >= 2' failed.
./test.sh: line 3:  2521 Broken pipe             yes
      2522 Aborted                 | aborter "$@"

在第二种情况下,如何抑制 bash 输出的“Broken pipe”和“Aborted”作业控制行? 我希望输出只是 C++ 断言转储(第一行)。

这不需要编辑aborter.cpptest.sh 界面。

【问题讨论】:

  • 您使用的是什么操作系统?
  • 将错误消息重定向到dev/null,而不是标准输出runpipe "$@" 2&gt;/dev/null

标签: bash jobs abort


【解决方案1】:

在第二种情况下,如何抑制 bash 输出的“Broken pipe”和“Aborted”作业控制行?

将 bash stderr 重定向为 null,除非您处理。

exec 3<&2 2>/dev/null
function runpipe() {
    yes | aborter "$@" 2>&3
    test $? -ne 0 || echo OK
}
runpipe "$@"

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 2018-11-29
    • 1970-01-01
    • 2022-10-15
    • 2011-05-26
    • 1970-01-01
    • 2015-03-28
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多