Skip to content

Instantly share code, notes, and snippets.

@courtney-rosenthal
Created March 29, 2016 15:18
Show Gist options
  • Save courtney-rosenthal/25f3306c166b91efce9e to your computer and use it in GitHub Desktop.
Save courtney-rosenthal/25f3306c166b91efce9e to your computer and use it in GitHub Desktop.
Display a text countdown for a given number of seconds.
#!/bin/bash
#
# countdown - Display a text countdown for a given number of seconds.
#
# Chip Rosenthal
# chip@unicom.com
USAGE="usage: $0 secs"
if [[ $# -ne 1 ]] ; then
echo "$USAGE" >&2
exit 1
fi
nsecs="$1"
if [[ ! $nsecs =~ ^[0-9]+$ ]] ; then
echo "$USAGE" >&2
exit 1
fi
tnow=`date "+%s"`
tstart=$(( tnow ))
tstop=$(( tstart + nsecs ))
while (( $tnow < $tstop )) ; do
tremain=$(( tstop - tnow ))
echo -e "\\r${tremain} ... \\c"
sleep 1
tnow=`date "+%s"`
done
echo -e "\\rdone "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment