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
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
ASKER
alpmoon,
I have to use bash. Below is an example of my script..
--- SCRIPT -----
#!/usr/bin/bash
EFG_ISQL="${SYBASE}/bin/is ql -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 have to use bash. Below is an example of my script..
--- SCRIPT -----
#!/usr/bin/bash
EFG_ISQL="${SYBASE}/bin/is
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`
${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`
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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