-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
countdown.sh
executable file
·54 lines (45 loc) · 2.59 KB
/
countdown.sh
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
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# This is a simple script to display a countdown timeer with optional message. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Countdown #
# -------------------------------------------------------------------------------- #
# Loop for the required numbers of seconds and display the message. We have to #
# re-eval each time to ensure that the string is updated correctly. #
# -------------------------------------------------------------------------------- #
function countdown()
{
local seconds=${1:-0}
local message=${2:-''}
while [ "${seconds}" -gt 0 ]; do
eval_message=$(eval "echo ${message}")
echo -ne "${eval_message}\033[0K\r"
sleep 1
: $((seconds--))
done
echo
}
# -------------------------------------------------------------------------------- #
# Test #
# -------------------------------------------------------------------------------- #
# A VERY simple test function to ensure that it all works #
# -------------------------------------------------------------------------------- #
function run_tests()
{
# shellcheck disable=SC2016
countdown 3 'Processing will start in $seconds seconds. Press Ctrl+C to abort'
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# This is the actual 'script' and the functions/sub routines are called in order. #
# -------------------------------------------------------------------------------- #
run_tests
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #