Last active
March 12, 2018 03:18
-
-
Save alecdwm/4c3a1f7c15b453e19d0e45cbf53ef95e to your computer and use it in GitHub Desktop.
A small countdown timer in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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