Defining and Using Traps

As you will see, your script can trap for errors and the exit condition. These are probably the most commonly trapped-for events. Can other things be trapped? Let's answer that by examining the meaning of a trap.

Traps are typically described as asynchronous events. The event is asynchronous because of its unpredictability. An example is a Ctrl+C being pressed while your script is running. The event of the Ctrl+C is totally unpredictable from the perspective of your script. During one run, it might occur after 20 seconds; during another run, it might occur after 2 hours. And during a third run, it might not occur at all.

You may be thinking that a Ctrl+C causes an interrupt, not a trap. The trap is the action of reacting ...

Get Korn Shell Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.