1834813 Members
2698 Online
110070 Solutions
New Discussion

Re: trap

 
SOLVED
Go to solution
Khashru
Valued Contributor

trap

what does the folloing line mean in /home/user/.profile

trap : 1 2 3
13 REPLIES 13
Senthil Kumar .A_1
Honored Contributor

Re: trap

Hi Khashru,

Trap command is usually used in shell scripts to trap signals. To know different signal's and its significance try man pag of kill command.

This command command syntax is

trap {commands} {signal number list}


For example let us assume, you shell script creates lot of temp files in the course of execution.When I kill the shell script while it is running using

kill -15 PID, This actually kills the process and exits without any additional execution. Instead you shell scripts sets trap like the folowing..

trap "rm /tmp/tempfile" 15

then the sript will execute the command "rm /tmp/tempfile" when its killed with the signal 15.

I hope this explanation helped,

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Arunvijai_4
Honored Contributor

Re: trap

Hi Khashru,

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=30026

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: trap

Hi Khashru,

Some important signals are,

Signals 1(hangup)
Singnal 2(interrupt)
Singnal 3(quit)
Singnal 0 (shell exit)
Signal 15 (terninate)

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Khashru
Valued Contributor

Re: trap

what is : after trap command. it is used to stop ctrl+c.
Sivakumar TS
Honored Contributor

Re: trap

Hi,

By giving the follwoing enty in profile.

trap " " 1 2 3

you can "trap"/"supress" those signals.

it means... if the user executes Ctrl+C in a menu, the program won ger terminated and he wont get the shell prompt. Its done for security reasons.

With Regards,

Siva.
Nothing is Impossible !
Khashru
Valued Contributor

Re: trap

yes. but I typed trap : 1 2 3 not trap "". I want to know why : not ""
Sivakumar TS
Honored Contributor
Solution

Re: trap


Hi Khashru,

: is equivallent to "" itself....

go thru the follwing,

server[/]#trap
trap -- 'echo '\''logout root'\' EXIT
server[/]#trap : 1 2 3
server[/]#trap
trap -- : QUIT
trap -- : INT
trap -- : HUP
trap -- 'echo '\''logout root'\' EXIT
server[/]#trap "" 4
server[/]#trap
trap -- '' ILL
trap -- : QUIT
trap -- : INT
trap -- : HUP
trap -- 'echo '\''logout root'\' EXIT


With Regards,

Siva.
Nothing is Impossible !
Muthukumar_5
Honored Contributor

Re: trap

I typed trap : 1 2 3 not trap "". I want to know why : not ""

--

trap is using syntax as,

trap "function execution" signal number

trap "" 1 2 3

"" -> Null string .. If it gets the singal then it will do anything.

signal()
{
echo "Got signal"
}

trap "signal" 1 2 3

when ever it gets signal it will call signal shell function.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: trap

It is always better to use man pages to get more information.

See ksh man page for trap command.

% trap [arg] [sig ...]
arg is a command read and executed when the shell
receives signal(s) sig. (Note that arg is scanned once
when the trap is set and once when the trap is taken.)
Each sig can be given as a number or name of the
signal. Trap commands are executed in signal number
order. Any attempt to set a trap on a signal that was
ignored upon entering the current shell has no effect.
If arg is omitted or is -, all traps for sig are reset
to their original values. If arg is the null string,
this signal is ignored by the shell and by the commands
it invokes. If sig is DEBUG, arg is executed after
each command. If sig is ERR, arg is executed whenever
a command has a non-zero exit code. If sig is 0 or
EXIT and the trap statement is executed inside the body
of a function, the command arg is executed after the
function completes. If sig is 0 or EXIT for a trap set
outside any function, the command arg is executed on
exit from the shell. The trap command with no
arguments prints a list of commands associated with
each signal number.

--
Muthu
Easy to suggest when don't know about the problem!
Sivakumar TS
Honored Contributor

Re: trap

sorry there was a typo error..

it NOT equivallent.

and setting : dosent trap the signals.
Nothing is Impossible !
Khashru
Valued Contributor

Re: trap

but it works. i have used : and it works.
Arunvijai_4
Honored Contributor

Re: trap

Check this thread for Control -C

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=943834

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: trap

Hi Khashru,

Also, worth reading these pages,

http://unix.ittoolbox.com/groups/technical-functional/hp-ux-l/893492
http://unix.ittoolbox.com/groups/technical-functional/hp-ux-l/893922

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"