About Linux FAQ

Browse More FAQs:

Shell script to get the time difference

Posted by Vivek Gite [Last updated: September 19, 2007]

Q. I am able to write PHP or Perl script where I can find out time difference between script executions. Now I have .shtml file that is nothing but a shell script outputting some data to browser. What I want is time difference or time it took to execute a script. How do I write a shell script?

A. Your Logic should be as follows:

* Get start time and store to a variable START

* Execute a shell script

* Grab output and send to web browser

* Get time again and store to a variable END

* Calculate difference using expression END - START

Shell script o get the time difference

Here is small script that does the same thing (please note that script teated on GNU/Linux and with GNU date command only):
$ vi timediff.bash
Append text as follows:

#!/bin/bash
START=$(date +%s)
# do something

# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here

END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

Save and execute the script as follows:
$ chmod +x timediff.bash
Execute the script:
$ ./timediff.bash
Output:

It took 4 seconds

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Anonymous Says:

    What ifmy script takes mili seconds to execute and I want to be able to measure the time difference even if it is few micro seconds or mili seconds.

    Thanks,

    Ananda

  2. Anonymous Says:

    instead of
    $(date +%s)
    use
    $(date +%s%N)

    and you will get nanoseconds precision


    Mitsuhashi Da! Oboeteoke!

  3. satish asnani Says:

    i need to find the no.of days between system date and the date of file creation. how do i achieve this?

  4. pavana Says:

    i need to find the no.of days and time between system date and the date of file creation. how do i achieve this?

  5. Raj Says:

    Hi, you time diff script doesn’t work. date +%s returns only %s as value. And thus it does not work forward.

  6. Ratna Says:

    The above mentioned script will not work because “%s” is given in small letter.
    Replace “%s” with “%S” and the script will work.

  7. seema Says:

    can i have the similar script in k-shell

  8. Johannes Says:

    Cool script, thanks!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.