If you try to retrieve information about alarm, using the standard ksh93 builtin options (--man or --help) only a single usage line is outputted.
$ alarm --manDave Korn has stated that -r means repeat the alarm every interval, varname is the name of the variable which is invoked, and interval can be the absolute time from the Epoch or of the form +nseconds. Milliseconds are also supposedly supported but I have not tested this functionality.
Usage: alarm [-r] [varname interval]
$
Internally, alarm is an ksh93 built-in which adds an alarm discipline to a function or variable. This built-in sets up an interval timer that will call the alarm discipline as needed.
For our first example, consider the following trivial example which uses alarm to display the time every 2 seconds
alarm -r mytime +2The first line specifies that the function mytime is to be called every 2 seconds. The second line defines the mytime function i.e. print the current time in HH:SS format. The third line is just a hack so that you can see the results of the previous 2 lines.
function mytime.alarm { print -n "$(date +%H:%S)\r"; }
read dummy # dummy wait
Note that ksh93 is very picky as regards how statements are written when discipline functions are used. For example, if you place the mytime function definition before the alarm statement, ksh93 will produce an error, i.e. "mytime.alarm: invalid discipline function" and exit the script.
Next is an example that demonstrates how to abort a script after a waiting 10 seconds for input from a user. Note that the ksh93 read command has a timeout option but we are not using it here. This example also shows you how to remove an alarm using the unset built-in.
alarm -r abortscript +10Here is the output when this example is run.
abortscript.alarm()
{
print "Exceeded allowed time. Aborting script ......"
kill -9 $$
}
print "waiting for input from read"
read dummy # dummy wait - press return to continue
unset abortscript
print "abortscript unset. Press return to exit"
read dummy # dummy wait - press return to continue
exit 0
$ ./example2My final example shows how alarm can be used to update a progress bar once a second on a terminal.
waiting for input from read
Exceeded allowed time. Aborting script ......
Killed
$
$ ./example2
waiting for input from read
abortscript unset. Press return to exit
$
alarm -r progressbar +1Again the order of things in the script is very important. First we specify the function that alarm is going to use, next we define the function itself and then we set the compound variable values.
progressbar.alarm()
{
tput cup 2 10
(( ++progressbar.pvalue > 50 )) && {
tput cup 2 10; tput el
progressbar.pvalue=0
}
printf "Progress: %s" "${progressbar.pstr:1:${progressbar.pvalue}}"
}
progressbar.pvalue=0
progressbar.pstr="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
tput clear
read dummy?"Press RETURN to exit"
unset progressbar
exit 0
Use alarm with caution since Dave Korn has also stated that "alarm hasn't been documented because it is not working in all cases." The examples shown in this post were tested using ksh93 version s.
0 comments:
Post a Comment