The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 12:35 PM
Help with RPC Exception ejbrever HP-UX 2 08-24-2006 02:08 PM
RPC Exception - Help ejbrever UNIX for Advanced & Expert Users 0 08-21-2006 12:56 PM
Linux g++ 2.95.3 exception handling earl High Level Programming 0 08-16-2005 01:46 PM
exception handling RichardS UNIX for Advanced & Expert Users 1 06-16-2004 05:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-24-2007
bertpereira bertpereira is offline
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
  #2 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
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.
  #3 (permalink)  
Old 05-24-2007
bertpereira bertpereira is offline
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??
  #4 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
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.
  #5 (permalink)  
Old 05-24-2007
bertpereira bertpereira is offline
Registered User
  
 

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

Thanks Man - Brilliant
  #6 (permalink)  
Old 01-14-2009
kany2k kany2k is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
Hi Aigles,

I also have similar requirement as in this chain, i have written the shell script to execute a COBOL program which loads the data into Peoplesoft tables. If for some reason if the data load fails which means if the COBOL program fails i want the script to be errored and should be proceed with the rest of the process. Can i get some sample script of this type or any help is highly appreciated.

Kannan
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0