You are not logged in.
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 packages • Zsh and other configs
Offline
time make -j8 > /dev/null 2>&1
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@trilby - It's a null:
% tit=$(time sleep 1s > /dev/null 2>&1)
% echo $tit
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
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 packages • Zsh and other configs
Offline
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
#!/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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline