Last active
February 15, 2018 13:30
-
-
Save tobiisdesired/b3f08e3116bff540e30414c34a32376c to your computer and use it in GitHub Desktop.
Ever needed a well formatted countdown (aka tea timer) in your console?
This file contains hidden or 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 | |
# | |
# Simple tea timer | |
# | |
start=$(date +%s) | |
function print_help() { | |
cat <<EOF | |
usage: $(basename $0) DRUATION | |
DURATION is a positive number followed by | |
s for seconds (the default) | |
m for minutes | |
h for hours or | |
d for days | |
EOF | |
exit 1 | |
} | |
if [[ $1 =~ ([[:digit:]]+)([smhd])? ]]; then | |
NUMBER=${BASH_REMATCH[1]} | |
UNIT=${BASH_REMATCH[2]:-s} | |
else | |
print_help | |
fi | |
num=$NUMBER | |
case $UNIT in | |
m) | |
num=$((num*60)) | |
;; | |
h) | |
num=$((num*60*60)) | |
;; | |
d) | |
num=$((num*60*60*24)) | |
;; | |
esac | |
while true; do | |
sec_rem=$(($num - ($(date +%s) - $start))) | |
printf "\r\t%02dh:%02dm:%02ds\t" $((sec_rem /3600)) $((sec_rem % 3600 /60)) $((sec_rem % 60)) | |
[ $sec_rem -le 0 ] && { echo; exit 0; } | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment