Linux Command Line and Shell Scripting Bible by Richard Blum; Christine Bresnahan

Linux Command Line and Shell Scripting Bible by Richard Blum; Christine Bresnahan

Author:Richard Blum; Christine Bresnahan
Language: eng
Format: mobi, azw3
Tags: Reference:Computers
ISBN: 9781118004425
Publisher: Wiley
Published: 2008-01-02T10:00:00+00:00


trap “echo byebye” EXIT

count=1

while [ $count -le 5 ]

do

echo “Loop #$count”

sleep 3

count=$[ $count + 1 ]

done

$

$ ./test2

Loop #1

Loop #2

Loop #3

Loop #4

Loop #5

byebye

$

When the script gets to the normal exit point, the trap is triggered, and the shell executes the command you specify on the trap command line. The EXIT trap also works if you prematurely exit the script:

$ ./test2

Loop #1

Loop #2

ˆCbyebye

$

When the Ctrl+C key combination is used to send a SIGINT signal, the script exits (because that signal isn't listed in the trap list), but before the script exits, the shell executes the trap command.

Removing a Trap

You can remove a set trap by using a dash as the command and a list of the signals you want to return to normal behavior:

$ cat test3

#!/bin/bash

# removing a set trap



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.