Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Time command output is not stored into the variable.

by ungalnanban (Pilgrim)
on Apr 07, 2010 at 06:45 UTC ( #833202=note: print w/ replies, xml ) Need Help??


in reply to Re: Time command output is not stored into the variable.
in thread Time command output is not stored into the variable.


I know the time functions but I want to store the time command output in to a variable.
The time command output is printed in STDERR.
How can I store the output in to a variable, you have any idea?


--$ugum@r--


Comment on Re^2: Time command output is not stored into the variable.
Re^3: Time command output is not stored into the variable.
by Anonymous Monk on Apr 07, 2010 at 06:52 UTC
      please do not write your reply inside the signature divs
      I agree. In fact, some Monks completely filter out signature divs, as described in Signatures and Node Templates. For those Monks, the OP's postings appear to have no content.
Re^3: Time command output is not stored into the variable.
by choroba (Abbot) on Apr 07, 2010 at 07:36 UTC
    The output of time is printed to STDERR, but after all the redirections are performed. Therefore, to capture it, you have to start time in a subshell. This has nothing to do with perl, though.
    perl -e '$a=`(time ls) 2>&1 >/dev/null`;print "[$a]"'

      In addition to what you said, there's also the problem that the output the OP wants likely is from the bash builtin "time".  Perl, however, is typically configured to call /bin/sh as the default shell (see perl -V:sh). And as sh doesn't know the time builtin (even if /bin/sh is just a link to /bin/bash — for compatibility reasons, the program name is what matters here), the shell will search for an external executable "time", which usually is /usr/bin/time. The latter produces a different output format, though.

      To sum up, what the OP probably wants is

      my $times = `/bin/bash -c "(time ls) 2>&1 >/dev/null"`;

      Compare

      #!/usr/bin/perl -l $times = `(time ls) 2>&1 >/dev/null`; print "/usr/bin/time:\n[$times]"; $times = `/bin/bash -c "(time ls) 2>&1 >/dev/null"`; print "bash builtin:\n[$times]"; __END__ /usr/bin/time: [0.00user 0.00system 0:00.00elapsed 114%CPU (0avgtext+0avgdata 0maxres +ident)k 0inputs+0outputs (0major+396minor)pagefaults 0swaps ] bash builtin: [ real 0m0.008s user 0m0.004s sys 0m0.004s ]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833202]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-08-22 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls