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

Try to use the 'set -e' command.
Extract from man page :

set -e
export id=root
export db=testcsv
export db_add=localhost

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

Jean-Pierre.

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??

You can use the trap command :

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.

Thanks Man - Brilliant :slight_smile:

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