10.8. Interrupt Handling

 <  Day Day Up  >  

If a script is interrupted with the Interrupt key, it terminates and control is returned to the C shell; that is, you get your prompt back. The onintr command is used to process interrupts within a script. It allows you to either ignore the interrupt (^C) or transfer control to another part of the program before exiting. Normally, the interrupt command is used with a label to "clean up" before exiting. The onintr command without arguments restores the default action.

Example 10.49.
 (The Script) 1  onintr  finish  2         < Script continues here > 3  finish:  4  onintr   # Disable further interrupts  5    echo Cleaning temp files 6    rm $$tmp* ; exit 1 

EXPLANATION

  1. The onintr command is followed by a label name . The label finish is a user -defined label; control will be transferred to the finish label if an interrupt occurs. Usually this line is at the beginning of the script. It is not in effect until it is executed in the script.

  2. The rest of the script lines are executed unless ^C (Interrupt key) is pressed while the program is in execution, at which time, control is transferred to the label.

  3. This is the label; when the interrupt comes in, the program will continue to run, executing the statements below the label.

  4. To shield this part of the script from further interrupts, the onintr “ is used. If ^C is entered now, it will be ignored.

  5. This line is echoed to the screen.

  6. All tmp files are removed. The tmp files are prefixed with the shell's PID ( $$ ) number and suffixed with any number of characters . The program exits with a status of 1.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net