zsh-users
 help / color / mirror / code / Atom feed
* Redirect output of time builtin
@ 2011-01-31  0:39 Matthias Vallentin
  2011-01-31  4:00 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Vallentin @ 2011-01-31  0:39 UTC (permalink / raw)
  To: zsh-users

Is it possible to redirect output from the time builtin? I use it
quite often (in addition to $REPORTTIME) and would like to parse its
output in scripts or put it my prompt.

Any ideas how to do this? 

    Matthias


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

* Re: Redirect output of time builtin
  2011-01-31  0:39 Redirect output of time builtin Matthias Vallentin
@ 2011-01-31  4:00 ` Bart Schaefer
  2011-07-13  8:46   ` Matthias Vallentin
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2011-01-31  4:00 UTC (permalink / raw)
  To: zsh-users

On Jan 30,  4:39pm, Matthias Vallentin wrote:
}
} Is it possible to redirect output from the time builtin?

Sure.  It's printed to stderr.  The trick is that it's parsed as if

    time { pipeline including redirects }

so to redirect the output of time rather than the output of pipeline,
you must put in your own braces:

    { time pipeline including redirects } 2> stderr.txt

In order to split the stderr of time from the stderr of the pipeline,
you need to get a little trickier:

    { time simple command 2>&3 } 3>&2 2>time.txt

It might be easier to see what's going on there if you write the
redirections in the order that they are actually performed by the
shell when executing the command:

    3>&2 2>time.txt { time 2>&3 simple command }

Thus, 3 becomes the same as 2, then 2 goes to the file time.txt
(leaving 3 pointing where 2 used to point), then time implicitly
grabs 2 for its stderr, then 2 becomes the same as 3 (back to the
original 2 again, but leaving time's stderr directed to time.txt),
and finally the simple command runs with 2 as its stderr.

I replaced the pipeline with a simple command to keep the explanation
short; if you have an actual pipeline rather than a simple command,
placement of the 2>&3 becomes more complicated.

Additional note:  The time builtin applied to any construct that is
executed in the current shell, is silently ignored.  So although
it's syntactically OK to put an opening curly or a repeat-loop or
the like immediately after the time keyword, you'll get no timing
statistics.  You have to use parens instead, to force a subshell,
which is then timed.


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

* Re: Redirect output of time builtin
  2011-01-31  4:00 ` Bart Schaefer
@ 2011-07-13  8:46   ` Matthias Vallentin
  2011-07-13 15:12     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Vallentin @ 2011-07-13  8:46 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

> In order to split the stderr of time from the stderr of the pipeline,
> you need to get a little trickier:
> 
>     { time simple command 2>&3 } 3>&2 2>time.txt

I was exactly looking for this sort of splitting. What I am wondering is
whether there is a way to avoid hitting the disk and instead redirect
the output of time into some piece in memory. Non-blocking named papes
came to mind, but I haven't figured out whether this is the right
approach:

    mkfifo /tmp/fifo
    { time command 2>&3 } 3>&2 2<>/tmp/fifo

The above fails if there is no process reading from /tmp/fifo when the
time output is written. In fact, a FIFO would be overkill since I only
need constant space to store the output rather than a message pipe.

Ideally the output would be written into an environment variable. Is
this doable?

    Matthias


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

* Re: Redirect output of time builtin
  2011-07-13  8:46   ` Matthias Vallentin
@ 2011-07-13 15:12     ` Bart Schaefer
  2011-07-19 14:50       ` Matthias Vallentin
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2011-07-13 15:12 UTC (permalink / raw)
  To: zsh-users

On Jul 13,  1:46am, Matthias Vallentin wrote:
} Subject: Re: Redirect output of time builtin
}
} > In order to split the stderr of time from the stderr of the pipeline,
} > you need to get a little trickier:
} > 
} >     { time simple command 2>&3 } 3>&2 2>time.txt
} 
} I was exactly looking for this sort of splitting. What I am wondering is
} whether there is a way to avoid hitting the disk and instead redirect
} the output of time into some piece in memory.

time_out=$( { time simple command 2>&3 >&/dev/tty } 2>&1 3>&2 )

This isn't useful in a pipeline, though, and you may need to replace
>&/dev/tty with explicit shuffling of descriptors 1 and 2, and I don't
promise that interactive commands won't be seriously messed up.


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

* Re: Redirect output of time builtin
  2011-07-13 15:12     ` Bart Schaefer
@ 2011-07-19 14:50       ` Matthias Vallentin
  0 siblings, 0 replies; 5+ messages in thread
From: Matthias Vallentin @ 2011-07-19 14:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

> time_out=$( { time simple command 2>&3 >&/dev/tty } 2>&1 3>&2 )
> 
> This isn't useful in a pipeline, though, and you may need to replace
> >&/dev/tty with explicit shuffling of descriptors 1 and 2, and I don't
> promise that interactive commands won't be seriously messed up.

That works well for most my use cases, thanks!

    Matthias


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

end of thread, other threads:[~2011-07-19 14:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-31  0:39 Redirect output of time builtin Matthias Vallentin
2011-01-31  4:00 ` Bart Schaefer
2011-07-13  8:46   ` Matthias Vallentin
2011-07-13 15:12     ` Bart Schaefer
2011-07-19 14:50       ` Matthias Vallentin

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