Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-14-2010, 06:09 AM   #1
LQ Newbie
Registered: Feb 2010
Posts: 6

Rep: Reputation: 0
How do i run a part of code on exit in a shell script?

How do i run a part of code on exit in a shell script?

Suppose i have four echo statements like the following:

echo "Stmt 1"
echo "Stmt 2"
....some code goes here...

echo "Stmt 3"
echo "Stmt 4"

Suppose the first 2 echo statements are executed and then the user exits the script there a way to print the last two echo statements when the user exits the script manually?
Old 05-14-2010, 06:17 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian except for netbook still on Slackware 13.37 (and I have to support Ubuntu)
Posts: 8,572
Blog Entries: 30

Rep: Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182
Depends what you mean by "when the user exits the script manually". If it's by interaction with the script then it is under script control and you can program whatever you like. If it is by Ctrl+C then you can use bash' trap command to configure what is done on receipt of the signal. There are other ways of terminating a script: shutdown, logoff (if script is running in the background), sending a signal to it (for example by the kill command) ....

If the script is becoming so sophisticated it may be most convenient to write a single function which is called when the script terminates for whatever (interceptable) reason.
Old 05-14-2010, 06:22 AM   #3
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
It depends on the method used to exit manually. If the user can terminate the script using Ctrl-C, you can trap the signal sent to the script and execute whatever you want. The same if the EXIT signal (not interruption) is sent to the script. See these two examples on the Advanced Bash User Guide to get the idea.

Edit: Beaten by catkin...


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
script exit code to system() c function nemobluesix Programming 4 07-06-2009 06:56 AM
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 04:07 AM
eclipse doesn't run : JVM terminated. Exit code=1 arsham Linux - Software 1 05-07-2007 07:19 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
How to get exit code of a progarm in Shell? neo_in_matrix Linux - Newbie 2 09-22-2005 02:46 PM

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

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