[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ale] bash function to show elapsed time
- Subject: [ale] bash function to show elapsed time
- From: fassl.tod at gmail.com (Todor Fassl)
- Date: Mon, 17 Nov 2014 11:33:14 -0600
I thought I'd share a bash function I wrote this morning. It displays
elapsed time in hours, minutes, & seconds. You call it like this:
elapsed 0
[do something]
elapsed
[do something else]
elapsed
You canreset the start time by passing it any value as the first
parameter. I used zero in the example above because it looks a little
more clear. You could just as easily use "elapsed reset" or "elapsed start".
The function:
function elapsed
{
if [ -z "$t1" -o ! -z "$1" ]; then
t1=`date +"%s"`
else
t2=`date +"%s"`
diff=$((t2-t1))
hours=$((diff/3600))
diff=$((diff-(hours*3600)))
mins=$((diff/60))
secs=$((diff-(mins*60)))
printf "Elapsed %02d:%02d:%02d\n" $hours $mins $secs
fi
}