#!/bin/sh
echo "Please enter evaluate database username"
read eval_user
echo "Please enter evaluate database password"
read eval_pass
echo "Please enter the database name"
read db_name
LOGFILE=shell_log.txt
$ORACLE_HOME/bin/sqlplus -s /nolog <<-EOF>> ${LOGFILE}
connect $eval_user/$eval_pass@$db_name
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
DBMS_OUTPUT.put_line('Connected to db');
EOF
if [ $? != 0 ]
then
echo "The upgrade script failed. Please refer to the log results.txt for more information"
echo "Error code $?"
exit 0;
fi
I am entering garbage values trying to force this script to fail. But, annoyingly, it keeps moving ahead without any mention of any error code. What else needs to be done here?
The fact you are entering fake values, are probably only related to the login. Then: Check database connectivity using Shell script
The
WHENEVER ...
are for errors during the SQL script execution. Once you'll successfuly connect with your script (I assume this your problem right now), you should get the kind of error managed byWHENEVER ERROR
because you forgot theEXEC
at your line withDBMS_OUTPUT
.it might be possible that your whenever statements are executed after connection to the db has been established (since you have mentioned them afterwards). Try the following code :-
Aji's answer with
and then using
is not correct (or not correct in most cases). See details below.
Shell script in an UNIX OS can return codes up to 255. E.g. "ORA-12703 this character set conversion is not supported" return code should be 12703, but it doesn't fit into UNIX 8-bit return code.
Actually I just did a test and ran a bad SQL that fails with "ORA-00936: missing expression" -
sqlplus returned 168 (!).
So the actual return code 936 was wrapped at 256 and just remainder got returned. 936%256=168.
On Windows this probably could work (not tested), but not on UNIX (tested as explained above).
The only reliable mechanism is probably to spool results into a log file and then do something like
So it would grep the spool log file and then cut actual latest ORA-code.
You can only trap sql error or os error. The dbms_output will fail at sqlplus level itself so the whenever error setting does not affect it.
What Max says is correct. Try this modified script
Please note the use of sql_return_code to capture the SQLPLUS return code.
The DBMS_OUTPUT statement should fail with error - "SP2-0734: unknown command beginning...". You can find the error message in log file.
It is possible to trap the sp2 errors in SQLPLUS 11g using the error logging facility. Please have a look at http://tkyte.blogspot.co.uk/2010/04/new-thing-about-sqlplus.html for more information.