我是 linux shell 的新手,我正在尝试这样做,最好是在一行中,条件如下:它无法向终端输出任何内容。
/var/folder/program.exe -L parameters | grep text_to_filter && echo SomeText >'/tmp/Log.txt'
问题是 .exe 向终端吐出 XML 数据。我不知道如何 grep,使用退出状态,但不会让屏幕因每场比赛的输出而变得困惑。如果我使用 /dev/null 2>&1
,它会完全通过管道传输,但我无法 grep 数据。有什么想法吗?
最佳答案
使用grep -q
(安静)
/var/folder/program.exe -L parameters |
grep -q "text_to_filter" && echo 'SomeText' > '/tmp/Log.txt'
根据man grep
:
-q, --quiet, --silent
Quiet; 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.
关于linux - 如何使用 grep 进行匹配但不打印匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19841812/