Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: subprocess



On Fri, Jun 5, 2020 at 6:01 PM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> > On 6/5/20, Pier Paolo Grassi <pierpaolog@xxxxxxxxx> wrote:
> > > efe() cat $1
> > > efe <(seq 1 10) | wc -l
> > > cat: /proc/self/fd/13: No such file or directory
> > > 0
> > >
> > > gives error, instead these all works fine:
> > > efe =(seq 1 10)| wc -l
> > > cat <(seq 1 10)| wc -l
>
> And this,
>
> % () { cat $1 } <(seq 1 10) | wc -l

You can see what's happening if you do it this way:

% efe() ls -l ${${1:+$1:h}:-/proc/self/fd}
% efe <(seq 1 10) | cat
total 0
lrwx------ 1 schaefer schaefer 64 Jun  6 10:59 0 -> /dev/pts/0
l-wx------ 1 schaefer schaefer 64 Jun  6 10:59 1 -> pipe:[1945962]
lrwx------ 1 schaefer schaefer 64 Jun  6 10:59 2 -> /dev/pts/0
lr-x------ 1 schaefer schaefer 64 Jun  6 10:59 3 -> /proc/5533/fd
% () { ls -l $1:h } <(seq 1 10) | cat
total 0
lrwx------ 1 schaefer schaefer 64 Jun  6 11:00 0 -> /dev/pts/0
l-wx------ 1 schaefer schaefer 64 Jun  6 11:00 1 -> pipe:[1945988]
lr-x------ 1 schaefer schaefer 64 Jun  6 11:00 11 -> pipe:[1944505]
lrwx------ 1 schaefer schaefer 64 Jun  6 11:00 2 -> /dev/pts/0
lr-x------ 1 schaefer schaefer 64 Jun  6 11:00 3 -> /proc/5537/fd

A named function on the left side of a pipeline becomes its own "job",
where all descriptors above 10 are closed.  An anonymous function
stays in the parent shell (which is still a forked subshell, because
of the pipeline) so those descriptors are not closed.

% efe =(seq 1 10) | cat
total 4
-rw------- 1 schaefer schaefer 21 Jun  6 11:09 zshkDpnRj

In that case a real file has been created and is referenced by name,
so the descriptors closing does not matter.

% efe <<(seq 1 10) | cat
total 0
lr-x------ 1 schaefer schaefer 64 Jun  6 11:13 0 -> pipe:[1946804]
l-wx------ 1 schaefer schaefer 64 Jun  6 11:13 1 -> pipe:[1946579]
lrwx------ 1 schaefer schaefer 64 Jun  6 11:13 2 -> /dev/pts/0
lr-x------ 1 schaefer schaefer 64 Jun  6 11:13 3 -> /proc/5599/fd

For <<(...) no file is created at all, instead the standard input of
the function is changed.

> Mikael Magnusson wrote on Fri, 05 Jun 2020 12:25 +0200:
> >
> > This also works,
> > % efe <(seq 1 10) > >(wc -l)
> > as does this,
> > % wc -l < <(efe <(seq 1 10))

In both of these cases, there is no pipeline, so efe stays in the
parent shell and all the descriptors remain open.



Messages sorted by: Reverse Date, Date, Thread, Author