From afb8bb68b42a97b60c7ff9d9ea27b8567bb49a2a Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Fri, 29 Apr 2022 02:57:43 -0700 Subject: [PATCH] sleep_until: safety --- .config/shell/functions/sleep-until | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.config/shell/functions/sleep-until b/.config/shell/functions/sleep-until index c5d787c..34fdb28 100644 --- a/.config/shell/functions/sleep-until +++ b/.config/shell/functions/sleep-until @@ -4,9 +4,13 @@ sleep_until() { if [ -n "$1" ] && date --date="$*" > /dev/null 2>&1; then - echo "Sleeping until: $(date --date="$*")" - - sleep $(( $(date -f - +%s- <<< "$*"$'\nnow') 0 )) + SLEEP_TIME="$(( $(date -f - +%s- <<< "$*"$'\nnow') 0 ))" + if [ "$SLEEP_TIME" -gt 0 ]; then + echo "Sleeping until: $(date --date="$*") (${SLEEP_TIME}s)" + sleep "$SLEEP_TIME" + else + echo "Sadly, you cannot sleep to a time in the past" + fi else echo "Please input a date" fi