[ale] bash function to show elapsed time

Todor Fassl fassl.tod at gmail.com
Mon Nov 17 12:33:14 EST 2014


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
	}




More information about the Ale mailing list