# Perform a certain action on specified time (useful as an alarm clock). function alarm { usage="usage: $(basename $0) HH[:MM[:SS]] [action]" default_action="mpc play" if [[ $# -lt 1 ]]; then print -- $usage return 1 fi wtime=$1 shift action="$default_action" if [[ $# -gt 0 ]]; then action="$@" fi wsplit=(${(s.:.)wtime}) integer H=${wsplit[1]} M=${wsplit[2]:-0} S=${wsplit[3]:-0} if [[ $#wsplit > 3 || $H -ge 24 || $M -ge 60 || $S -ge 60 ]]; then print -- $usage return 1 fi dtime=$(date +%T) ds=(${(s.:.)dtime}) integer CH=${ds[1]} CM=${ds[2]} CS=${ds[3]} (( sleeptime = ($H - $CH) * 3600 + ($M - $CM) * 60 + $S - $CS )) if (( $sleeptime < 0 )); then (( sleeptime = (24*3600) + $sleeptime )); fi sleep $sleeptime print -- "[$(red alarm) $(green $wtime)]: $action" if eval $action; then return 0 fi return 1 }