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 24150
- From: Aryn Starr <whereislelouch@xxxxxxxxxx>
- To: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- Subject: Re: Capture stdout, stdin, and exit status in different variables without using temporary files
- Date: Fri, 16 Aug 2019 15:15:49 +0430
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1565952357; bh=1aOh1xVkBdEUMWCAOzoW23ipxvibmLJLvf8K/KCAMQ8=; h=Content-Type:Subject:From:Date:Message-Id:To; b=JQDGHRABijQsIT33zfexjysqNyrbjv5Q1zam7W5KLC5G1QndC6LiWKCxNkapRvdge KwEpv3HjY1J43cURPMCTVyZ+DK4pePFH/BhWfVzpurt/bH7JhKAP/kjbZANM+ddgMx 265dfHHkpa5YDgfPGACtXz87bkAfIv40nEATLBSOpSv8HUh+mr0oTUFYL7wxPXfSyU 68G+VvsDf63dD0ioFQNLis02CSPtZIYbQhgsfTFOBp9isTO/l40/YGa+ibPbWbB0mP x3h0sEFxUlwqKvcC8P5aNXlPSdWMW+FP5P1o5hGr7wtfIdxkOevTUAgPq/jVJGQRNM bJt/aWUYFku6A==
- In-reply-to: <20190816100501.nsvfbdwwnoaihlhe@chaz.gmail.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>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <D639E5A6-A28C-40EE-B0E6-27BED4C8CDDB__31117.9253900022$1565917892$gmane$org@icloud.com> <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@xxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author