LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 05-14-2010, 05:09 AM   #1
saiteju
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 manually...is there a way to print the last two echo statements when the user exits the script manually?
 
Old 05-14-2010, 05:17 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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, 05:22 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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...
 
  


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


All times are GMT -5. The time now is 01:06 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