LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2011, 12:49 PM   #1
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Rep: Reputation: 15
Bash script - if error then [do something]


Hello,

So I have a bash script with the set -e flag and for the most part it does exactly what I need it to. If there is an error executing something within the script it will break out of the script as to not cascade error on top of error.

My issue is, I have some cleanup that is done at the end of the script and this cleanup needs to happen before running the script again (the script will error out if cleanup isn't done). If there is an error while executing I have to go and perform that cleanup manually before I can try to run it again. Is there any way to basically say - if there is an error then perform this cleanup first then break out of the script?
 
Old 09-26-2011, 01:06 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can try a trap. You can make the script catch every error signal coming from the commands inside the script and cause it to execute everything you want before exiting. Example:
Code:
#!/bin/bash
trap "echo ERR signal received" ERR
The command inside double quotes will be executed every time the shell catches an error, but you can also execute it upon EXIT, so that you can execute the same command independently from the fact it fails or terminates successfully:
Code:
#!/bin/bash
trap "do something here and now" ERR EXIT
A brief explanation here: http://tldp.org/LDP/Bash-Beginners-G...ect_12_02.html

Last edited by colucix; 09-26-2011 at 01:15 PM.
 
1 members found this post helpful.
Old 09-26-2011, 02:16 PM   #3
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
You can try a trap. You can make the script catch every error signal coming from the commands inside the script and cause it to execute everything you want before exiting. Example:
Code:
#!/bin/bash
trap "echo ERR signal received" ERR
The command inside double quotes will be executed every time the shell catches an error, but you can also execute it upon EXIT, so that you can execute the same command independently from the fact it fails or terminates successfully:
Code:
#!/bin/bash
trap "do something here and now" ERR EXIT
A brief explanation here: http://tldp.org/LDP/Bash-Beginners-G...ect_12_02.html
Looks like exactly what I need, thanks! I'll try it out.
 
Old 09-26-2011, 02:53 PM   #4
Orange Sunshine
Member
 
Registered: Jul 2004
Location: Kansas
Posts: 37

Original Poster
Rep: Reputation: 15
Works great, thanks again!
 
Old 09-26-2011, 03:03 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You're welcome!
 
  


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
Why do I keep getting the same error in my bash script? okos Linux - Software 6 01-26-2010 12:30 AM
bash script read error and awk ouptut error whited Programming 4 10-16-2007 08:05 PM
bash script error noir911 Programming 4 09-27-2006 08:52 AM
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 11:01 AM
bash script error corbis_demon Linux - Newbie 7 09-27-2004 11:41 AM


All times are GMT -5. The time now is 06:45 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration