Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
immediately with an exit status greater than 128, immediately after which the trap is executed.<br />
12.2.3. More examples<br />
12.2.3.1. Detecting when a variable is used<br />
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
When debugging longer scripts, you might want to give a variable the trace attribute and trap DEBUG<br />
messages for that variable. Normally you would just declare a variable using an assignment like<br />
VARIABLE=value. Replacing the declaration of the variable with the following lines might provide valuable<br />
information about what your script is doing:<br />
declare -t VARIABLE=value<br />
trap "echo VARIABLE is being used here." DEBUG<br />
# rest of the script<br />
12.2.3.2. Removing rubbish upon exit<br />
The whatis command relies on a database which is regularly built using the makewhatis.cron script with<br />
cron:<br />
#!/bin/bash<br />
LOCKFILE=/var/lock/makewhatis.lock<br />
# Previous makewhatis should execute successfully:<br />
[ -f $LOCKFILE ] && exit 0<br />
# Upon exit, remove lockfile.<br />
trap "{ rm -f $LOCKFILE ; exit 255; }" EXIT<br />
touch $LOCKFILE<br />
makewhatis -u -w<br />
exit 0<br />
12.3. Summary<br />
Signals can be sent to your programs using the kill command or keyboard shortcuts. These signals can be<br />
caught, upon which action can be performed, using the trap statement.<br />
Some programs ignore signals. The only signal that no program can ignore is the KILL signal.<br />
12.4. Exercises<br />
A couple of practical examples:<br />
1. Create a script that writes a boot image to a diskette using the dd utility. If the user tries to interrupt<br />
the script using Ctrl+C, display a message that this action will make the diskette unusable.<br />
Chapter 12. Catching signals 140