zsh-users
 help / color / mirror / code / Atom feed
* Capture stderr to variable without new process
@ 2013-01-21 20:11 rahul
  2013-01-21 23:35 ` Phil Pennock
  0 siblings, 1 reply; 3+ messages in thread
From: rahul @ 2013-01-21 20:11 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 200 bytes --]

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 ?

-- 
 rahul

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Capture stderr to variable without new process
  2013-01-21 20:11 Capture stderr to variable without new process rahul
@ 2013-01-21 23:35 ` Phil Pennock
  2013-01-23  7:36   ` Han Pingtian
  0 siblings, 1 reply; 3+ messages in thread
From: Phil Pennock @ 2013-01-21 23:35 UTC (permalink / raw)
  To: rahul; +Cc: zsh-users

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
% coproc -

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Capture stderr to variable without new process
  2013-01-21 23:35 ` Phil Pennock
@ 2013-01-23  7:36   ` Han Pingtian
  0 siblings, 0 replies; 3+ messages in thread
From: Han Pingtian @ 2013-01-23  7:36 UTC (permalink / raw)
  To: zsh-users; +Cc: rahul

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-01-23  7:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-21 20:11 Capture stderr to variable without new process rahul
2013-01-21 23:35 ` Phil Pennock
2013-01-23  7:36   ` Han Pingtian

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).