| Logan Shaw 2007-01-23, 7:31 am |
| Jeff wrote:
> Can someone answer this question about TRAP. I have trap specified
> in the main shell for the correct exits to remove some tmp files. My
> shell calles a function, if there is a error within the called function
> the trap does not remove the files. If I specify the trap within the
> function that errors the files are removed corectly. I beleived that
> trap in the main should know there was a exit in the function and
> remove the files. Why does trap in the main not know a function failed
> and remove the files?
You'll have to give some more information. I tried to duplicate your
problem on Solaris 8[1] and couldn't see any problem like what you are
describing. Here's what I tried:
$ uname -sr
SunOS 5.8
$ cat /tmp/foo.sh
#! /bin/sh
trap 'rm /tmp/abc' 0
foo ()
{
exit 1
}
touch /tmp/abc
file /tmp/abc
foo
$ /tmp/foo.sh
/tmp/abc: empty file
$ file /tmp/abc
/tmp/abc: cannot open: No such file or directory
$
So, it would appear that calling exit from inside a function still
causes the trap code to run.
- Logan
[1] x86 edition, but it should hardly matter since well over 90% of the
code is identical, including probably all of the shell code.
|