Operating System - HP-UX
1752806 Members
5564 Online
108789 Solutions
New Discussion юеВ

Re: Need Syntax and Manual for trap command

 
Ezhilarasan
Occasional Advisor

Need Syntax and Manual for trap command

Hi,

There is no man page for trap command.
But in our scripts, trap command is
being used in many places.
To understand these funcions, I need the
full syntax and use of this command.

Thanks
R.Ezhil
6 REPLIES 6
Jeff Schussele
Honored Contributor

Re: Need Syntax and Manual for trap command

RAC_1
Honored Contributor

Re: Need Syntax and Manual for trap command

trap syntax is as follows.

trap "commands to be executed" signal_no.

Check man 5 signal, man kill to see if a signal can be trapped.

trap when the said signal is trapped, executes the commands specified.
There is no substitute to HARDWORK
Michael Tully
Honored Contributor

Re: Need Syntax and Manual for trap command

trap is part of the shell. Have a look at the ksh man page and also signal (5)

$ man ksh
$ man signal

Regards
Michael
"When I have trouble spelling, it's called fat finger syndrome"
Anyone for a Mutiny ?
Frank Slootweg
Honored Contributor

Re: Need Syntax and Manual for trap command

For the normal (POSIX) shell, see the sh-posix(1) manual page ("man sh-posix").

To easily 'browse' a manual page, do:

man sh-posix | col -b >/tmp/manpage
view /tmp/manpage

Then use "/" to search for a text or pattern and use "n" to go to the next occurence.
In this case:

/trap
n

will (probably) bring you to the desired "trap [arg] [sig]..." section.
Yogeeraj_1
Honored Contributor

Re: Need Syntax and Manual for trap command

hi,

below a quote from Professor Brown's lecture notes (http://www.ibilce.unesp.br/courseware/unix/unix14.htm)



Shell procedures can use the trap command to disable a signal or redefine its action. The format of the trap command is,

trap arg signal-list

Arg is a string to be interpreted as a command list and signal-list consists of one or more signal numbers as described by the signal keyword.

Number Signal
0 - exit from shell
1 - HANGUP
2 - INTERRUPT character (DELETE or RUB OUT)
3 - QUIT (ctrl-\)
9 - KILL (cannot be caught or ignored)
11 - segmentation violation
15 - software termination signal

The commands in arg are scanned at least once, so it is wise to use single rather than double quotes to surround these commands.

The following shell procedure lists all files ending in .tmp (in /bin and /usr/bin) and traps signals 2, 3 and 15. If a trap occurs, it prints the name of the current directory.

trap 'echo Directory was `pwd` when interrupted' 2 3 15
for i in /bin /usr/bin
do
cd $i
for files in *.tmp
do
echo $files
done
done
Ensure you know which key can generate a trap signal 2. This might involve reconfiguring some telnet options.



The trap command is frequently used to remove temporary files upon termination of a shell procedure.

temp=$HOME/temp/$$
trap 'rm -f $temp; exit' 0 1 2 3 15
ls > $temp
# commands that use $temp here

In this case, the exit command is used to terminate the shell procedure when the trap occurs. At this time, the temporary file is also removed.


hope this helps!

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Shannon Petry
Honored Contributor

Re: Need Syntax and Manual for trap command

O'Reilly and Associates publishes a book called "Unix in a Nutshell", and System Administration guide which both give examples of the trap command.

Trap is a function of the shell, and not a binary command. The list signals to trap is available with "kill -l". Since it is not an external command, but a shell internal there is no man page for trap. Man signal and man ksh/sh will give you better information. Cshell has very different methods ;)

Regards,
Shannon
Microsoft. When do you want a virus today?