-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcntdown.sh
144 lines (121 loc) · 3.22 KB
/
cntdown.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
#
# Copyright © 2022-2023 Chee Bin HOH. All rights reserved.
#
# A front end to cntdown
#
# Happy coding! .... note that I am always a C programmer before
# I am anything else in computing, so the habit of declaring variable
# in the proper scope even thought bourne shell has a loose scope
# definition and variable just spawn into existence in outer scope
# if it is assigned value in inner scope.
displayHelp()
{
echo "Usage: `basename $0` [-H hours -M minutes -S seconds -b -s -m \"message at end\" -f \"%Y-%m-%d %H:%M:%S\"] \"2022-01-21 12:00:00\""
}
# parameters parsing
silent=""
beep=""
adjustHour=""
adjustMin=""
adjustSecond=""
while getopts "H:M:S:bm:sf:h" opt; do
case "$opt" in
H)
adjustHour="${OPTARG}"
;;
M)
adjustMin="${OPTARG}"
;;
S)
adjustSecond="${OPTARG}"
;;
b)
beep="yes"
;;
s)
silent="yes"
;;
f)
formatOption="-f ${OPTARG}"
;;
m)
msg=${OPTARG}
;;
*)
displayHelp
exit 1
esac
done
# FIXME: put up an autoconf system to detect version of date and then
# customize the script file accordingly, the other way is to use more
# platform independent language, like python, but who need python when
# you have bourne shell.
#
# for some reason if we try to run "date" as a command inside the "if",
# like "if date -v +1H 2>dev/null; then ..." two instances of the command
# is spawned in ubuntu shell for WSL that I test, something that I do not
# quite experience before, so I change it to capture output to force shell
# to wait for the command to run and only one instance is run. I plan to
# look into that, but this is not needed if I put it into autoconf
dateResult=$(date -v +1H 2>/dev/null)
if [ "$dateResult" = "" ]; then
linuxDate="yes"
else
linuxDate=""
fi
adjustDateTimeParams=""
if [ "$adjustHour" != "" ]; then
if [ "$linuxDate" = "yes" ]; then
while [ "$adjustHour" -gt "0" ]; do
adjustDateTimeParams="${adjustDateTimeParams} next hour"
adjustHour=$((adjustHour - 1))
done
else
adjustDateTimeParams="-v +${adjustHour}H"
fi
fi
if [ "$adjustMin" != "" ]; then
adjustDateTimeParams="$adjustDateTimeParams -v +${adjustMin}M"
fi
if [ "$adjustSecond" != "" ]; then
adjustDateTimeParams="$adjustDateTimeParams -v +${adjustSecond}S"
fi
dateTimeVal=""
if [ "$adjustDateTimeParams" != "" ]; then
if [ "$linuxDate" = "yes" ]; then
dateTimeVal=`date --date="$adjustDateTimeParams" +"%Y-%m-%d %H:%M:%S"`
else
dateTimeVal=`date $adjustDateTimeParams "+%Y-%m-%d %H:%M:%S"`
fi
fi
shift $((OPTIND-1))
if [ "$#" -eq "1" ]; then
dateTimeVal="$*"
fi
if [ "$dateTimeVal" = "" ]; then
displayHelp
exit 1
fi
# main body
remain=`./cntdown.out ${formatOption+"$formatOption"} "$dateTimeVal"`
runResult=$?
if [ "$runResult" -ne "0" ]; then
exit $runResult
fi
while [ "${remain}" -gt "0" ]; do
if [ "${silent}" != "yes" ]; then
echo "\r \c" # a quick hack :)
echo "\r${remain}\c";
fi
sleep 1;
remain=`./cntdown.out ${formatOption+"$formatOption"} "$dateTimeVal"`
done
if [ "${remain}" -eq "0" ]; then
if [ "${silent}" != "yes" ]; then
echo "\r${msg:-0}"
fi
if [ "${beep}" == "yes" ]; then
echo '\007\c'
fi
fi