Cleanup countdown and at scripts

This commit is contained in:
Daniel Flanagan 2024-04-26 11:35:28 -05:00
parent 6b1654ea47
commit f386180bb7
2 changed files with 21 additions and 8 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
usage() { usage() {
echo "at - waits until after the specified datetime" echo "at - exit after the specified datetime"
echo "Usage:" echo "Usage:"
echo " at <DATETIME> && command..." echo " at <DATETIME> && command..."
echo echo
@ -9,13 +9,17 @@ usage() {
echo " at 15:00:00 && echo \"it is 3 o'clock\"" echo " at 15:00:00 && echo \"it is 3 o'clock\""
} }
[[ -z "$1" ]] && { echo "Error: No DATE argument provided." >&2; usage; exit 1; } [[ -z "$1" ]] && { echo "error: no DATE argument provided" >&2; usage; exit 1; }
d="$(date -d "${@}" +%s)" d="$(date -d "${@}" +%s)"
_dt=$((d - $(date +%s)))
days=$((_dt / 86400))
printf 'Started at %s (with duration %sd %s)\n' "$(date)" "$days" "$(date -u --date @$((_dt)) +%H:%M:%S)"
while [[ "$d" -ge "$(date +%s)" ]]; do while [[ "$d" -ge "$(date +%s)" ]]; do
_dt=$((d - $(date +%s))) _dt=$((d - $(date +%s)))
days=$((_dt / 86400)) days=$((_dt / 86400))
echo -ne "\rTime Remaining: ${days}d $(date -u --date @$((_dt)) +%H:%M:%S) "; printf "\r%sd %s " "$days" "$(date -u --date @$((_dt)) +%H:%M:%S)";
sleep 0.1 sleep 0.1
done done
exit 0 exit 0

View file

@ -1,15 +1,24 @@
#!/usr/bin/env bash #!/usr/bin/env bash
[[ $# -lt 1 ]] && { echo "No argument provided." >&2 ; exit 1; } function usage {
echo "countdown - exit after a certain amount of time has passed"
echo " Usage:"
echo " countdown <SECONDS> && command..."
echo
echo " Examples:"
echo ' countdown 120 && echo "Two minutes has elapsed!"'
}
[[ $# -lt 1 ]] && { printf "error: no SECONDS argument provided\n" >&2; usage; exit 1; }
d=$(($(date +%s) + $1)); d=$(($(date +%s) + $1));
echo "Countdown started at $(date)" printf 'Started at %s\n' "$(date)"
while [[ "$d" -ge "$(date +%s)" ]]; do while [[ "$d" -ge "$(date +%s)" ]]; do
_dt=$((d - $(date +%s))) _dt=$((d - $(date +%s)))
days=$((_dt / 86400)) days=$((_dt / 86400))
echo -ne "\r${days}d $(date -u --date @$((_dt)) +%H:%M:%S) "; printf "\r%sd %s " "$days" "$(date -u --date @$((_dt)) +%H:%M:%S)";
sleep 0.1 sleep 0.1
done done
echo -ne "\rCountdown finished $(date)\n" printf "\rCountdown finished %s\n" "$(date)"