-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.sh
executable file
·328 lines (244 loc) · 6.12 KB
/
alarm.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
###########################################################################
#
# Shell program to generate a beeping alarm.
#
# Copyright 2000-2002, William Shotts <[email protected]>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# This software is part of the LinuxCommand.org project, a site for
# Linux education and advocacy devoted to helping users of legacy
# operating systems migrate into the future.
#
# You may contact the LinuxCommand.org project at:
#
# http://www.linuxcommand.org
#
# Description:
#
# This program causes your computer to beep until you press the
# enter key. This is useful when placed after a long process to
# alert the user that something requires his/her attention. The
# program will accept options to control the rate of beeping (the
# default is 1 beep per second) and the prompting message.
#
# Usage:
#
# alarm [ -h | --help ] [-s seconds] [-m message]
#
# Options:
#
# -h, --help Display this help message and exit.
# -s seconds seconds between beeps
# -m message message to display
#
#
# Revisions:
#
# 04/02/2000 File created
# 04/02/2000 Completed first version (1.0.0)
# 02/17/2002 Cosmetic changes (1.0.1)
#
# $Id: alarm,v 1.2 2002/02/18 14:18:38 bshotts Exp $
###########################################################################
###########################################################################
# Constants
###########################################################################
PROGNAME=$(basename $0)
VERSION="1.0.1"
TEMP_FILE=/tmp/${PROGNAME}.$$
###########################################################################
# Functions
###########################################################################
function clean_up
{
#####
# Function to remove temporary files and other housekeeping
# No arguments
#####
# Kill background process if it is still running
if [ "$pid" != "" ]; then
kill $pid
fi
}
function graceful_exit
{
#####
# Function called for a graceful exit
# No arguments
#####
clean_up
exit
}
function error_exit
{
#####
# Function for exit due to fatal program error
# Accepts 1 argument
# string containing descriptive error message
#####
echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
clean_up
exit 1
}
function term_exit
{
#####
# Function to perform exit if termination signal is trapped
# No arguments
#####
echo "${PROGNAME}: Terminated"
clean_up
exit
}
function int_exit
{
#####
# Function to perform exit if interrupt signal is trapped
# No arguments
#####
echo "${PROGNAME}: Aborted by user"
clean_up
exit
}
function usage
{
#####
# Function to display usage message (does not exit)
# No arguments
#####
echo "Usage: ${PROGNAME} [-h | --help] [-s seconds] [-m message]"
}
function helptext
{
#####
# Function to display help message for program
# No arguments
#####
local tab=$(echo -en "\t\t")
cat <<- -EOF-
${PROGNAME} ver. ${VERSION}
This is a program to generate a beeping alarm.
$(usage)
Options:
-h, --help Display this help message and exit.
-s seconds seconds between beeps
-m message message to display
-EOF-
}
function check_seconds
{
#####
# Verify that seconds is a number
# Returns number to stdout if seconds is a
# number, returns nothing otherwise.
# Arguments:
# 1 number of seconds (required)
#####
# Fatal error if required arguments are missing
if [ "$1" = "" ]; then
error_exit "check_seconds: missing argument 1"
fi
echo "$1" | awk '/^[0-9]*$/ { print $0 }'
} # end of check_seconds
function string
{
#####
# Emulate the old BASIC function "string$"
# Arguments:
# 1. character
# 2. length
#####
local length
# Fatal error if required arguments are missing
if [ "$1" = "" ]; then
error_exit "string: missing argument 1"
fi
if [ "$2" = "" ]; then
error_exit "string: missing argument 2"
fi
# Make sure that argument 2 is actually a number
length=$(echo "$2" | awk '/^[0-9]*$/ { print $0 }')
if [ -z "$length" ]; then
error_exit "string: argument must be a number"
fi
# Make the string
awk -v char=$1 -v len=$length '
BEGIN {
str = ""
for (i=0; i < len; i++) {
str = str char
}
print str
}'
} # end of string
function message_display
{
######
# Output a string with a border
# Arguments:
# 1. string
#####
local length str
# Fatal error if required arguments are missing
if [ "$1" = "" ]; then
error_exit "message_border: missing argument 1"
fi
length=$(echo $1 | awk '{ print length($0) }')
str=$(string "-" $length)
echo $str
echo $1
echo $str
} # end of message_border
###########################################################################
# Program starts here
###########################################################################
# Trap TERM, HUP, and INT signals and properly exit
trap term_exit TERM HUP
trap int_exit INT
# Process command line arguments
if [ "$1" = "--help" ]; then
helptext
graceful_exit
fi
# Process arguments - edit to taste
seconds=1
message="${PROGNAME}: Press enter to continue "
pid=
while getopts ":hs:m:" opt; do
case $opt in
s ) seconds=$OPTARG
if [ "$(check_seconds $seconds)" = "" ] ; then
error_exit "seconds must be a number"
fi ;;
m ) message="${PROGNAME}: ${OPTARG} - Press Enter to continue" ;;
h ) helptext
graceful_exit ;;
* ) usage
exit 1
esac
done
# Launch beeping sub-shell and put it in the background
( while true; do
echo -en "\007"
sleep $seconds
done ) &
# Get process ID of sub-subshell
pid=$!
# Display prompt and wait for user to press enter key
message_display "$message"
read foo
# Now that key has been pressed, kill sub-shell
kill $pid
# All done - make pid empty so clean_up won't kill anything
pid=
graceful_exit