Skip to content

Instantly share code, notes, and snippets.

@weirane
Last active February 15, 2020 15:16
Show Gist options
  • Save weirane/4f83e5c4e5e636c55cdf1f61e11cabf6 to your computer and use it in GitHub Desktop.
Save weirane/4f83e5c4e5e636c55cdf1f61e11cabf6 to your computer and use it in GitHub Desktop.
Simple countdown script
#!/bin/bash
# Count down for a specified time
# Example (count down for 3 minutes and 30 seconds):
# $ countdown 3m 30s
shopt -s extglob
time=0
while [ -n "$1" ]; do
case $1 in
+([0-9]))
parsed=$1
;;
+([0-9])s)
parsed=${1%s}
;;
+([0-9])m)
raw=${1%m}
parsed=$((raw * 60))
;;
+([0-9])h)
raw=${1%h}
parsed=$((raw * 3600))
;;
+([0-9])d)
raw=${1%d}
parsed=$((raw * 86400))
;;
*)
echo >&2 "Invalid: '$1'"
exit
;;
esac
time=$((time + parsed))
shift
done
trap 'tput cnorm' EXIT
tput civis
digits=$(echo "scale=0; l($time)/l(10) + 1" | bc -l)
while [ "$time" -gt 0 ]; do
time=$((time - 1))
printf "\r%${digits}d" $time
sleep 1
done
date=$(date +%T)
printf "\r%s: Time is up\n" "$date"
notify-send --expire-time=0 'countdown' "$date: time is up"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment