如何抑制grep的输出? [英] How can I suppress output of grep?
问题描述
当我运行此命令时,当grep成功执行时,它还会在文件中打印实际的$item
.我不想打印内容/$item
.我只想显示我的回声.
When I run this command, it also prints the actual $item
in the file when the grep is successful. I do not want to print the content/$item
. I just want to show my echo.
我该怎么办?
if grep $item filename; then
echo it exist
else
echo does not exist
fi
推荐答案
使用-q
:
if grep -q "$item" filename; then
echo "it exists"
else
echo "does not exist"
fi
或在一个班轮中:
grep -q "$item" filename && echo "it exists" || echo "does not exist"
来自man grep
-q ,--quiet,--silent
-q, --quiet, --silent
安静; 请勿在标准输出中写入任何内容.立即退出 如果找到任何匹配项,则状态为零,即使检测到错误也是如此. 另请参见-s或--no-messages选项. (-q由POSIX指定.)
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. (-q is specified by POSIX.)
如下面的AdrianFrühwirth点,grep -q
只会使STDIN静音.如果要摆脱STDERR,可以将其重定向到/dev/null
:
As Adrian Frühwirth points below, grep -q
alone will just silence the STDIN. If you want to get rid of the STDERR, you can redirect it to /dev/null
:
grep -q foo file 2>/dev/null
这篇关于如何抑制grep的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!