Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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. ...
  1. #1
    Just 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

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    READTIME="$({ time cp -f /testfile-188MB /tmp 2>/dev/null ;} 2>&1)"
    If stderr is not a problem, just:

    Code:
    READTIME="$({ time cp -f /testfile-188MB /tmp;} 2>&1)"

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    which time
    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.

    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.
    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$
    If you don't care about the output from the program you're timing, do this:
    Code:
    READTIME=`timex sleep 5 3>/dev/null`
    Note that TIMEFORMAT is not used anywhere.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  5. #5
    Just 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?

  6. #6
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    See wooledge.org's BashFAQ, question 32.

    The $() is the new prefered syntax
    (and has nothing to do with the time problem).

  7. #7
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by wje_lf View Post
    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.
    Nooo,
    this was simple

  8. #8
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    All time variants (shell keywords and external programs) I tested
    write to stderr.

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oh, really?

    What do you get when you run this script?

    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
    I get:
    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.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...