Unix Programming - Trap question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2007 > Trap question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Trap question
Jeff

2007-01-22, 1:16 pm

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? Thanks in advance. We are running the shell on
a sun server.

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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com