Web lists-archives.com

Re: Pipe behavior clarification?




On 01/22/2017 04:23 PM, Eliot Moss wrote:
> On 1/22/2017 3:19 PM, David Balažic wrote:
>> Hi!
>>
>> Is this a correct pipe behavior?
>>
>> $ echo booo | tee >(md5sum --tag) >/dev/null
>> MD5 (-) = 9c8b79bdf79ef0ee73a77b8d36d27a2d
>>
>> $ echo booo | tee >(md5sum --tag) | cat >/dev/null
> 
> Here's what I think happens, even if it is a bit counter-intuitive:
> 
>>(...) creates a subprocess, whose input comes from some kind
> of pipe or socket, and tee is presented with a filename it can
> use to write to that socket.
> 
> The *output* of the >(...) subprocess is hooked up to what is
> known to be the output of tee *at the time the subprocess is
> created*.  This happens *before* any > redirections are done.

Rather, all >() and > redirections are performed in left-to-right order.
 But you are correct that the second >/dev/null is overwriting the
stdout that was originally given by >(md5sum), and therefore tee is NOT
writing to the md5sum process.

> 
> However, in the case of the | pipe, that plumbing is set up
> *before* the >(...) construct is acted on.

Also correct. Mixing >() and | is usually not what you want, as you are
no longer writing to the pipeline.

> 
> Note that you could do >(md5sum --tag >whatever) if you want
> to specifically control the output of md5sum.
> 
> I am sure someone more knowledgeable will correct me if I've
> missed anything important here :-) ...

You got the gist of it.  Order matters, and specifying more than one
stdout (by any mix of >, >(), or |) is generally not what you want.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature