"grep -q"有什么意义
What is the point of "grep -q"
我在阅读 grep 手册页时遇到了 -q 选项,它告诉 grep "not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected."
我不明白为什么这是令人满意或有用的行为。在一个似乎从 stdin 读取、处理、写入 stdout 的程序中,我为什么要完全静音它?
在什么情况下将目标为输出内容的程序静音会有用?为什么有人想要完全忽略错误并强制执行成功的 return 代码?
谢谢!
grep
的退出状态不一定表示 错误⟩;它表示成功或失败。 grep
将成功定义为匹配 1 行或多行。失败包括匹配零行, 或 其他一些首先阻止匹配发生的错误。
-q
用于不关心匹配的行,只关心 一些 行匹配的情况。
if grep -q foo file.txt; then
echo "file.txt contains foo"
else
echo "file.txt does not contain foo"
fi
我在阅读 grep 手册页时遇到了 -q 选项,它告诉 grep "not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected."
我不明白为什么这是令人满意或有用的行为。在一个似乎从 stdin 读取、处理、写入 stdout 的程序中,我为什么要完全静音它?
在什么情况下将目标为输出内容的程序静音会有用?为什么有人想要完全忽略错误并强制执行成功的 return 代码?
谢谢!
grep
的退出状态不一定表示 错误⟩;它表示成功或失败。 grep
将成功定义为匹配 1 行或多行。失败包括匹配零行, 或 其他一些首先阻止匹配发生的错误。
-q
用于不关心匹配的行,只关心 一些 行匹配的情况。
if grep -q foo file.txt; then
echo "file.txt contains foo"
else
echo "file.txt does not contain foo"
fi