-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer
More file actions
executable file
·93 lines (84 loc) · 2.13 KB
/
timer
File metadata and controls
executable file
·93 lines (84 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
# A timer that makes an audible sound after the time's up.
#
# author: andreasl
show_usage() {
echo "Usage: ${0##*/} [-h <hours>] [-m <minutes>] [-s <seconds>] [--alt-melody] [--quiet]"
echo " -h, --hours Set hours"
echo " -m, --minutes Set minutes"
echo " -s, --seconds Set seconds"
echo " -a, --alt-melody Use alternative melody"
echo " -t, --time Set alarm for clock time (HH:MM or HH:MM:SS)"
echo " -q, --quiet Quiet mode"
echo " --help Show this help"
}
[ "$#" -eq 0 ] && {
show_usage
exit 1
}
hours=0
minutes=0
seconds=0
quiet=false
alt_melody=false
target_time=
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --hours)
hours="$2"
shift
;;
-m | --minutes)
minutes="$2"
shift
;;
-s | --seconds)
seconds="$2"
shift
;;
-q | --quiet)
quiet=true
;;
-a | --alt-melody)
alt_melody=true
;;
-t | --time)
target_time="$2"
shift
;;
--help)
show_usage
exit 0
;;
*)
[ "$hours" -eq 0 ] && [ "$minutes" -eq 0 ] && [ "$seconds" -eq 0 ] && seconds="$1"
;;
esac
shift
done
print() { [ "$quiet" != true ] && printf "%b" "$1"; }
if [ -n "$target_time" ]; then
# if absolute time is set via `-t` or `--time`
target_timestamp="$(date -d "$target_time" +%s)"
[ -z "$target_timestamp" ] && exit 2
now="$(date +%s)"
[ "$target_timestamp" -le "$now" ] && target_timestamp="$(date -d "tomorrow ${target_time}" +%s)"
remaining=$((target_timestamp - now))
else
# if relative time is set via `-h`, `-m` and `-s`
remaining=$((hours * 3600 + minutes * 60 + seconds))
fi
while [ "$remaining" -ge 5 ]; do
print "${remaining}..."
sleep 5
remaining=$((remaining - 5))
done
print "${remaining}..."
sleep "$remaining"
notes=(440 554 659 880 659 554 440)
[ "$alt_melody" == true ] && notes=(880 698 587 523 587)
print "Time's up!\n"
for f in "${notes[@]}"; do
timeout 0.2 speaker-test --period 10 --test sine --frequency "$f" >/dev/null 2>&1
done
exit 0