Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

how to record what time returns?

Hi

When doing:

$ VAR=`time echo foo > /dev/null`

real ...
user ...
sys ...

But when echo $VAR nothing is displayed!!

I want to record in a variable that real/user/sys lines

how to do it?
0
Kyroii
Asked:
Kyroii
  • 7
  • 5
  • 2
  • +1
2 Solutions
 
ahoffmannCommented:
some shells have their own built-in time, unfortunately ...
bash's time for example insist on writting to the tty directly (heaven knows why)-:
use /usr/bin/time or check your man-page of the shell
0
 
TintinCommented:
/usr/bin/time -o /tmp/$$ echo foo
VAR=$(cat /tmp/$$)

0
 
NovaDenizenCommented:

VAR=`time echo foo > /dev/null`
Your command redirects the stdout of "time echo foo" into /dev/null.  "time" prints its time information onto stderr, not stdout.

VAR=`time echo foo 2>&1 > /dev/null`
The '2>&1 > /dev/null' has the effect of sending the stdout to /dev/null, and causing what used to come out on stderr to come out through the original stdout, allowing the time stderr output to be captured in $VAR.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:
NovaDenizen.

Don't know what shell/Linux version you use, but what you have written is not correct for all Linux versions I've tried.

ahoffmann's answer explains why.
0
 
NovaDenizenCommented:
Ok, mine worked on solaris' /bin/sh.  Looks like it doesn't work in bash.
0
 
TintinCommented:
bash (and ksh) have a shell builtin called time.

However, I've confirmed that on Linux, you can do:

var=`/usr/bin/time echo foo 2>&1 >/dev/null`
0
 
ahoffmannCommented:
use csh/tcsh (with built-in time) and all dragons are gone :-D

unset var; csh -c 'set var = `time echo foo`; echo $var'
0
 
KyroiiAuthor Commented:
Thk u for answers

But I need to use Bash's time. Any more Ideas?
0
 
ahoffmannCommented:
http:#13813427  works in any shell ;-)
0
 
TintinCommented:
What's wrong with using /bin/time?
0
 
KyroiiAuthor Commented:
Not installed /bin/time in the computer and can't install it.

It also has not installed csh

Cheers
0
 
ahoffmannCommented:
do you have tcsh?
0
 
TintinCommented:
What Unix flavour are you running?  I don't know of any that don't have the time command as standard.

0
 
TintinCommented:
Actually, I've just discovered that the bash time builtin *does* write to STDERR.  The reason you don't see it when you do:

time ls 2>output

is that STDERR only applies to the child process (ls in this instance), as time is being run in the parent process  If you run the whole lot in a subshell, eg:

(time ls) 2>output

You can capture the output from the time builtin.
0
 
ahoffmannCommented:
my bash writes on the tty :-(
0
 
TintinCommented:
ahoffman.

What version of bash are you running?

I've tested against:

GNU bash, version 2.04.21(1)-release (i386-redhat-linux-gnu)

GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)

GNU bash, version 2.05b.0(1)-release (powerpc-redhat-linux-gnu)

GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)

GNU bash, version 2.05b.0(1)-release (i386-portbld-freebsd4.7)

GNU bash, version 2.05a.0(1)-release (i386-pc-linux-gnu)

GNU bash, version 2.05.8(1)-release (i386-checkpoint-linux-gnu)

GNU bash, version 2.05b.0(1)-release (i686-pc-cygwin)

And none of those versions writes directly to /dev/tty
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now