Bash scripting is a requirement in production environments. On your personal computer, be it a MAC or Linux! (Windows scripting is important and one can use bash provided CYGWIN or similar is installed). However, the point here is sometimes you want to know how long your script runs.
Here is a quick, 30 seconds or less implementation to add to your script to have a proper time stamp issued in a log or to terminal.
It's really simple, just using the date command and exporting that in to an environment variable to save the start. Than issuing another date at the scripts operational end.
The mechanism used to save the start in an environment variable, just as an example this was named as TimeNowEnv.
This is saved upon operational end as we issue the call to date to get another timestamp (In this case the end). Once this is displayed we issue an 'unset TimeNowEnv' just to clean things up. This could be issued several times in the script so this is just a housekeeping task.
In file TimeMe.sh
export TimeNowEnv=$(date)
echo 'Start: ' $TimeNowEnv
sleep 30
echo 'End:' $(date)
unset TimeNowEnv
Execution is ./TimeMe.sh and will print the start, than, to simulate an operation the script sleeps (sleep 30) for 30 seconds and than prints the operational end. Output below:
[prod@ZAP scripts]$ ./TimeMe.sh
Start: Mon Feb 1 14:49:21 CST 2010
End: Mon Feb 1 14:49:51 CST 2010