trap scope question


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users trap scope question
# 1  
Old 09-13-2012
trap scope question

Hi all

I have a script that listens on a named pipe. That all works fine but I want to trap any kill signals and make sure I delete the named pipe on exit.

I can delete it fine exiting normally but cannot get the trap to see the variable holding the pipe name -

Code:
trap "rm -f ${PIPE};exit" EXIT

I have modified this to echo the value of PIPE into a file on exit -

Code:
trap "echo "P=$PIPE" > /tmp/mlog;rm -f ${PIPE};exit" EXIT

I see the P= but the value is blank.

How can I get the trap to see the variable?

Thanks
# 2  
Old 09-13-2012
Single-quote the trap command so that the variable is evaluated only when the trap signal occurs and not when the trap is being set.
Code:
trap 'rm -f $PIPE;exit' EXIT

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest. The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies

2. Shell Programming and Scripting

Using trap question

One of the scripts I'm working on, has a trap command at the very end. tap "echo ${myname}: Deployment failure; exit 2" ERR This command is present in a Script B, which is being called in the middle of another script A. If the echo command is being executed, can we identify which line in... (2 Replies)
Discussion started by: zubin_shandilya
2 Replies

3. 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

4. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 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

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

7. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

8. Programming

scope

Each thread has a copy of auto variables within a function, but variables declared as static within a function are common to all threads. To circumvent this can static variables be placed outside the function. If so, will the scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 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. Programming

C++: scope, different files etc..

I'm having a problem getting this to work.. I got 3 files, start.C - Where i got my main() function Menu.C & Menu.h - Where I'm trying to use hash_map start.C #include <iostream> #include "Menu.h" using namespace std; int main() { /* test code here */ return 0; } Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies
Login or Register to Ask a Question