-
Notifications
You must be signed in to change notification settings - Fork 3
/
stopwatch_countdown_JBs.sh
executable file
·81 lines (77 loc) · 2.33 KB
/
stopwatch_countdown_JBs.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
#!/bin/bash
#
# Autor= João Batista Ribeiro
# Bugs, Agradecimentos, Críticas "construtivas"
# me envie um e-mail. Ficarei Grato!
# e-mail: joao42lbatista@gmail.com
#
# Com contibuições de Rumbler Soppa (github.com/rumbler)
#
# Este programa é um software livre; você pode redistribui-lo e/ou
# modifica-lo dentro dos termos da Licença Pública Geral GNU como
# publicada pela Fundação do Software Livre (FSF); na versão 2 da
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança que possa ser útil,
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a
# qualquer MERCADO ou APLICAÇÃO EM PARTICULAR.
#
# Veja a Licença Pública Geral GNU para mais detalhes.
# Você deve ter recebido uma cópia da Licença Pública Geral GNU
# junto com este programa, se não, escreva para a Fundação do Software
#
# Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script: stopwatch and countdown function
# https://superuser.com/questions/611538
#
# Last update: 23/08/2023
#
stopwatch() {
start=$(date +%s)
while true; do
time="$(( $(date +%s) - $start))"
printf ' %s\r' "$(date -u -d "@$time" +%H:%M:%S)"
sleep 1s
done
}
countdown() {
time_value=$1
start="$(( $(date '+%s') + $time_value))"
while [ $start -ge $(date +%s) ]; do
time="$(( $start - $(date +%s) ))"
printf ' %s\r' "$(date -u -d "@$time" +%H:%M:%S)"
sleep 1s
done
}
help() {
echo -e "\nHelp message:\nUsage: $0 [option]"
echo -e " s - Stopwatch"
echo -e " c \"time\" - Countdown with \"time\" in seconds"
echo -e " h - Show this help message\n"
exit 0
}
if [ "$#" -lt 1 ]; then
echo -e "\n$(basename "$0"): Error - need the pass option"
help
fi
option_value=$1
time_value=$2
case $option_value in
's' )
echo -e "\n Stopwatch - Ctrl + C to terminate"
stopwatch ;;
'c' )
echo -e "\n Countdown of \"$time_value\" seconds"
if [ "$time_value" == '' ]; then
echo -e "\n$(basename "$0") c : Error - need the \"time\" value"
help
else
countdown "$time_value"
echo -e "\n\n End of countdown\n"
fi ;;
"h" )
help ;;
* )
echo "Error: Not recognized option \"$option_value\"" ;;
esac