Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Capture exit codes from Unix

Original Message
Name: braveking
Date: May 14, 2004 at 18:44:41 Pacific
Subject: Capture exit codes from Unix
OS: solaris, AIX
CPU/Ram: 512
Comment:
Hi I have the following code that gets called from a main script

. $SCRIPT $DIVN | tee -a $LOGFILE

export ERR=$?
if (( $ERR != 0 ))
then
export EXVAL=$ERR
echo "\n$(date +"%D %T") Error: Process script $SCRIPT failed." | tee -a $LOGFILE
cat $LOGFILE > $HLDLOGFILE
exit $EXVAL
fi


Using the above code , even the $SCRIPT exits with return code > zero, the $? is not capturing it , because of the tee command.
I want to be able to write the messages from
$SCRIPT both to screen and logfile and at the same time manipulate the exit code.


I tried doing this
$SCRIPT $DIVN ; export ERR=$? | tee -a $LOGFILE
if (( $ERR != 0 )) ...etc

It still doesn't work. i am able to capture
ERR=1 from export, but when it comes to $ERR, it has a value of zero , maybe because my main script initialises it to zero at the beginning of main script.

Can some body help me to debug this?


Report Offensive Message For Removal


Response Number 1
Name: braveking
Date: May 16, 2004 at 11:42:45 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 2
Name: thepubba
Date: May 17, 2004 at 08:09:37 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
if you write a simple script like this:

#!/bin/ksh

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
errorCode=$?

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
errorCode=$?
if [[ $errorCode -ne 0 ]]
then
return $errorCode
fi

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.

Jerry



Report Offensive Follow Up For Removal

Response Number 3
Name: braveking
Date: May 17, 2004 at 09:32:29 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
Hi Jerry..Thx for responding. 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.

Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: May 17, 2004 at 11:56:49 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
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:

tempout=/tmp/myscript$$
$SCRIPT $DIVN > $tempout
err=$?
cat $tempout | tee -a logfile
rm $tempout
if [ $err -ne 0 ] ...


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: May 17, 2004 at 13:50:41 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
Hi:

Jim and Jerry have some good ideas - especially about capturing the exit code of piped commands. Depending on program $SCRIPT, have you thought about checking stderr?

SCRIPT=who
cmd="$SCRIPT 2> err.file |tee -a log.file"

If $SCRIPT is a well designed program, if there any errors, then should be in the standard error file - err.file in this case.

Regards,

Nails


Report Offensive Follow Up For Removal


Response Number 6
Name: nails
Date: May 17, 2004 at 13:52:16 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
Whoops!

Forgot the eval:

SCRIPT=w
cmd="$SCRIPT 2> err.file |tee -a log.file"

eval $cmd



Report Offensive Follow Up For Removal

Response Number 7
Name: Dlonra
Date: May 17, 2004 at 14:17:07 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
TMTOWTDI
my preference:
(. $SCRIPT $DIVN ; echo $? >RES)| tee -a $LOGFILE
export ERR=$(cat RES)


Report Offensive Follow Up For Removal

Response Number 8
Name: shahnawaz
Date: June 4, 2004 at 13:18:16 Pacific
Subject: Capture exit codes from Unix
Reply: (edit)
Hi

Try trap command. The syntax is

trap ' command ' ERR


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Capture exit codes from Unix

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC