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

Re: Is it possible to capture stdout and stderr to separate variables in Zsh?



On Sep 1,  7:59pm, Han Pingtian wrote:
}
} I'm using zsh 5.0, but looks like still cannot close the prior coproc
} with "coproc exit":
} 
} % coproc while read line;do print -r -- "$line:u";done

This is a bug/misfeature that I think I've mentioned before (possibly
only on zsh-workers) which affects built-in commands including loop
constructs.

The trouble is that zsh opens the coproc descriptors in the parent
shell (as it must) and marks them close-on-exec.  It then forks for
the actual coprocess job and dups the coproc descriptors to the job's
stdin and stdout.

Then the bug happens:  Because the coprocess is a built-in, there is
no exec, so the original coproc descriptors are never closed.  This
leaves the coprocess "talking to itself":

zsh     17133 schaefer    0r  FIFO    0,7          2256774 pipe
zsh     17133 schaefer    1w  FIFO    0,7          2256773 pipe
zsh     17133 schaefer    2u   CHR  136,2                4 /dev/pts/2
zsh     17133 schaefer   10u   CHR  136,2                4 /dev/pts/2
zsh     17133 schaefer   11r  FIFO    0,7          2256773 pipe
zsh     17133 schaefer   14w  FIFO    0,7          2256774 pipe

Note that 14w is connected to 0r and 11r is connected to 1w, all in
the same process.  IIRC exactly the same thing happens in at least
some revisions of ksh if you use a built-in construct as a coproc.

The workaround for now is to close the coproc inside the coproc:

% coproc { coproc exit; while read line;do print -r -- "$line:u";done }



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