Skip to content

Instantly share code, notes, and snippets.

@ekimekim
Created May 29, 2013 06:40
Show Gist options
  • Save ekimekim/5668382 to your computer and use it in GitHub Desktop.
Save ekimekim/5668382 to your computer and use it in GitHub Desktop.
A countdown timer that uses the sysv banner(1) program to display the time remaining. May optionally run a command when timer finishes.
#!/bin/bash
USAGE="$0 TIMESPEC [COMMAND {ARGS}]
Do a banner-style countdown starting from TIMESPEC.
TIMESPEC should be either integer seconds, or in one of the forms:
XXm XXmXX XXmXXs XX:XX
For example: 10m, 10m00, 10m0s, 10:00 or 600 are all ways of saying 10 minutes.
If given, COMMAND will be run with ARGS once the timer has completed.
"
timestr=$1
shift
if egrep -q '^[0-9]+[m:][0-9]*s?$' <<<"$timestr"; then
# of form XXm, XXmXXs? or XX:XXs?, eg. 10m, 10m30s, 10m30, 10:30
leading_number () { grep -o '^[0-9]*' <<<"$1"; }
mins=`leading_number "$timestr"`
secs=`leading_number $(egrep -o '[0-9]*s?$' <<<"$timestr")`
n=$((mins*60+secs))
unset leading_number
else
n="$timestr"
fi
for ((; n>0; n--)); do
m=$((n/60))
s=$((n-m*60))
clear
echo
banner "`printf "%02d:%02d" "$m" "$s"`"
sleep 1
done
[ "$#" -gt 0 ] && "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment