trap

I'd like to use "trap" command on my unix machine sunOS 5.7.
But somehow when I do "which trap" command, it's no where to be found.

Any one knows how I can get it installed?

Thanks!!

doing 'man trap' yields:

I saw it too.
But I don't have it.
I can't do "trap", it will tell me command not found
????

what shell are you using?

I am using /usr/bin/sh

strange - works fine under Bourne sh on Sun/Solaris.

Does anybody know what to do?

Thanks!

I just checked on a SunOS 2.7 (aka Solaris 7) box. No surprise /usr/bin/sh has trap command.

whatisthis, "which trap" is never going work. trap is a built-in.

You don't "install" trap. It is a shell built-in. So if your shell supports it, you can use it in a shell script.

From Solaris 7 sh man page:

 trap [ argument n [ n2 ... ]]
       The command argument is to be read and  executed  when
       the  shell receives numeric or symbolic signal\(s\) \(n\).....

For example, to remove some temp files after a normal exit:

#!/usr/bin/sh
<some code>
trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0

You can catch other signals as well.

Cheers,

Keith

Also, if I'd like to remove some files after the signal has been triggered, how do I know when the user triggered it during the program?
The file I need to delete is only created on certain condition.
Is there any way to use if statement with trap?