Trapping exit and continuing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trapping exit and continuing
# 1  
Old 04-29-2010
Trapping exit and continuing

Hello

I need to source a script. But that script terminates with a trailing exit. Which exits my script. I'm using bash, and this doesn't work:

Code:
trap 'echo disabled' EXIT
source other_file
trap '' EXIT

Instead, it calls my trap, but then exits anyway. I could get disgusting and tricky (execute remainder of code in the trap hook), but I don't want to. Any elegant solutions?
# 2  
Old 04-29-2010
When exit is called, the shell will exit, regardless of any traps that have been set.

The most palatable workaround that comes to mind:
Code:
exit() { :;}
source other_file
unset -f exit


Aliasing may also be an option, but the following will most likely fail unless the shell is in interactive mode or a sh option specifiying expansion of aliases in non-interactive mode is enabled.
Code:
alias exit=:
source other_file
unalias exit

Either solution will evaluate exit's args, so if they have side effects those effects will occur (though this seems to me unlikely with an exit command).

Regards,
Alister

---------- Post updated at 03:53 PM ---------- Previous update was at 03:30 PM ----------

If the only occurrence of "exit" in the script is as a command name, then you can also do the following:
Code:
eval "$(sed 's/exit/:/g' other_file)"

If exit only occurs on a line by itself:
Code:
eval "$(sed '/exit/d' other_file)"


Last edited by alister; 04-29-2010 at 04:40 PM..
This User Gave Thanks to alister For This Post:
# 3  
Old 04-29-2010
Great thanks. I know you meant to write:

Code:
function exit() {:;}
source file
unset -f exit

Appreciate the help.
# 4  
Old 04-29-2010
You're quite welcome, for the help.

Regarding the function definition, nope, I meant exactly what I typed. posix sh function definitions do not use the "function" keyword. In shells that do support functions defined using the "function" keyword (e.g. ksh), they behave a bit differently (mostly with respect to positional parameters) than those defined without it.

The space after the opening brace is needed (confirmed in ksh and bash).

Your function definition seems to be some odd hybrid of two different syntaxes. Syntax one (posix-compliant) does not use the "function" keyword and requires parenthesis after the function's name. Syntax two (ksh-style, I presume) uses the "function" keyword and does not require any parenthesis. You are using both the "function" keyword and parenthesis.

What kind of mutant shell are you using? Smilie

On an unrelated note, my ksh93 considers "exit" an illegal function name, but bash has no problem with it.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 04-29-2010
<deleted> I misread part of your response.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File validation prior to continuing script

Hi Guys, I am trying to find a way within a bash script to check a file that exists in the same directory to ensure every line starts with 44 and is 12 digits long. If it doesn't then print some sort of text advising of the error and stop the script from going any further. If all lines... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

How to get script to wait until status is true before continuing?

I'm extremely new to shell scripting so I apologize for the crudeness of my descriptions. I am editing a script that will write files (e.g. Job0_A.com, Job1_A.com, etc.) and then run them through a program called gaussian (computational chemistry program). The script will then take the output files... (10 Replies)
Discussion started by: butson
10 Replies

3. Shell Programming and Scripting

Error Trapping

Hi, I have one shell script as below while read SegList do if test -s ${SourceFile_Path}/${Segment_List_Temp} then ls -r -1 ${FTP_Path}/${SegList}.DAT.${Datelist}.GZ|cut -d '.' -f2>>${SourceFile_Path}/${List_Temp} echo "IF above statment Fail I want to Create Emtpy File How to Trapp... (3 Replies)
Discussion started by: samadhanpatil
3 Replies

4. Shell Programming and Scripting

check/wait for files to exist before continuing

I'm attempting to write a pretty simple script. It opens a Filemaker file successfully. That Filemaker file takes around 30-90 seconds to finish. When it's done, it writes a few .xml files into the same directory where my shell script and the Filemaker script reside. In my script, how can I... (2 Replies)
Discussion started by: alternapop
2 Replies

5. Shell Programming and Scripting

make sure logged in as userx before continuing script

i have a bash script and I want to add to the begining of the script to make sure that the script is being ran as you are logged in as a certain user (userx) before continuing to run the script....how? (2 Replies)
Discussion started by: ajp7701
2 Replies

6. Shell Programming and Scripting

PERL: Trapping EXIT

Hey Everyone, Just starting with PERL (5.8.2) after years of KSH. Is there a way to trap the exit as you can in KSH (i.e., "trap EXIT_SCRIPT EXIT")? Thanks in advance for any help, gsatch (4 Replies)
Discussion started by: gsatch
4 Replies

7. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

8. UNIX for Dummies Questions & Answers

trapping keys

how do i trap enter command entered by a user. actually i am throwing a screen this screen has no input but this screen should be displayes unless and until the user presses the enter key. as the user presses enter key the command prompt should come. how do i achieve this (1 Reply)
Discussion started by: sunil bajaj
1 Replies
Login or Register to Ask a Question