1748136 Members
3628 Online
108758 Solutions
New Discussion юеВ

trap question

 
kamal_15
Regular Advisor

trap question

I want to know what is the (trap).

i read about it .but i coudn't use it
can any one help me?

i want an example
6 REPLIES 6
Michael Schulte zur Sur
Honored Contributor

Re: trap question

Hi,

trap is a shell command to ignore certain signals like ^C or hangup. See the manual page of the shell you are using. It is used like
trap 1 2 15 ""
You specify the signals you want to catch and the action that is to be taken when a signal occurs.

greetings,

Michael
kamal_15
Regular Advisor

Re: trap question

thank u man

but i don't understand how can i receive the
output from trap?

EX:
****
if i want to execute a sample shell
and i want to receive a signals explane the status.

what command can i write to do that

NOTE:
i have hp-ux V.10
when i type
man trap
no manual entry


thanx
Stephen Keane
Honored Contributor

Re: trap question

man trap wont show anything because trap is part of the shell. Try man ksh instead and look for trap

e.g.

trap 15 "echo \"Hello\""

Stephen Keane
Honored Contributor

Re: trap question

If you do
# kill -l
it will show you the names of the signals and their numeric values. e.g.

15 = TERM or SIGTERM

Michael Selvesteen_2
Trusted Contributor

Re: trap question

Trap is an interrupt signal (CTRL-C) usually kills the process.

Processes may be sent signals using either the kill command, or a control key combination such as CTRL-C.

The trap command typically appears as one of the first lines in the shell script. It contains the commands to be executed when a signal is detected as well as what signals to trap.


#!/bin/sh
TMPFILE=/usr/tmp/junk.$$
trap 'rm -f $TMPFILE; exit 0' 1 2 15
.
.
.

Upon receiving signals 1, 2 or 15, $TMPFILE would be deleted and the script would terminate the shell script normally. This shows how trap may be used to clean up before exiting.


#!/bin/sh
TMPFILE=/usr/tmp/junk.$$
trap '' 0 1 2
.
.
.

The above example shows how trap may be used to ignore specific signals (0, 1 and 2).

NOTE that when the signal is received, the command currently being executed is interrupted, and execution flow continues at the next line of the script.

Hope this helps

--
M
Gerhard Roets
Esteemed Contributor

Re: trap question

Hi Kamal

For future reference.

The command "man sh-posix" would be a good place to look for infomation regarding built in shell commands.

Regards
Gerhard