how to use trap in sh?

how to use trap in sh?

Post by Aihua » Tue, 12 Mar 1996 04:00:00



Can anybody help me to use trap in sh?
A simple script is:
---------
#!/bin/sh
trap "echo trapped" 1 2 15
trap "echo ok" 0
exit 1
echo continue
---------
The result is
ok

I expect
trapped

What's wrong?
Thank you.


 
 
 

how to use trap in sh?

Post by Peter Turnbul » Thu, 14 Mar 1996 04:00:00



> Can anybody help me to use trap in sh?
> A simple script is:
> ---------
> #!/bin/sh
> trap "echo trapped" 1 2 15
> trap "echo ok" 0
> exit 1
> echo continue
> ---------
> The result is
> ok

> I expect
> trapped

> What's wrong?
> Thank you.



The problem is that you are confusing exit codes with signals.
Do a 'man signal' to get a list of all the signals

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Ph: 018-978656                          
------------------------------------------------------------------------
Lobbying the government to increase the height on Mt Kosiuskco by
another 1000 metres so that it will have a permanent snow cover.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

 
 
 

how to use trap in sh?

Post by Cliff Ca » Thu, 21 Mar 1996 04:00:00



>Can anybody help me to use trap in sh?
>A simple script is:
>---------
>#!/bin/sh
>trap "echo trapped" 1 2 15
>trap "echo ok" 0
>exit 1
>echo continue
>---------
>The result is
>ok
>I expect
>trapped
>What's wrong?
>Thank you.


The trap code executes in response to receiving signal values
specified on the latter part of the trap command.

When exiting a script a signal number 0 is generated which is what
causes the unexpected response.

To test the trap code, one needs to place the script in a perpetual
loop in the background and then send signals with kill and quit
keyboard sequences.

Remember that triggering the trap code will not cause automatic
termination of the script.  The trap code should end with an exit
(after disabling the trap 0 if set)

 
 
 

1. Execute a sh script under perl and sh: sh script; perl script?

I can execute the following perl scripts under either shell or Perl
====

        if 0;
[perl scripts ...]
====
i.e.
sh script
perl script

How do I execute a sh script under either shell or Perl
simililar to what shown above?

Thanks.
--
Michael Wang

http://www.mindspring.com/~mwang    

2. Raw Informix 723uc3 db backup

3. strange traps in HP-UX /bin/sh

4. sed question

5. signal trapping problem in [k]sh...

6. scsi logging

7. ksh != sh on traps

8. NetBSD-1.1 (i386) and hard disk..

9. trapping a kill -9 in sh

10. Error trap/detection question in sh

11. How secure is trap sh on UNIX generally, AIX in particular?

12. How to clean up when killed (trap in sh)?

13. Can someone explain the 'trap' command in /bin/sh?