bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: grep invert-match return code


From: Bob Proulx
Subject: Re: grep invert-match return code
Date: Thu, 20 Apr 2006 23:49:33 -0600
User-agent: Mutt/1.5.9i

Tavis Ormandy wrote:
> POSIX says `grep -v` will "Select lines not matching any of
> the specified patterns.", and the return code should be 1
> if "No lines were selected.", however this doesnt seem to be
> true in GNU grep:

Thanks for the report.  But you are not reading this correctly.

> $ printf "one\ntwo\nthree\n" | grep -v one; echo $?
> two
> three
> 0
> $ printf "one\ntwo\nthree\n" | grep -v four; echo $?
> one
> two
> three
> 0

In both cases lines were selected and printed.  Because lines were
selected the return value was zero.  The output you show is from
selected lines.  If no lines had been selected then there would have
been no output.

> both return 0, "One or more lines were selected", even though this
> doesnt seem to be true in the second example, as no lines were
> selected.

Here is another way to think about it.

  if [ $(printf "one\ntwo\nthree\n" | grep -v four | wc -l) -ne 0 ]; then
    return 0
  else
    return 1
  fi

> Sorry if I have misinterpreted the documentation.

To get the result that you are looking for you would need to keep the
same pattern but instead invert the return code.

  ! printf "one\ntwo\nthree\n" | grep one; echo $?
  one
  1

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]