Trap to cleanup and nested shells


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap to cleanup and nested shells
# 1  
Old 06-16-2014
Trap to cleanup and nested shells

So I came across a minor issue here, and was hoping anyone could help me.

I'm on Unix SunOS, 5.10 as well as Linux 2.6.32
So everything I do has to work on both Smilie (fun ..)

Anyway, I have a few scripts, which handle some temporary files, I dropped in some basic trap commands in order to clean things up when it exits:

Code:
TMPF=`mktemp -p ${TEMP_DIR}`
trap "exec 5>&-;rm -f ${TMPF};" EXIT

This (seems to) work fine, however, I run into a small issue when I call one script from the other

So I have the above code in 2 scripts, and in the first I call the 2nd. They each have their own temp file, both of which I want cleaned up when that particular script exits.

ie

Code:
> cat script_1.ksh
#!/usr/bin/ksh
TMPF=`mktemp -p ${TEMP_DIR}`
trap "exec 5>&-;rm -f ${TMPF};" EXIT
 
# do something
 
./script_2.ksh
 
# do something else
exit 0

and
Code:
> cat script_2.ksh
#!/user/bin/ksh
TMPF=`mktemp -p ${TEMP_DIR}`
trap "exec 5>&-;rm -f ${TMPF};" EXIT
 
# do something ...
exit 0

However, I'm noticing the the file from script_1.ksh sticks around, and isn't being cleaned up. I'm assuming it's because of the similar declaration in script_2 is "clobbering" it ... however, I'm not 100% sure how to work around this?
I'm tempted to change the line:
Code:
 
trap "exec 5>&-;rm -f ${TMPF};" EXIT

to:
Code:
 
trap "exec 6>&-;rm -f ${TMPF};" EXIT

However, even if that works [edit] it doesn't - tried it - found confirmation that it's the subsequent trap clobbering the existing trap[/edit], I'm not sure that's the "correct" solution.

Any suggestions?

Last edited by Ditto; 06-16-2014 at 05:18 PM..
# 2  
Old 06-16-2014
Redeclare the trap in #1 right after #2 returns.

From POSIX on assigning traps:
Code:
If the assignment precedes a utility name in a command, the export attribute shall not persist in the 

current execution environment after the utility completes, with the exception that preceding one of the special 

built-in utilities causes the export attribute to persist after the built-in has completed.

(red emphasis mine)

A "utility" can be executing a subprocess or "shelling out'.
# 3  
Old 06-16-2014
Jim, doesn't your quote rather mean a prefixed assignment like
Code:
PATH=/bin:/usr/bin uname

?
--
For me this looks like bug. Running annother program, even another shell, should not obscure the current signals.
# 4  
Old 06-16-2014
Which version of ksh are you using?
# 5  
Old 06-17-2014
Quote:
Originally Posted by fpmurphy
Which version of ksh are you using?
I listed my O/S version above - how do I check the specific version of ksh?
# 6  
Old 06-17-2014
In Korn shell you might want to use "trap 0" to do end processing (such as removing temporary files). It is a virtual signal and the trap code is executed every time the script exits, regardless of how the exit is done.

I hope this helps.

bakunin
# 7  
Old 06-17-2014
Quote:
Originally Posted by bakunin
In Korn shell you might want to use "trap 0" to do end processing (such as removing temporary files). It is a virtual signal and the trap code is executed every time the script exits, regardless of how the exit is done.

I hope this helps.

bakunin
Sorry, am I missing something?
I thought:

Code:
trap "<stuff>" 0

and
Code:
trap "<stuff>" EXIT

where essentially identical?
In anycase, wouldn't they both get nuked when I called the next shell anyway? (according to the docs?) [edit]correction: meant to imply when the next TRAP command is issued - within the next shell - sorry Smilie [/edit]

Sorry, I'm not sure what you're trying to say here that I haven't already tried?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

2. Red Hat

How to Cleanup Multipathing

I have a server running redhat 5.5 and it has one SAN device presented to it as LUN9. How can I clean up the remaining entries. I cannot afford to interupt the service. Please assist. # multipath -l mpath0 (36000097000019260298953666633436) dm-11 EMC,SYMMETRIX \_ round-robin 0 \_ 2:0:0:9 ... (2 Replies)
Discussion started by: Tirmazi
2 Replies

3. Shell Programming and Scripting

Cleanup between parenthesis

Hi, I am trying to clean up data between parenthesis () in a file. See example below.... Input File : (New York) Chicago (London) New York (Chicago) London New York Chicago (London) (New York) (Chicago) (London) New York (Chicago) ... (3 Replies)
Discussion started by: msalam65
3 Replies

4. Solaris

/home cleanup

Hi All, I have this script for linux on cleaning up orphaned folder. But I need to use this on solaris 8/9/10 for user in $(ls | grep -v lost+found) ; do id $user >/dev/null 2>&1 if ] then ls -ld $user grep $user /etc/passwd fi done Can someone please convert this script? ... (1 Reply)
Discussion started by: itik
1 Replies

5. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

6. Shell Programming and Scripting

Cleanup script

Hi! I would like to write a script which remove some files, all beginning with the same prefix : prefix.1 doc/prefix.2 ../prefix.3 etc. So, I would create a file and chmod it executable. But I dont know how to pass a variable to a script. I would like to write something like ... (2 Replies)
Discussion started by: tipi
2 Replies

7. Shell Programming and Scripting

Help with cleanup

I am trying to add a unique string to a variable to prevent some name space collisions. DATAFILE=/u001/app/unica/affinium644/campaign/partitions/limited/tmp/ebf9aaah.t~# DATETIME=`date +%Y%m%d_%H%M%S` echo $DATAFILE > tmpnme.txt sed 's_/_ _g' tmpnme.txt > tmpnme2.txt DATA=$(cat tmpnme2.txt)... (2 Replies)
Discussion started by: whdr02
2 Replies

8. AIX

Login ID cleanup

Hello I have many old IDs on my AIX and would like to know the simplest way of knowing the last time an ID was used. I am familiar with the "last" command. Thanks for any info :) (1 Reply)
Discussion started by: MILLERJ62
1 Replies

9. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies

10. UNIX for Dummies Questions & Answers

sendmail cleanup

What is the correct procedures to clean up /var/spool/mqueue? Any help appreciated. This directory gets really clogged up at times. :( :( (1 Reply)
Discussion started by: thomi39
1 Replies
Login or Register to Ask a Question