zsh-users
 help / color / mirror / code / Atom feed
From: Aryn Starr <whereislelouch@icloud.com>
To: Stephane Chazelas <stephane.chazelas@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Capture stdout, stdin, and exit status in different variables without using temporary files
Date: Fri, 16 Aug 2019 15:15:49 +0430	[thread overview]
Message-ID: <70382019-1857-4452-85FC-F038D8BBE206@icloud.com> (raw)
In-Reply-To: <20190816100501.nsvfbdwwnoaihlhe@chaz.gmail.com>

Thanks! I read those links and reached two conclusions: That I should this `yash` out. And that I should look for ways to create files on memory.

(I meant stderr, \(ᵔᵕᵔ)/ )

> On Aug 16, 2019, at 2:35 PM, Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> 
> 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


  reply	other threads:[~2019-08-16 10:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <D639E5A6-A28C-40EE-B0E6-27BED4C8CDDB__31117.9253900022$1565917892$gmane$org@icloud.com>
2019-08-16  7:22 ` Stephane Chazelas
2019-08-16 10:05 ` Stephane Chazelas
2019-08-16 10:45   ` Aryn Starr [this message]
2019-08-16 13:01     ` TJ Luoma
2019-08-16 13:16       ` Roman Perepelitsa
2019-08-16 17:08         ` TJ Luoma
2019-08-15 15:52 Aryn Starr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=70382019-1857-4452-85FC-F038D8BBE206@icloud.com \
    --to=whereislelouch@icloud.com \
    --cc=stephane.chazelas@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).