If you have followed this blog for a while, you should remember how to use variables in Unix shell scripts.
Going further, I'd like to show you some basics of working with time and date in your scripts – generating all sorts of timestamps and timing some parts of your script for reporting purposes.
The history of (Unix) time
Most of Unix-like system today store and calculate this time according to Unix epoch, which means they all have an internal counter which counts the number of seconds elapsed since midnight of January 1, 1970.
As you can imagine, this is a pretty big number (it passed 1,000,000,000 seconds back in 2001, just so that you know), and reporting in directly would not be very useful to real humans. Because of this, most of Unix time and date reporting commands and functions do the conversion from Unix time into something more meaningful to the end user.
While for the vast majority of time and date related tasks this conversion is good, a task of timing certain events can sometimes benefit from using raw second counters and simply subtracting them when needed.
Getting Unix time and date
In Unix shells, the easiest way to get a current time and date is to use the date command:
ubuntu$ date Tue Jun 10 10:46:07 IST 2008
date is a very smart command, and apart from this default behavior it supports template system for printing the current time and date – so you can use it to report only specific part of the time and date like the current hour or the day of a week or the year.
Read the man page for date (type man date in your shell prompt) to learn all the details, but this is how you use date output templates:
ubuntu$ date "+%b %d, %Y" Jun 10, 2008
In this example, the %b parameter in this template represents the short name of the current month, %d is the day of the month, and %Y is the four-digit representation of the current year.
Timing parts of your Unix script
If you're after timing some parts of your script, you'll need to use two variables: one for saving the time before the start of an observed part of the script, and another one for saving the time after the same piece of code.
These two variables will be used to subtract the time and report the elapsed time in seconds, so in order to do this we'll need the date command to report time in seconds. That's why I've given you an introduction to Unix time earlier: date reports the number of seconds since the Unix epoch:
ubuntu$ date +%s 1213091896
If you run it just a few seconds later, you'll see a different number:
ubuntu$ date +%s 1213091922
That's why, if such values are saved and then subtracted, we'll get the elapsed time in seconds.
Here's a simple script showing how this is done:
#!/bin/bash # START=`date +%s` echo "Script start time (Unix epoch): $START" # echo "- sleeping for 3 seconds..." sleep 3 echo "- sleeping for 2 seconds more..." sleep 2 # FINISH=`date +%s` echo "Script finish time (Unix epoch): $FINISH" # ELAPSED=`expr $FINISH - $START` echo "Elapsed time: $ELAPSED"
And if you run it, you will see this output:
ubuntu$ /tmp/time-example.sh Script start time (Unix epoch): 1213092131 - sleeping for 3 seconds... - sleeping for 2 seconds more... Script finish time (Unix epoch): 1213092131 Elapsed time: 5
That's all I wanted to show you today. In future posts, I'll show you a few more things you can do regarding timing and timestamps in Unix. Until then – good luck with your scripting, and feel free to ask if you need any more help!
If you want to learn more, here's a great book:
Share and Enjoy