【问题标题】:How to quiet the output of command in Bash?如何让 Bash 中的命令输出安静?
【发布时间】:2018-07-07 15:45:51
【问题描述】:

我在我的 bash 脚本中编写了以下命令: service service1 status | grep -q "good"

在这里,我正在尝试quiet命令service service1 status的输出

请建议如何做到这一点?

我已经尝试过service service1 status | grep -q "good" > /dev/null,但如果service service1 status 失败,它仍然会给出命令输出

【问题讨论】:

  • 嗯,如果你想让输出静音,那么grep 是干什么用的?如果你静音,没有什么可以grep的?
  • @MarcinOrlowski:有时,如果grep 的结果被测试,通常是通过显式测试$? 是有效的。当然有更好的方法来做到这一点,但模式很常见。

标签: bash shell systemctl


【解决方案1】:

问题不在于grep -q - 它不会产生任何输出。我认为service 命令正在写入标准错误。所以,你需要重定向它:

service service1 status 2>&1 | grep -q "good"

除非这样做,否则grep 将看不到service 的输出,因此它在逻辑上也不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2021-05-09
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2021-01-14
    相关资源
    最近更新 更多