Skip to content

Instantly share code, notes, and snippets.

@alecdwm
Last active March 12, 2018 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecdwm/4c3a1f7c15b453e19d0e45cbf53ef95e to your computer and use it in GitHub Desktop.
Save alecdwm/4c3a1f7c15b453e19d0e45cbf53ef95e to your computer and use it in GitHub Desktop.
A small countdown timer in bash
#!/bin/bash
# Usage: countdown --help
while test $# -gt 0; do
case "$1" in
"-h"|"--help")
showhelp="true"
shift; continue;;
"-v"|"--version")
showversion="true"
shift; continue;;
"-a"|"--alarm")
file="$HOME/.countdown-alarm.wav"
if [ ! -f "$file" ]; then
echo "File not found: '$file'" >> /dev/stderr
exit 1
fi
shift; continue;;
"-f"|"--alarm-file")
file="$2"
if [ ! -f "$file" ]; then
echo "File not found: '$file'" >> /dev/stderr
exit 1
fi
shift; shift; continue;;
"-m"|"--message")
message="$2"
shift; shift; continue;;
*)
if [ "$time" == "" ]; then
time=$1
else
time="$time $1"
fi
shift; continue;;
esac
done
if [ "$showversion" == "true" ]; then
echo "countdown version 1.0"
exit 0
fi
if [ "$time" == "" ] || [ "$showhelp" == "true" ]; then
echo "Usage: countdown [OPTION]... TIME"
echo "Example: countdown -m \"Food's ready\" 1h 30m"
echo ""
echo "OPTIONS"
echo " -h, --help print help text"
echo " -v, --version print version info"
echo " -a, --alarm use alarm at \$HOME/.countdown-alarm.wav"
echo " -f, --alarm-file FILE use alarm at FILE on timer completion"
echo " -m, --message MESSAGE notify-send MESSAGE on timer completion"
echo ""
echo "See 'man sleep' for details on the formatting for TIME"
echo ""
exit 0
fi
sleep "$time" 2>/dev/null
echo -en "\007" # beep
if [ "$message" != "" ]; then
notify-send "Timer finished: $message"
else
notify-send "Timer finished"
fi
[ -f "$file" ] && mpv --no-vid "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment