Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by choroba (Cardinal)
on Apr 07, 2010 at 07:36 UTC ( [id://833216]=note: print w/replies, xml ) Need Help??


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

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]"'

Replies are listed 'Best First'.
Re^4: Time command output is not stored into the variable.
by almut (Canon) on Apr 07, 2010 at 09:20 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://833216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found