database instance Error capture

I wrote a script to capture some rows from the DB.
However I want to capture the errors if the DB instance is down which usually produces errors like below. What should be my approach to kill script if the DB instance is down:

DATABASE ERRORS:

Msg 937, Level 14, State 1:
Server 'UAT_SRVR10':
Database 'test2' is unavailable. It is undergoing LOAD DATABASE.

Msg 4001, Level 11, State 1:
Server 'UAT_SRVR10':
Cannot open default database 'test2'.

Msg 208, Level 16, State 1:
Server 'UAT_SRVR10', Line 1:
TST_TABLE not found. Specify owner.objectname or use sp_help to check whether
the object exists (sp_help may produce lots of output).

MY SCRIPT:

isql -U${dbuid} -P${dbpwd} -S${dbsrv} -w5000 << EOF >> outfile
select * from the testtable where testargs='zombie'
go
exit

EOF
if [[ $? != 0 ]]
then
echo "`date` Error has occured.  Database is not available !! >>logfile
echo "`date` ABORTING  ===================================" >>logfile
echo " kill my script if the above error generated !!!"
 exit  11
fi

echo " DB connection met and continue to do the rest of the script"

System: SunOS 5.8 Generic_117350-53 sun4u sparc SUNW,Netra-T12
Database: Sybase

Well, first, put the password on the first line of input stream.

You can grep -E various patterns to find errors in the error log.

I like to do something like this to separate the data from everything else (and you can add other processing in sed):

echo "$DB_PASSWD
select 'dAtA' as X, * from tablex . . . .
go' | isql -s'|' . . . 2>&1 | sed '
  s/^dAtA|//
  t d
  /^Password: $/d
  /^$/d
  /^|x   |/d
  /^|-[-|]*-|$/d
  b
  :d
  w '$data_file'
  d
 ' >>$log_file

thanks for your response :slight_smile: sed always gives me the buzz lol...
Okay just a bit confused where echo "$DB_PASSWD followed by the select..
Trying to understand since i'm just selecting for now..but i will be running delete along with other selects in this script..not sure how to modify my script along with sybase error capture and what you are trying to explain. if you don't mind elaborating a bit please :slight_smile: