Capture Exit Codes from Unix
The following code that gets called from a main script
export ERR=$?
Using the above code , even the $SCRIPT exits with return
code > zero, the $? is not capturing it , because of the tee command.
I tried doing this
It still doesn't work. I am able to capture
By the way, no changes can be done
to $SCRIPT. The main script is being created so that it can run any interface
and takes care of the logging and maintains log files.
If you write a simple script like this:
ls -lai fdffds 2>/dev/null The return value from the script will be 1 (since this file does not exist). If you then rewrite the script to read: #!/bin/ksh ls -lai fdffds 2>/dev/null
The return value from the script will be 0. This is because the assignment of the value of $? to the variable errorCode was successful. If you then rewrite it script to read: ls -lia fddfdsfds 2>/dev/null
The return code from the script will be the value of $errorCode if it is not a 0. So, what is probably happening in your script is that
you are executing another command after you encounter the error. This command
is successful so the script will return 0. You need to capture the error
at the point of the occurance and explictly return it or you will continue
to get a 0 return code.
I understand your reasoning. My
question is How do I capture the error code ? At the same time I want to
be able to display the error messages and log the error messages from the
sub script.
Capturing error code within a series of piped commands
has always been a problem for me also. One solution is to avoid the pipe.
Redirect stdout to a file, which can then be sent to the screen and the
logfile:
Depending on program $SCRIPT, have you thought about checking stderr? SCRIPT=who
If $SCRIPT is a well designed program,
if there any errors, then should be in the standard error file - err.file
in this case.
Whoops!
SCRIPT=w
eval $cmd
TMTOWTDI
Try trap command. The syntax is trap ' command ' ERR
Have a Unix Problem
Unix Books :-
Return to : - Unix System Administration Hints and Tips (c) www.gotothings.com All material on this site is Copyright.
|