Results 1 to 10 of 13
Hi,
I am writing a bash script to time disk read and writes have come across a problem timing the duration of the command and saving it as a variable. ...
- 11-14-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
How to grab the timed duration of a command and save it as a variable
Hi,
I am writing a bash script to time disk read and writes have come across a problem timing the duration of the command and saving it as a variable. It would seem that I need to use either the shell's builtin time command or /usr/bin/time.
This snippet shows the problem:
grma@grma-lap:$ TIMEFORMAT=%R
grma@grma-lap:$ READTIME=`time cp -f /testfile-188MB /tmp`
3.455
grma@grma-lap:$ echo $READTIME
grma@grma-lap:$
The time is displayed on the terminal and the variable $READTIME is empty.
How do I get the variable $READTIME to be the elapsed time of the command.
Any help gladly appreciated
Rgds
Gary
- 11-14-2007 #2If stderr is not a problem, just:Code:
READTIME="$({ time cp -f /testfile-188MB /tmp 2>/dev/null ;} 2>&1)"
Code:READTIME="$({ time cp -f /testfile-188MB /tmp;} 2>&1)"
- 11-14-2007 #3
Yep, you have a problem there.
bash's time command outputs the time not to standard error or standard output, but (apparently) /dev/tty, making it difficult to capture it unless you want to mess with script or expect or something.
And if you do this at the command line:
you'll discover the free-standing time program, independent of bash, which (at least on my ancient Slackware 9.1 system) does not accept the TIMEFORMAT variable, even if you export it as an environment variable.Code:which time
So. Can I sell you a watch?
Well, not exactly a watch. A timex. Or, more precisely, a script called timex. And not exactly sell. Just take the script and put it in your $PATH.
If you don't care about the output from the program you're timing, do this:Code:wally:~/wednesday/2$ cat timex #!/bin/sh begin_time=$(date +%s.%N) "$@" >&3 end_time=$(date +%s.%N) echo "$end_time - $begin_time" | bc wally:~/wednesday/2$ READTIME=`timex sleep 5 3> wherestandardoutputshouldgo` wally:~/wednesday/2$ echo $READTIME 5.013724000 wally:~/wednesday/2$
Note that TIMEFORMAT is not used anywhere.Code:READTIME=`timex sleep 5 3>/dev/null`
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 11-14-2007 #4
Well, darn, radoulov, while I was testing my solution, you slipped in there with a better one!
My hat would be off to you, if I had one.
Wait. I do have one. So it's off to you.--
Bill
Old age and treachery will overcome youth and skill.
- 11-14-2007 #5Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
Thanks radoulov,
The command you suggest works just as I want.
May I ask you to explain the command line so that I can better understand what the problem was and what you have done to get over it? ie why the double quote, the dollar sign and the brackets and braces?
- 11-14-2007 #6
See wooledge.org's BashFAQ, question 32.
The $() is the new prefered syntax
(and has nothing to do with the time problem).
- 11-14-2007 #7
- 11-16-2007 #8
I solved this before with the time built-in and it's really making me mad I can't recall how lol.
But, just so you know there is a GNU time and a bash built in time. The GNU time goes to stderr. It can be found in /usr/bin
- 11-16-2007 #9
All time variants (shell keywords and external programs) I tested
write to stderr.
- 11-16-2007 #10
Oh, really?
What do you get when you run this script?
I get:Code:#!/bin/bash echo about to do it plain time sleep 3 echo about to redirect stderr time sleep 3 2> standard_error.txt echo did we get the stats to the screen, even with redirected stderr? echo === beginning of what was in stderr cat standard_error.txt echo === end of what was in stderr
Code:wally:~/friday/1$ 1.sh about to do it plain real 0m3.005s user 0m0.000s sys 0m0.000s about to redirect stderr real 0m3.010s user 0m0.000s sys 0m0.000s did we get the stats to the screen, even with redirected stderr? === beginning of what was in stderr === end of what was in stderr wally:~/friday/1$
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
