Error Handling


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error Handling
# 1  
Old 02-15-2012
Error Handling

Below code works for different databases i.e. MYSQL and ORACLE

The problem is for MYSQL in Block:
Code:
if [ $? -ne 0 ]

;
Code:
$?

taking value accordingly but in case of ORACLE
Code:
$?

is always taking this value as zero (0).

That is the reason in Oracle it always going in else Block in any case.. Smilie
and in case of MYSQL it is working fine..... Smilie

Code:
add_fk()
{
echo "`date` Entering $0 " | tee -a $LOGFILE

if [ $DB_SYSTEM = "ORACLE" ]
then
sqlplus -s sna/dbmanager <<! | tee -a $LOGFILE
ALTER TABLE TABLE_NAME ADD CONSTRAINT CONSTRAINT_NAME_FKEY
      FOREIGN KEY (LAYERRATENAME)
      REFERENCES REFERENCE_TABLE(REFERENCE_TABLENAME);
exit
!
     if [ $? -ne 0 ]
     then
    echo "Foreign Key can not be established - incompatibile data" | tee -a $LOGFILE   
    echo " Please verify the data in TABLE_NAME and REFERENCE_TABLE table" | tee -a $LOGFILE
    status=2
     else
    echo "Foreign Key is Added" | tee -a $LOGFILE   
     fi

elif [ $DB_SYSTEM = "MYSQL" ]
then
    mysql --user=${OSMDB_USR} --password=${OSMDB_PWD} ${DB_NAME} <<!
    set storage_engine=INNODB;   
    ALTER TABLE TABLE_NAME ADD CONSTRAINT CONSTRAINT_NAME_FKEY
      FOREIGN KEY (LAYERRATENAME)
      REFERENCES REFERENCE_TABLE(REFERENCE_TABLENAME);
    exit
!
     if [ $? -ne 0 ]
     then
    echo "Foreign Key can not be established - incompatibile data" | tee -a $LOGFILE   
    echo " Please verify the data in TABLE_NAME and REFERENCE_TABLE table" | tee -a $LOGFILE
    status=2
     else
    echo "Foreign Key is Added" | tee -a $LOGFILE   
     fi
fi
}

Please help meSmilie

---------- Post updated at 04:41 PM ---------- Previous update was at 04:12 PM ----------

Code:
if [ $? -ne 0 ]

Or please let me know .. what does above meant..? Smilie
# 2  
Old 02-15-2012
$? is a special variable which says the previous command status.

most of the commands exit with 0 ( if it is successfully executed )

---------- Post updated at 04:47 PM ---------- Previous update was at 04:44 PM ----------

use the below in your sql

Code:
 
whenever sqlerror exit sql.sqlcode

or

Code:
 
exit sql.sqlcode;

so, your $? variable will have the exit status code.
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 02-15-2012
Thanks itkamaraj!!

I implemented the same as you suggested i.e.

Code:
whenever sqlerror exit sql.sqlcode

Sorry to say, but still
Code:
$?

is returning zero(0) value...

Kindly, Can you please explain me more about it...

Thanks again...
Ambar
# 4  
Old 02-15-2012
It might be worth trying to replace
Code:
sqlplus -s sna/dbmanager <<! | tee -a $LOGFILE

with
Code:
(sqlplus -s sna/dbmanager | tee -a $LOGFILE) <<!

What I am thinking might be causing the issue:
Your might be capturing the return value of tee instead of sqlplus... I think that moving the heredoc to the end might result in sqlplus status being in $? when you expect it to.
You might be able to use PIPESTATUS as well, if your shell supports it?


Moderator's Comments:
Mod Comment Please use code tags
for your code and data!

Last edited by vbe; 02-15-2012 at 07:13 PM..
This User Gave Thanks to browncoat For This Post:
# 5  
Old 02-16-2012
Thanks a ton browncoat!!

Yes you rightly pointed out the problem ... Thanks Again....

Actually
Code:
$?

was returnning the value for the
Code:
tee -a $LOGFILE

which will always successful and it return always zero (0) i.e.
Code:
$? = 0 always

...

Once I remove that
Code:
tee -a $LOGFILE

... the code for ORACLE also works perfectly fine now....

Kudos to you....
Ambar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error handling for file

Hi Guys, I got a csv with pipe delimted file and i want to check second column of the file has any alpha character becuase I am expecting only number in that, and if any alpha characters then it should throw an error Thanks in advance (1 Reply)
Discussion started by: Rizzu155
1 Replies

2. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

3. Shell Programming and Scripting

Expect Script Error Handling

Good Day Everyone, I was hoping to get a little insight into an expect script that I've written. Basically we have this expect script to perform an sftp upload, key authentication is not an option, and sftp is the only method supported by our vendor, thus the need for this. I want to be... (3 Replies)
Discussion started by: thaller
3 Replies

4. Shell Programming and Scripting

PERL error handling

I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file. perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4 How do I handle errors coming out... (1 Reply)
Discussion started by: OTChancy
1 Replies

5. Shell Programming and Scripting

Help needed with error handling

I am writing a wrapper to one of the .ksh script. The wrapper script should capture the error/fatal(if any) and exit out of the script. Here is the script: #!/bin/sh . main_script.ksh <arg1> <arg2> > mainscript.log 2>&1 cnt=`grep 'ERROR' -c mainscript.log` if ; then echo "Error" ... (6 Replies)
Discussion started by: stunnerz_84
6 Replies

6. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

7. Shell Programming and Scripting

Finger and error handling

I have this segment of code : cmd = "finger -m " $1 " 2>/dev/null | head -1" cmd | getline userinfo close(cmd) Sometimes finger returns no such user when given a user id. With the redirection to the default trash file i am getting rid of any screen "finger:no such user" messages. I also want... (2 Replies)
Discussion started by: beatblaster666
2 Replies

8. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

9. Shell Programming and Scripting

SFTP Error Handling

Hi , Can any one tell me is there any standard method to track errors during sftp ? using which command i can track sftp errors ? i tried using echo $? . Most of the times i am getting error number 127 ,1, 255. whether it is constant numbers ? Please help me out. Thanks in advance (2 Replies)
Discussion started by: deepusunil
2 Replies

10. Shell Programming and Scripting

Handling ftp error

I have a script which connects to remote server and ftp the files It works fine, however if there is any failure in ftp connection can it be handled??? ftp log ftp session start time is: Thu Jun 19 00:00:02 BST 2008 Not connected. Not connected. Interactive mode off. Not connected.... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies
Login or Register to Ask a Question