Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Capture stdout, stdin, and exit status in different variables without using temporary files
- X-seq: zsh-users 24149
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Aryn Starr <whereislelouch@xxxxxxxxxx>
- Subject: Re: Capture stdout, stdin, and exit status in different variables without using temporary files
- Date: Fri, 16 Aug 2019 11:05:01 +0100
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=yW7X4PiodxCJs34mqXmGQRTU3qOeMMUo9yKIgEhirQ8=; b=a5+h+Ec7AkSh6NTL0vl/RcTOjygELiMHLeuzcxWzP+r0sq9LWP6nGme+mYRI6lOXAf POJoIlCb9MZMXpclaANdgk7HafcamXcQUPEM2y4+/3ggA3oBpUnfFS7+6Zz/BopAxbpz XmJJnot8HyLzaEeXHJPCIPedngyZO2gkxHHNb4HQsRQBcq5E8RIv5gt1GoAAiw+M2sG+ Sl+XYukQxTFocKasDFDP5rMNXgHex0CZ1p/uDp0Pd8HsaWTxBHbGQVnMFj0Za0hUm+Gw QNWB7LaFKCcF1OeIQraItofrM4UmjsBazjWpGAIahG4n1d0h5allHfCoG85UhZiyT+xF z9Tg==
- In-reply-to: <D639E5A6-A28C-40EE-B0E6-27BED4C8CDDB__31117.9253900022$1565917892$gmane$org@icloud.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mail-followup-to: Aryn Starr <whereislelouch@xxxxxxxxxx>, zsh-users@xxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <D639E5A6-A28C-40EE-B0E6-27BED4C8CDDB__31117.9253900022$1565917892$gmane$org@icloud.com>
I assume you meant stderr and not stdin which would make little
sense. And the bash wiki pages refers to capturing both stdout
and err, not stdin (whatever that means).
2019-08-15 20:22:38 +0430, Aryn Starr:
> If not, is a named pipe advantageous to a temporary file?
With named pipes, you'd get deadlocks unless you use a
select()/poll() loop like for unnamed pipes.
> Is there a way to avoid disk IO (which will probably slow things down considerably)?
To expand on the solution in the link I gave earlier to also
capture the exit status, that would be:
<<
#! /bin/zsh -
zmodload zsh/zselect
zmodload zsh/system
(){exec {wo}>$1 {ro}<$1} <(:) # like yash's wo>>|ro (but on Linux only)
(){exec {we}>$1 {re}<$1} <(:)
# the command (here ls as an example)
ls -d / /x >&$wo 2>&$we & pid=$!
exec {wo}>&- {we}>&-
out= err=
o_done=0 e_done=0
while ((! (o_done && e_done))) && zselect -A ready $ro $re; do
if ((${#ready[$ro]})); then
sysread -i $ro && out+=$REPLY || o_done=1
fi
if ((${#ready[$re]})); then
sysread -i $re && err+=$REPLY || e_done=1
fi
done
wait "$pid"; exit_status=$?
printf '%s: %s\n' stdout "$out" stderr "$err" 'exit status' "$exit_status"
>>
Which gives:
stdout: /
stderr: ls: cannot access '/x': No such file or directory
exit status: 2
(note that in $out and $err, the trailing newline character is
not removed).
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author