help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How not to wait for the inputs finish if a program does


From: Bob Proulx
Subject: Re: [Help-bash] How not to wait for the inputs finish if a program does not need to see all the input?
Date: Sat, 26 Mar 2016 00:05:24 -0600
User-agent: Mutt/1.5.24 (2015-08-30)

Ehsan Hajiramezanali wrote:
> If one runs the following examples, both head and awk will wait until
> `sleep` is finished.
> 
> (echo 1; sleep 10; seq 10) | head -n 1
> (echo 1; sleep 10; seq 10) | awk -e 'NR==1{print $1;exit}'

Try it with exit.

  (sleep 10 | exit)

Or try it with true.

  sleep 10 | true

The shell must wait for the sleep to finish since it is a foreground
process.

> However, they only need the output from the first echo command. It is
> unnecessary that they wait until `sleep` is finished. I am wondering
> if this problem is related with the shell or the programs head/awk.

Neither.  It is related to the sleep process which is still running.

> How one might let head and awk finish as soon as they see the first line?

To make the entire command line finish the entire command line must
finish.  Which means that the sleep (a surrogate for the real process
I am sure) must exit.

Normally when the second process in the pipeline exits this will close
the pipe and when the first process in the pipeline writes to the pipe
it will receive SIGPIPE and exit due to the signal.  Here the sleep
isn't writing anything and therefore won't notice the closed pipe and
won't trigger a SIGPIPE.

At one level the easiest thing is to have the process represented by
the sleep to continue to write a significant output into the pipe so
that when the second process exists it will trigger and receive the
SIGPIPE.

HTH,
Bob



reply via email to

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