Correctly evaluating the exit status


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Correctly evaluating the exit status
# 1  
Old 10-22-2015
Correctly evaluating the exit status

Hi,

I am running a bteq command in UNIX and I want bteq output to be displayed on stdout and also be logged to a file using unix 'tee' command. Also I intend to check the bteq return code and take necessary action. Here is the code snippet -

*****************************************************
Code:
bteq << EOF | tee bteq_log_file
.logon td1/user1,passwd;
select count(*) from table;
.IF errorcode <> 0 .GOTO ERROR;

.LABEL ERROR
.QUIT 99
.LOGOFF
.QUIT
EOF

RETURN_CODE=$?

if [ $RETURN_CODE -ne 0 ]
then
exit $RETURN_CODE
fi

*****************************************************

When bteq throws an error the "if" logic is supposed to evaluate to true and the program should exit. However, the return code check below is not working because it is in effect evaluating the return code of the 'tee' command (which is executed after bteq) and not evaluating the return code of the bteq, which executes before 'tee'. So the return code check always evaluates to 'true' even when the bteq command returns a non-zero exit status.

Is there any way to circumvent this problem ? I want to display the output of bteq on stdout and redirect to a file and also check the return code of bteq (and not that of tee).

Please advise.

Thanks
# 2  
Old 10-22-2015
You're lucky if you're using a recent bash - it provides an array with all the exit codes of a pipe. man bash:
Quote:
PIPESTATUS
An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may
contain only a single command).
# 3  
Old 10-22-2015
In all shells you can do
Code:
{
bteq << EOF
.logon td1/user1,passwd;
select count(*) from table;
.IF errorcode <> 0 .GOTO ERROR;

.LABEL ERROR
.QUIT 99
.LOGOFF
.QUIT
EOF

RETURN_CODE=$?

if [ $RETURN_CODE -ne 0 ]
then
exit $RETURN_CODE
fi
} | tee bteq_log_file

It relies on proper SIGPIPE handling. (I have seen an issue with applications that set a bad signal mask and then spawn a shell where SIGPIPE cannot be handled.)
Or try the safe&simple
Code:
logfile=bteq_log_file
bteq << EOF >$logfile
.logon td1/user1,passwd;
select count(*) from table;
.IF errorcode <> 0 .GOTO ERROR;

.LABEL ERROR
.QUIT 99
.LOGOFF
.QUIT
EOF

RETURN_CODE=$?

cat $logfile

if [ $RETURN_CODE -ne 0 ]
then
exit $RETURN_CODE
fi

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-24-2015
@MadeInGermany - Thanks for your suggestion. The 2nd solution is simple and effective :-) I will pursue the same...

However, one question - Why doesn't redirecting the output to a file not alter the return code of bteq ? Anyway, redirection is also an "operation" which would have its own return code (i think) to indicate success or failure ??

Thanks much !
# 5  
Old 10-25-2015
Good question. Redirection is not a command in itself, but is part of the command and is used to open/close files.
Code:
bteq << EOF >$logfile

is equivalent to
Code:
>$logfile bteq << EOF

or even
Code:
<<EOF >$logfile bteq

If a redirection fails, then the command as a whole fails

Last edited by Scrutinizer; 10-25-2015 at 05:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

2. Shell Programming and Scripting

exit status from the script is always 0

Hi , I have a bash script , which does the network configuration. Messages from this script are dumped on console as well as stored in a log file . This script is invoked from a C code using system call . The script returns different exit code , to indicate different error cases. The... (1 Reply)
Discussion started by: abhirai
1 Replies

3. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

4. UNIX for Dummies Questions & Answers

Processes and exit status

Hi, I can't understand why the last $? is 1? can somebody plz help me to understand it? thanks $ ksh $ ps -f UID PID PPID C STIME TTY TIME COMMAND msarabad 12361 12319 0 15:17:58 pts/1 0:00 ksh msarabad 12319 12317 0 15:15:11 pts/1 0:00 -sh msarabad 12362 12361 ... (7 Replies)
Discussion started by: messi777
7 Replies

5. UNIX for Dummies Questions & Answers

$? = Exit status variable

hi, exit status variable $?, returns some digits. 0 ---> succes. 1..126 Failure (the program itself will decide what the numbers mean) 127 Command not found 128..254 The program did not exit normally. (E.g., it crashed, or received a signal) 255 Invalid exit code well, if $?... (4 Replies)
Discussion started by: dummydba
4 Replies

6. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

7. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

8. Shell Programming and Scripting

Checking Exit Status

I hope one of you smart people out there can help me with what seems like a real simple questing but I can't quite figure out. In a script I am doing a cmp on two files. I am trying to check the exit status with an if statement but can't seem to figure out the syntax. If the exit status is 1 I... (4 Replies)
Discussion started by: PrimeRibAndADew
4 Replies

9. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies

10. UNIX for Advanced & Expert Users

ftp exit status.

Does ftp from unix have an exit status. In the sense after ftp is invoked and if the ftp fails during file transfer does it exit out with a status other than 0. What is do right now is invoke ftp and right it to a log and then grep for 'File Transferred Sucessfully'. Is this the only way to do it... (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question