From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18096 invoked by alias); 23 Jan 2013 07:37:08 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17603 Received: (qmail 6274 invoked from network); 23 Jan 2013 07:36:56 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at linux.vnet.ibm.com does not designate permitted sender hosts) Date: Wed, 23 Jan 2013 15:36:25 +0800 From: Han Pingtian To: zsh-users@zsh.org Cc: rahul Subject: Re: Capture stderr to variable without new process Message-ID: <20130123073625.GA10056@localhost.localdomain> References: <20130121233549.GA6494@redoubt.spodhuis.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20130121233549.GA6494@redoubt.spodhuis.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13012307-5806-0000-0000-00001EA0CF32 On Mon, Jan 21, 2013 at 06:35:49PM -0500, Phil Pennock wrote: > On 2013-01-22 at 01:41 +0530, rahul wrote: > > I guess the typical way would be: > > > > ERR=$(popd 2>&1) > > > > However, the command cannot run in a sub-shell as it would have no effect > > in the current shell. > > Is writing to a file the only way ? > > If the output can only be one line and you have an unbuffered cat(1) (-u > option). > > % coproc cat -u > [1] 6550 > % popd 2>&p > % read -p foo > % echo $foo > popd: directory stack empty Good method. I think we can improve it by using a loop and add a timeout to read: % coproc cat % git abc 2>&p % while read -t 2 -p line;do ERR+=$line$'\n';done % print "$ERR" git: 'abc' is not a git command. See 'git --help'. Did you mean this? add % > % coproc - This is interesting. How could we figure out this method, please? > > Otherwise, you need to script a cat-command which can exit on a sentinel > line; I don't know of a way to close the coproc's stdin while leaving > the coproc running to collect its output later. Perhaps someone else > does? If you could send EOF on the coproc's stdin, then you could just > use: > popd_stderr="$(cat <&p)" > after closing its stdin. > > -Phil