[Top][All Lists]

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

Re: [head] wished an option to continue consuming the input after the sp

From: Bob Proulx
Subject: Re: [head] wished an option to continue consuming the input after the specified number of lines has been read
Date: Sun, 14 Oct 2012 21:03:15 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

Thibault LE PAUL wrote:
> As far as I know, when the specified number of lines has been read,
> the 'head' process stops,

That is by design.  Because often people will do things like this:

  # File trial1
  line1=$(head -n1)
  line2=$(head -n1)
  line3=$(head -n1)
  cat > somefile

And then call it:

  printf "one\ntwo\nthree\nfour\nfive\n" | trial1

and other similar things.  Therefore head needs to avoid draining
input that it isn't going to read.

> and a signal is sent to the anterior process in the pipe.

That depends upon the circumstances.  SIGPIPE is sent to a writer when
the write pipe is closed while still trying to write output.  However
continuing as above there won't be sigpipe sent since later head calls
and the final 'cat' above will consume the rest of the input.  It
isn't head that is sending the signal.

> I think this behavior is not always the best. I suggest to add an
> option to say head to continue reading the input until EOF.

You can always use 'sed' to do this.  Simply have sed discard all
subsequent lines if that is what you wish to do.  For example to do
something similar to head -n2 but to consume all input do this:
  sed '3,$d'

So will this:

  awk 'NR<3'

So will this:

  head -n2 ; cat >/dev/null

Since there are already so many existing ways to accomplish this I
don't think there is a need for an option.

Having said all of the above, I can't think of any cases when I would
actually want to do this.  Under what circumstances are you having
problems with SIGPIPE?  I think this is an XY problem[1].  If we can
fix X then we don't need to fix Y.  :-)  Why are you having issues
with SIGPIPE?  Or if not SIGPIPE then what is the original issue you
are trying to solve?


[1] XY problem:

reply via email to

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