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



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