You are not logged in.

#1 2012-12-22 15:36:53

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

capture the output of `time dosomething` to a variable [SOLVED]

I'd like to have a bash script capture the output of `time dosomething` to a single line variable that I can parse and store.

For example:

% time make -j8
...
...
... tons of make output
...
...
make -j8  541.38s user 26.02s system 726% cpu 1:18.05 total

My interest is only in the last line where time reports the statistics.

Thanks!

Last edited by graysky (2012-12-22 16:00:32)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2012-12-22 15:52:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

time make -j8 > /dev/null 2>&1

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2012-12-22 15:56:10

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

@trilby - It's a null:

% tit=$(time sleep 1s > /dev/null 2>&1)
% echo $tit

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2012-12-22 16:00:22

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

OK... extra/time is what's needed for this...

% /usr/bin/time -f "%e,%P" -o /scratch/log do_something_interesting

The script can then awk out what is important for later processing.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2012-12-22 16:07:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

It seems there are differences between gnu time (/usr/bin/time) and the bash builtin time.  I assume you  are using the former(?)

If so you can check out the -o/--output flag.

EDIT: cross posted with the above which clarifies the half-question.

EDIT: also, with the bash builtin you can use this script:

#!/bin/bash

T=$(sh <<EOF 2>&1
time $@ >/dev/null 2>&1
EOF
)
echo $T

Call it timer, then run

./timer make -j8

The wierdness of that script is because the shell builtin will take everything following time as the command to be run.  In this command you want to redirect all output to  the bit bucket to be ignored.  Then time outputs the time info on stderr.  But you want to redirect *that* stderr to stdout.  Using a subshell was the only way I could come up with, and a here-document made that simple (but the version below is much simpler).  The here document runs the time <command> bit, but that here-document's output is redirected to stdout and then captured in the T variable.

I suspect the -o/--output flag of extra/time could direct to the stdout, but I didn't figure out how.

Last edited by Trilby (2012-12-22 16:32:40)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2012-12-22 16:26:58

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

TIMEFORMAT=%R elapsed=$({ time dostuff >/dev/null; } 2>&1 )

Offline

#7 2012-12-22 17:37:47

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: capture the output of `time dosomething` to a variable [SOLVED]

#!/bin/sh
# Note the redirections both in the command passed to bash and the output of the subshell.
t="$(bash -c 'time sleep 1 >/dev/null 2>&1' 2>&1)"
echo "----------------------------------------"
echo $t
echo "----------------------------------------"
----------------------------------------
real 0m1.001s user 0m0.000s sys 0m0.000s
----------------------------------------

edit: Nvm, just saw Falconindy's example with curly braces for command grouping, which is probably a better approach. I would however redirect STDERR to STDOUT in the internal command to be sure:

t=$({ time sleep 1 >/dev/null 2>&1;} 2>&1 )


Hmmm, maybe not... curly braces take longer on my system:

#!/bin/sh
t="$(bash -c 'time sleep 1 >/dev/null 2>&1' 2>&1)"
echo "----------------------------------------"
echo $t
echo "----------------------------------------"

t=$({ time sleep 1 >/dev/null 2>&1;} 2>&1 )
echo "----------------------------------------"
echo $t
echo "----------------------------------------"
----------------------------------------
real 0m1.002s user 0m0.003s sys 0m0.000s
----------------------------------------
----------------------------------------
real 0m1.003s user 0m0.000s sys 0m0.000s
----------------------------------------

Last edited by Xyne (2012-12-22 17:47:15)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB