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

You probably want to use Perl's built-in times function.

  • Comment on Re: Time command output is not stored into the variable.

Replies are listed 'Best First'.
Re^2: Time command output is not stored into the variable.
by ungalnanban (Pilgrim) on Apr 07, 2010 at 06:45 UTC

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