alarm.sh 928 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. clear
  4. while true; do
  5. if [[ -z "${1:-}" ]]; then
  6. echo "Set alarm time in HH:MM AM/PM format"
  7. read -r ALARM_TIME
  8. else
  9. ALARM_TIME="$1"
  10. fi
  11. if [[ "$ALARM_TIME" =~ (1[0-2]|0?[1-9]):([0-5]?[0-9])(\s?[AP]M)? ]]; then
  12. echo "Alarm set to $ALARM_TIME"
  13. break
  14. else
  15. echo "Invalid time format"
  16. set --
  17. unset ALARM_TIME
  18. fi
  19. done
  20. run_at="$(date -d "$ALARM_TIME" +%s)"
  21. current_time="$(date +%s)"
  22. duration_to_sleep=$(( "$run_at"-"$current_time" ))
  23. if [[ "$duration_to_sleep" -le "0" ]]; then
  24. echo "Can't set an alarm at past time"
  25. exit
  26. fi
  27. sleep "$duration_to_sleep"
  28. notify-send "${2:-Alarm}" --icon=dialog-information
  29. awk &>/dev/null '
  30. BEGIN {
  31. k = 10
  32. while(k--) {
  33. i=j=500
  34. while (i--) print "a\0\0\0" | "aplay"
  35. while (j--) print "z\0\0" | "aplay"
  36. }
  37. }
  38. '
  39. exit