Link to home
Start Free TrialLog in
Avatar of ns11
ns11

asked on

Capturing ISQL error codes in bash

Hello experts,

I am running SYBASE ISQL through my UNIX bash scripts. The ISQL while connecting to the database or running a select/update might return a error. How do i capture that error in my bash script.? How can i print something in a ISQL command on the screen. If there are any errors i want the script to exit. A example would be highly appreciated.

Thanks,
NS
Avatar of alpmoon
alpmoon
Flag of Australia image

It depends on what kind of statements you have in your script and how you use the isql outputs. You should be able to check isql outputs by egrep easily, but if you have large output files, maybe you should check the size first. Basically you should have something like (this is ksh):

LOG=........

isql -U ... -S ... -P ... -i .... > output_file

if [ $? -ne 0 ]; then
  echo "\n isql didn't work, terminating" >> $LOG
  exit
fi

egrep -i "Msg|Error" output_file

if [ $? -ne 0 ]; then
  echo "\n sql failed, terminating" >> $LOG
  cat output_file >> $LOG
  exit
fi
Avatar of ns11
ns11

ASKER

alpmoon,

I have to use bash. Below is an example of my script..

--- SCRIPT -----
#!/usr/bin/bash

EFG_ISQL="${SYBASE}/bin/isql -Sserver_name001 -Uadmin -Padmin "
count=`${EFG_ISQL} -b <<-!
        set nocount on
        go

        use db_name
        go
        delete table_name where accountId in (select accountId from table_name where runType = Z' )
        go
        !`
--- END OF SCRIPT --

What i want is to check 1) if the db connection was successfully established and 2) if the delete/select was successful. Basically some debugging statements within the block.

Thanks for your help..
I don't know equivalent commands in bash, but I think it is easier to convert ksh to bash. Basically you need to redirect your output to a file and check the output first:

${EFG_ISQL} -b <<-! > output_file
        set nocount on
        go

        use db_name
        go
        delete table_name where accountId in (select accountId from table_name where runType = Z' )
        go
        !

if [ $? -ne 0 ]; then
  echo "\n isql didn't work, terminated" >> $LOG
  exit
fi

egrep -i "Msg|Error" output_file

if [ $? -ne 0 ]; then
  echo "\n sql failed" >> $LOG
  cat output_file >> $LOG
  exit
fi

count=`cat output_file`
Avatar of ns11

ASKER

alpmoon,

I tried your script using ksh and it doesnt work. It just doesnt do anything or log anything. Can we put in some "print" statements, and raiseerrors flags..? Please advise

Really appreciate you help. Thanks,
ASKER CERTIFIED SOLUTION
Avatar of alpmoon
alpmoon
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alpmoon,

One slight improvement - you can assign the output of the entire command to a shell variable, completely removing the need to output to a file.  Much neater and removes the risk of something else changing / replacing the file you are looking at.

Simon