LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-13-2012, 12:39 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814

Quote:
Originally Posted by porphyry5 View Post
What command within a sourced script will stop execution and return to it's shell's command prompt?
You can use return for this.

Quote:
return
return [n]
Cause a shell function to exit with the return value n. If n is not supplied, the return value is the exit status of the last command executed in the function. This may also be used to terminate execution of a script being executed with the . (or source) builtin, returning either n or the exit status of the last command executed within the script as the exit status of the script. Any command associated with the RETURN trap is executed before execution resumes after the function or script. The return status is non-zero if return is used outside a function and not during the execution of a script by . or source.
 
1 members found this post helpful.
Old 07-13-2012, 12:39 PM   #17
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Glad I could be of service.

I was thinking though that it would be more robust to set it up as a trap. That way it will print the output whenever the script fails or is manually terminated as well.

Code:
saveenv(){
	declare > file.txt
	exit $1
}

trap 'saveenv $?' INT TERM EXIT
You can modify the function to include other commands too, of course.

The hard quotes around the trap command ensure that the exit code of the final command to run gets passed to the function, and therefore preserved as the script's exit code. Double-quotes would expand the value at the time the trap is set up instead, and you'd (probably) always get 0.

(Edit: Sheesh! Between the time I opened the page and the time I posted, there've been 3 posts. Kinda confuses the context of what I was replying to. Sorry. )

Last edited by David the H.; 07-13-2012 at 12:44 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make my BASH script yes/no user input query terminate with 3 invalid inputs? lupusarcanus Linux - Newbie 9 03-16-2010 03:43 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
How to terminate Bash Script if remote session disconnects zia.hassan Linux - Newbie 4 06-15-2009 04:04 AM
how can a shell script determine its own location when being sourced? DeuceNegative Linux - Software 5 02-21-2008 01:57 PM
How to run script that doesn't terminate when shell closes? bjdea1 Linux - General 2 09-20-2004 10:20 PM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration