Skip to Content
Learning Linux Shell Scripting
book

Learning Linux Shell Scripting

by Ganesh Sanjiv Naik
December 2015
Beginner
306 pages
5h 2m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting

Using traps in function

If we use the trap command inside a function in script, then the reassigned signal behavior will become global inside a script. We can check this effect in the following script example.

Let's write Shell script trap_01.sh as follows:

#!/bin/bash
trap "echo  caught signal SIGINT" SIGINT
trap "echo  caught signal SIGQUIT" 3
trap "echo  caught signal SIGTERM" 15
while :
do
    sleep 50
done

Let's test the program as follows:

$ chmod +x trap_01.sh
$ ./ trap_01.sh

Output:

^Ccaught signal SIGINT
^\Quit (core dumped)
caught signal SIGQUIT

Let's write one more Shell script trap_02.sh as follows:

#!/bin/bash trap "echo caught signal SIGINT" SIGINT trap "echo caught signal SIGQUIT" 3 trap "echo caught signal SIGTERM" 15 trap "echo caught signal ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting - Second Edition

Learning Linux Shell Scripting - Second Edition

Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9781785286216