Trap getting lost in 2nd-level function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap getting lost in 2nd-level function
# 1  
Old 06-19-2009
Trap getting lost in 2nd-level function

This is probably a failure in my understanding of trap or of function invocation. I'd really appreciate if someone could explain. This is Solaris 10, but I don't think it matters.

Code:
#!/usr/bin/ksh
trap 'cleanUp $@' exit
function cleanUp
{
  print "I got called"
}

function check
{
  exit 40
}

function check2
{
check
}

check2

The trap (cleanUp) never gets called. If I invoke check directly, it does, and if I add the trap to check2, it does, but not as written above. Why?
# 2  
Old 06-20-2009
Quote:
Originally Posted by plavacek
This is probably a failure in my understanding of trap or of function invocation. I'd really appreciate if someone could explain. This is Solaris 10, but I don't think it matters.

Code:
#!/usr/bin/ksh
trap 'cleanUp $@' exit
function cleanUp
{
  print "I got called"
}

function check
{
  exit 40
}

function check2
{
check
}

check2

The trap (cleanUp) never gets called. If I invoke check directly, it does, and if I add the trap to check2, it does, but not as written above. Why?


Code:
you have to use this:-
trap 'commands ....commands'  EXIT
# EXIT is the name of the signal generated upon exit from a script


another way to use trap see example below:-

trap 'rm -f ${TMPFILE} ; exit' 0 1 2 3 15
# ==>                       Signals: HUP INT (Ctl-C) QUIT TERM
# ==> Delete tempfile in case of abnormal exit from script.

# 3  
Old 06-20-2009
I just tried this on Solaris and the trap isn't called with ksh from check2.

But if I use bash or sh it is taken!

It works fine in Linux with ksh.

I'm using Solaris x86. Ksh couldn't be different on that to other platforms... could it. Which are you using ahmad?
# 4  
Old 06-20-2009
Quote:
Originally Posted by scottn
I just tried this on Solaris and the trap isn't called with ksh from check2.

But if I use bash or sh it is taken!

It works fine in Linux with ksh.

I'm using Solaris x86. Ksh couldn't be different on that to other platforms... could it. Which are you using ahmad?
Code:
I am using solaris10, on Sun Blade-100 server and a bash shell ...



---------- Post updated at 07:03 AM ---------- Previous update was at 07:00 AM ----------

Code:
may be because you are using 
exit 40

use :

trap 'commands....'  40

BR



---------- Post updated at 07:17 AM ---------- Previous update was at 07:03 AM ----------

Quote:
Originally Posted by plavacek
This is probably a failure in my understanding of trap or of function invocation. I'd really appreciate if someone could explain. This is Solaris 10 , but I don't think it matters.

Code:
#!/usr/bin/ksh
trap 'cleanUp $@' exit
function cleanUp
{
  print "I got called"
}

function check
{
  exit 40
}

function check2
{
check
}

check2

The trap (cleanUp) never gets called. If I invoke check directly, it does, and if I add the trap to check2, it does, but not as written above. Why?
Code:
Did you try the below syntax

trap "{ cleanUp "$@" }" exit
where you need to quote the variables... kindly try it..
BR


Last edited by ahmad.diab; 06-20-2009 at 11:41 AM..
# 5  
Old 06-20-2009
40 is the exit code, not the signal.

By changing "exit 40" to "return 40" it works.

Code:
#!/usr/bin/ksh
trap 'cleanUp $@' exit
function cleanUp
{
  print "I got called"
}

function check
{
  return 40
}

function check2
{
check
}

check2

Enough of this, me thinks!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. UNIX for Advanced & Expert Users

Help - Infinite Loop: Error in trap function

Hi, I was working on implementing error handling in my bash scripts, and decided to use trap to send myself an email incase of any errors. But it seems that somethings has gone wrong, and I am continuously getting same emails for an old error repeatedly (even though I have stopped/killed all... (1 Reply)
Discussion started by: cool.aquarian
1 Replies

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

5. Solaris

Difference between run level & init level

what are the major Difference Between run level & init level (2 Replies)
Discussion started by: rajaramrnb
2 Replies

6. Shell Programming and Scripting

Record the Signal Type or Number in Bash Trap function

In my Bash script I have an exit/cleanup function in a trap statement like: trap exitCleanup 1 2 3 6 15 25 Is there anyway to capture which signal # has occurred to record in a log file. Please note I am trying to avoid something like: trap 'mySignal=1; exitCleanup' 1 trap... (1 Reply)
Discussion started by: ckmehta
1 Replies

7. UNIX for Advanced & Expert Users

Unable to execute a function using trap

I have a script A which calls script B. I wrote a function in script A to be executed when Kill command is issued for script A and I invoke that function using the trap command.The function identifies all child process running under script A (in this case script B) and kills the child process and... (3 Replies)
Discussion started by: smohandass
3 Replies

8. 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
Login or Register to Ask a Question