-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown
More file actions
executable file
·51 lines (46 loc) · 1.08 KB
/
countdown
File metadata and controls
executable file
·51 lines (46 loc) · 1.08 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
#!/bin/bash
USAGE="$0 TIMESPEC [COMMAND {ARGS}]
Do a banner-style countdown starting from TIMESPEC.
TIMESPEC should be either integer seconds, or in one of the forms:
XXm XXmXX XXmXXs XX:XX
For example: 10m, 10m00, 10m0s, 10:00 or 600 are all ways of saying 10 minutes.
If given, COMMAND will be run with ARGS once the timer has completed.
"
now() {
date +%s
}
timestr=$1
BANNER="${BANNER:-banner}"
shift
if grep -Eq '^[0-9]+[m:][0-9]*s?$' <<<"$timestr"; then
# of form XXm, XXmXXs? or XX:XXs?, eg. 10m, 10m30s, 10m30, 10:30
leading_number () { grep -o '^[0-9]*' <<<"$1"; }
mins=$(leading_number "$timestr")
secs=$(leading_number "$(grep -Eo '[0-9]*s?$' <<<"$timestr")")
duration=$((mins*60+secs))
unset leading_number
else
duration="$timestr"
fi
start=$(now)
diff=1
while (( diff > 0 )); do
diff=$(( duration - ($(now) - start) ))
m=$((diff/60))
s=$((diff-m*60))
h=$((m/60))
if [ "$h" -gt 0 ]; then
m=$((m-h*60))
out=$(printf "%d:%02d:%02d" "$h" "$m" "$s")
else
out=$(printf "%02d:%02d" "$m" "$s")
fi
clear
echo
$BANNER "$out"
sleep 1
done
clear
if [ "$#" -gt 0 ]; then
"$@"
fi