GNU bug report logs - #41700
grep -v always exiting with 1 for empty file

Previous Next

Package: grep;

Reported by: Andi Kleen <andi <at> firstfloor.org>

Date: Thu, 4 Jun 2020 04:33:01 UTC

Severity: normal

Tags: notabug

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


Message #14 received at 41700 <at> debbugs.gnu.org (full text, mbox):

From: Andi Kleen <andi <at> firstfloor.org>
To: Norihiro Tanaka <noritnk <at> kcn.ne.jp>
Cc: Andi Kleen <andi <at> firstfloor.org>, 41700 <at> debbugs.gnu.org
Subject: Re: bug#41700: grep -v always exiting with 1 for empty file
Date: Thu, 4 Jun 2020 07:06:55 -0700
On Thu, Jun 04, 2020 at 07:26:34PM +0900, Norihiro Tanaka wrote:
> 
> On Wed, 3 Jun 2020 20:26:41 -0700
> Andi Kleen <andi <at> firstfloor.org> wrote:
> 
> > 
> > % grep --version
> > grep (GNU grep) 3.4
> > ...
> > % echo -n > foo
> > % grep -v foo foo ; echo $?
> > 1
> > 
> > Would expect it to exit with zero in this case, since foo is not in the
> > file.
> > 
> > When the file is one byte it works as expected:
> > 
> > % echo > foo
> > % grep -v foo foo ; echo $?
> > 
> > 0
> > %
> 
> `0 if a line is selected, 1 if no lines were selected,' in manual.
> `0: One or more lines were selected. 1: No lines were selected.' in
> POSIX.
> 
> > % echo -n > foo
> > % grep -v foo foo ; echo $?
> 
> It returns no lines, so it seems reasonable to return 1.

I would argue that a empty file has a single empty line.

Besides it completely breaks the "is foo not in file" functionality.

In normal terminology that's a off by one bug. Why should 
an empty file be different than any other files?

I just had to rewrite a test script of mine and add lots of 

echo >> outputfile

statements to work around it.

-Andi





This bug report was last modified 4 years and 239 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.