Skip to content

Instantly share code, notes, and snippets.

@esoupy
Last active December 10, 2015 18:58
Show Gist options
  • Save esoupy/4478235 to your computer and use it in GitHub Desktop.
Save esoupy/4478235 to your computer and use it in GitHub Desktop.
bash script Timer function. Creates a stopwatch and displays the time duration between calls.
#!/bin/bash
## Timer Function - displays the timed duration between start and stop ##
# example:
# Timer start
# Script_Cmds
# Timer stop
Timer() {
## Set a timer to display duration ##
# Usage: Timer [start|stop]
Tcmd=$1
case $Tcmd in
start|Start|START) _StartSecs=$(date +%s)
;;
stop|Stop|STOP) _StopSecs=$(date +%s)
[[ ! $_StartSecs ]] && echo "[Internal Error] $FUNCNAME did not record a start." && return
_DiffSecs=$(($_StopSecs-$_StartSecs))
TimeLapse=$(date -u -d@"$_DiffSecs" +'%-Hh%-Mm%-Ss')
echo "Timer: $TimeLapse"
;;
*) echo "[Internal Error] $FUNCNAME: Unknown arg '$Tcmd'"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment