bash grep 命令静默响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19841812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:31:54  来源:igfitidea点击:

grep command response silently

linuxbashshellgrep

提问by Nimjox

I'm new to linux shell and am trying to do this, preferably in one line, with the following condition: It can't output anything to the terminal.

我是 linux shell 的新手,我正在尝试这样做,最好在一行中,条件如下:它无法向终端输出任何内容。

/var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt'

The problem is the .exe spits out XML data to terminal. I can't figure out how to grep it and not have the shell output it. If I use /dev/null 2>&1, it pipes it quite but then I can't grep the data. Any idea's?

问题是 .exe 将 XML 数据输出到终端。我不知道如何 grep 并且不让 shell 输出它。如果我使用/dev/null 2>&1,它会很好地传输它,但是我无法 grep 数据。有任何想法吗?

回答by anubhava

Use grep -q(quiet)

使用grep -q(安静)

/var/folder/program.exe -L parameters | grep -q "text_to_filter" && echo 'SomeText' > '/tmp/Log.txt'

As per man grep:

根据man grep

-q, --quiet, --silentQuiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.

-q, --quiet, --silent安静的; 不要向标准输出写入任何内容。如果找到任何匹配项,则立即以零状态退出,即使检测到错误也是如此。另请参阅 -s 或 --no-messages 选项。

回答by damienfrancois

Try using |& rather than just |. (needs bash 4)

尝试使用 |& 而不仅仅是 |。(需要 bash 4)