Computing.Net > Forums > Unix > Error handling

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Error handling

Reply to Message Icon

Name: Chris
Date: February 12, 2003 at 23:59:40 Pacific
OS: HP Unix
CPU/Ram: ???
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Frank
Date: February 13, 2003 at 00:33:06 Pacific
Reply:

Chris,

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

Frank


0

Response Number 2
Name: Chris
Date: February 13, 2003 at 04:22:14 Pacific
Reply:

In this case any error.....could be the shell script but could also be inside sqlplus.


0

Response Number 3
Name: WilliamRobertson
Date: February 13, 2003 at 05:59:27 Pacific
Reply:

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 10

This 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.


0

Response Number 4
Name: WilliamRobertson
Date: February 13, 2003 at 06:03:59 Pacific
Reply:

Oops, the WHENEVER syntax is actually:

WHENEVER SQLERROR EXIT 5 ROLLBACK
WHENEVER OSERROR EXIT 10 ROLLBACK


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Error handling

Definition of Port for Socket interface programming www.computing.net/answers/unix/definition-of-port-for-socket-interface-programming/1846.html

error message in script www.computing.net/answers/unix/error-message-in-script/7025.html

IO Error on X server www.computing.net/answers/unix/io-error-on-x-server-/4982.html