-
Notifications
You must be signed in to change notification settings - Fork 2
/
sleep-countdown
executable file
·55 lines (46 loc) · 1.25 KB
/
sleep-countdown
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
#!/bin/bash
# Gokberk Cinbis, 2021
# set -e --> DONT! not compatible with wait() usage below (or handle its return signal)
function helpandexit() {
echo "USAGE: sleep-countdown [--keyinterrupt] <seconds>"
echo "If --keyinterrupt is given, any keypress immediately stops the countdown."
exit 1
}
function sleep_countdown_func() {
local narg=$#
if [ $narg -eq 1 ]
then
secs=$1
while [ $secs -gt 0 ]; do
echo -ne "Wait $secs seconds...\033[0K\r"
sleep 1
: $((secs--))
done
elif [ $narg -eq 2 ]
then
# https://linuxhint.com/bash_wait_keypress/
secs=$2
if [[ "$1" == "--keyinterrupt" ]]
then
:
else
echo "sleep-countdown - unrecognized argument: $1"
exit 2
fi
while [ $secs -gt 0 ]; do
echo -ne "Wait $secs seconds... (press any key to skip countdown) \033[0K\r"
command read -t 1 -n 1
if [ $? = 0 ] ; then
secs=0
else
: $((secs--))
fi
done
else
helpandexit
fi
echo -ne "\033[0K\r" # cleanup
exit 0
}
# ok with multiple quoted arguments
sleep_countdown_func "$@"