The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
MMU exception Puntino Linux 2 05-07-2008 09:35 AM
Help with RPC Exception ejbrever HP-UX 2 08-24-2006 11:08 AM
RPC Exception - Help ejbrever UNIX for Advanced & Expert Users 0 08-21-2006 09:56 AM
Linux g++ 2.95.3 exception handling earl High Level Programming 0 08-16-2005 10:46 AM
exception handling RichardS UNIX for Advanced & Expert Users 1 06-16-2004 02:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 3
Angry Exception Handling

Hi,

I have written a script to load csv files into a mysql database, however, i would like for the shell script to exit in the event of an error (missing file, load error etc.) - currently if an error is encountered the next statement is processed - This is how i am loading the csv scripts

export id=root
export db=testcsv
export db_add=localhost

mysql -h$db_add -u$id -D$db <loadA.sql

Thanks in advance

Bert
Reply With Quote
Forum Sponsor
  #2  
Old 05-24-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Try to use the 'set -e' command.
Extract from man page :

Quote:
-e If a command has a non-zero exit status, execute the ERR
trap, if set, and exit. This mode is disabled while reading profiles.
Code:
set -e
export id=root
export db=testcsv
export db_add=localhost

mysql -h$db_add -u$id -D$db <loadA.sql
Jean-Pierre.
Reply With Quote
  #3  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 3
Thanks!!!

Awesome!!!! - Is there a way i can GOTO a certain block if such an error is trapped?? - for instance once the error is encountered GOTO cleanup-script block??
Reply With Quote
  #4  
Old 05-24-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
You can use the trap command :

Code:
cleanup_script()
{
   echo "Cleaning ..."
}
trap 'cleanup_script' ERR
set -e

export id=root
export db=testcsv
export db_add=localhost

mysql -h$db_add -u$id -D$db <loadA.sql
Jean-Pierre.
Reply With Quote
  #5  
Old 05-24-2007
Registered User
 

Join Date: May 2007
Posts: 3
Thumbs up Thanks!!!

Thanks Man - Brilliant
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0