coreutils
[Top][All Lists]
Advanced

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

Re: stdbuf in pipes


From: Pádraig Brady
Subject: Re: stdbuf in pipes
Date: Mon, 10 Sep 2018 22:16:50 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

On 16/08/18 23:32, Paul Wagner wrote:
> Hello!
> 
> When I use a pipe and the receiving end dies, I understand that the 
> sending end is terminated.  But when I use stdbuf on the sender, a dying 
> receiving does not terminate the pipe.  Example:
> 
> $ { i=0; while true; do echo "send $i" >&2; echo $((i++)); sleep 1; 
> done; } | { while read s; [[ $s != 5 ]]; do echo "receive $s"; done; }
> send 0
> receive 0
> send 1
> receive 1
> send 2
> receive 2
> send 3
> receive 3
> send 4
> receive 4
> send 5
> send 6
> $ { i=0; while true; do echo "send $i" >&2; stdbuf -oL echo $((i++)); 
> sleep 1; done; } | { while read s; [[ $s != 5 ]]; do echo "receive $s"; 
> done; }
> send 0
> receive 0
> send 1
> receive 1
> send 2
> receive 2
> send 3
> receive 3
> send 4
> receive 4
> send 5
> send 6
> send 7
> send 8
> ... [manually killed by CTRL-C]
> 
> Could anyone explain the reason for this behaviour to me?

It's not specific to stdbuf.
stdbuf is causing the echo _program_ rather than the builtin shell echo to be 
used.
You can see the same thing by replacing `stdbuf` with `env`.
So the sub process (/bin/echo) is consuming the SIGPIPE and exiting.
One could handle that by adding a "break" like:

$ { i=0; while true; do echo "send $i" >&2;
    stdbuf -oL echo $((i++)) || break;
    sleep 1; done; } |
  { while read s; [[ $s != 5 ]]; do echo "receive $s"; done; }
send 0
receive 0
send 1
receive 1
send 2
receive 2
send 3
receive 3
send 4
receive 4
send 5
send 6

cheers,
Pádraig.



reply via email to

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