zsh-users
 help / color / mirror / code / Atom feed
From: Philippe Troin <phil@fifi.org>
To: Nikolai Weibull <now@bitwi.se>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Is it possible to capture stdout and stderr to separate variables in Zsh?
Date: Tue, 06 Mar 2012 09:16:25 -0800	[thread overview]
Message-ID: <1331054185.27052.19.camel@air.fifi.org> (raw)
In-Reply-To: <CADdV=Mtbszh2NjtB8yFjfeo9PR1U7XV-Qi2AQMXCZ0Ag0VTS+w@mail.gmail.com>

On Tue, 2012-03-06 at 09:09 +0100, Nikolai Weibull wrote:
> Is it possible to capture stdout and stderr to separate variables in Zsh?
> 
> I understand that it’s not possible in sh, but I was wondering if any
> of Zsh’s additions in the redirection area would allow for such a
> separation.

All I can think of is:

        coproc cat &
        pid=$!
        stdout="$( ( print "printed on stdout"; print -u 2 "printer on stderr" ) 2>&p )"
        sleep 1 
        kill "$pid"
        stderr="$(cat <&p)"
        print "stdout=\"$stdout\""
        print "stderr=\"$stderr\""

You'll notice the very ugly sleep+kill hack I had to use as I could not
find how you can close a coprocess's standard input cleanly.  Removing
the sleep+kill makes the cat <&p hang forever.

Of course this solution will hang if more than a buffer-full is printed
on stderr.  You can play tricks with dd by providing a bigger buffer, as
in:

        coproc dd obs=1M &

But in the end it's a losing game.  You might as well use a temporary
file for one of the output streams.

A completely different solution could involve the tcp zsh module which
can multiplex many streams with tcp_expect.  But that's probably too
involved for this problem.

Phil.


  reply	other threads:[~2012-03-06 17:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-06  8:09 Nikolai Weibull
2012-03-06 17:16 ` Philippe Troin [this message]
2012-03-07  7:01   ` Bart Schaefer
2012-03-07  9:26     ` Nikolai Weibull
2012-03-08 14:53       ` Bart Schaefer
2012-03-30  7:15         ` Nikolai Weibull
2012-03-30 14:49           ` Bart Schaefer
2012-09-10  6:16       ` Han Pingtian
2012-03-07 19:37     ` Philippe Troin
2012-03-08  4:56       ` Bart Schaefer
2012-09-01 11:59     ` Han Pingtian
2012-09-09 19:18       ` Bart Schaefer
2012-08-31  0:40 ` Paul Maisano

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=1331054185.27052.19.camel@air.fifi.org \
    --to=phil@fifi.org \
    --cc=now@bitwi.se \
    --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).