grep命令退出状态代码

grep命令退出状态代码,grep,Grep,退出状态部分报告中的grep手册: EXIT STATUS The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.) 返回的值始终为0。我本以为是1

退出状态部分报告中的grep手册:

EXIT STATUS The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)
返回的值始终为0。我本以为是1和0。我做错了什么?

记住
grep
是基于行的。如果有任何一行匹配,那么就有一行匹配。(在第一种情况下,
test.zip
匹配(更准确地说:您与
-v
一起使用,因此您要求的行与您的模式不匹配,
test.zip
正好匹配,即与您的模式不匹配。因此,您的grep调用成功)。比较

$ grep -vE '^[.]' <<<$'.\na'; echo $?
a
0

如果grep没有找到任何匹配项,则退出状态为
1
。两个给定样本都返回至少一个匹配行。因此,对于这两种情况,退出状态均为
0
。您的问题可以替换为:“为什么
grep-vE'^[.]'@Sundeep no'^[.]“只匹配以点开始的内容,可能不清楚输入是否是多行的?@MichaWiedenmann是的,但问题不是这样。@bandabassotti我可以善意地建议,您有点困惑,sundeep解释说,正是因为这条线是多行的,并且有一条线与您的模式匹配,所以您得到了结果。(Grep是基线。)
$ grep -vE '^[.]' <<<$'.\na'; echo $?
a
0
$ grep -vE '^[.]' <<<$'.\n.'; echo $?
1
Here Strings
    A variant of here documents, the format is:

           [n]<<<word

    The word undergoes brace  expansion,  tilde  expansion,  parameter  and
    variable  expansion,  command  substitution,  arithmetic expansion, and
    quote removal.  Pathname expansion and  word  splitting  are  not  per-
    formed.   The  result  is  supplied  as a single string, with a newline
    appended, to the command on its standard input (or file descriptor n if
    n is specified).
   $ cat <<<'hello world'
   hello world
$ echo $'1\na'
1
a