[Top][All Lists]

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

Re: [Help-bash] prepend to stdin

From: Chet Ramey
Subject: Re: [Help-bash] prepend to stdin
Date: Tue, 22 Dec 2015 20:16:29 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.4.0

On 12/22/15 3:18 PM, Brett Kail wrote:
> I'm writing a bash script, and I would like to execute a command passing as
> stdin the contents of a file followed by the script's stdin.  The command
> might or might not read stdin, but regardless, I would like the entire
> pipeline to end when the final command exits.  I tried this pipeline (using
> false as an example command):
> cat file - | true
> ...but it seems that something waits for a line of input before the
> pipeline somehow ends without error.  What is causing this wait?  Is there
> some way to disable the wait and have the entire pipeline end immediately
> when the final command exits?

You've created a race condition.  It depends on the behavior of the kernel
and its scheduling algorithm.  Consider the following possible sequence:

1. cat writes file to the pipe, which happens atomically because file is
   smaller than the pipe buffer size;
2. true exits;
3. cat processes its next input `file', which causes it to read a line from

On another system (Mac OS X, say), with a different scheduler, true might
exit before cat writes the file, and the entire pipeline would exit when
cat got the SIGPIPE.  But you can't tell.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden

reply via email to

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