Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I want to make some kind of error handling in a unix script. If any error occurs, then the script should exited...
For example.
I want to execute sqlplus from a script. If something is wrong the script should stop.Is there a possibility to exit the script if there is any error?
Thank you,
Chris

Chris,
"something is wrong the script", do you mean insied of the sqlplus script or the shell script ?
Frank

What I generally do is define a function that prints a message and exits with a non-zero status. Since functions run in the same process that will cause your script to exit. e.g:
function exit_error
{
rc=$?
print -u2 "${program}: ${*:-Error occurred: exiting.}"
exit ${rc}
}Now you have a convenient bombout function which you can use like:
if [[ some condition ]]
then
exit_error "Invalid whatever"
fi...or...
[[ -f somefile.dat ]] || exit_error "somefile.dat not found"
...or...
rcp somefile.dat somehost:/somepath || exit_error "Could not copy file"
...or...
trap "exit_error" ERR
This "trap" command will cause "exit_error" to be executed whenever any command returns a non-zero status.
Within SQL*Plus, include the following lines (or equivalent):
WHENEVER SQLERROR ROLLBACK EXIT 5
WHENEVER OSERROR ROLLBACK EXIT 10This is similar to the shell "trap" above. Now you can either
* sqlplus -s / @commandfile || exit_error "SQL\*Plus command returned error"
* Explicitly save $? immediately after calling sqlplus, and test its value
* Use the trap command so that any failure causes the script to exit.

Oops, the WHENEVER syntax is actually:
WHENEVER SQLERROR EXIT 5 ROLLBACK
WHENEVER OSERROR EXIT 10 ROLLBACK

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |