UNIX with DB2 error status Issue

I have a shell script main.ksh We are calling dbscript.ksh from main.ksh
I am using select statement in dbscript.ksh but there is a problem with the select statement in dbscript.ksh but still echo $? is showing as zero. I am using DB2 commands in dbscript.ksh

Main.ksh
dbscript.ksh
echo $?  #it is returning zero even though error is there in dbscript.ksh

This is probably because there is no error checking in dbscript.ksh.

Can you share that so we can consider it? Feel free to obscure and userid/passwords if you have them embedded in the script.

Thanks, in advance,
Robin

dbscript.ksh script details are mentioned as below.In this script also
$?is giving 0 and there is error in db2 select command


echo "Start Timestamp is : "$datevar1 > log.out;
db2 "export to $exportfilepath/abc.ixf of IXF
select * from table with UR" #Here I am selecting from various tables and conditions
datevar=`date +'%F-%H.%M.%S'`
echo "Current Timestamp is : "$datevar >>  log.out;
echo "The value of $?" # Here also it is returning 0
if [[ $? -ne 0 && $? -ne 2 ]]; then
        echo "Run Status is : Fail"
else
        echo "Run Status is : Success" 
fi

The $? shell variable holds the exit code of the LAST command executed, i.e. the echo in above. Save the individual exit codes into separate variables right after the respective command, like e.g.

db2 . . .
EXIT_DB2=$?
select . . .
EXIT_SEL=$?

and do an error handling based on those, or return either to the caller.

1 Like

Thanks for Input.Now dbscript.ksh is working fine but the problem is main.ksh is not working.Main.ksh should display dbscript.ksh got failed but it going to else part.Both script are mentioned as below

dbscript.ksh

db2 "export to $exportfilepath/abc.ixf of IXF select * from table with UR" #Here I am selecting from various tables and conditions.There is an error and it is working fine
if [[ $? -ne 0 ]]; then
        echo "Run Status is : Fail"
else
        echo "Run Status is : Success" 
fi

clarification:-Why it is going to else part is not clear to me?
main.ksh

dbscript.ksh
if [ $? -ne 0 ];
then 
echo "dbscript.ksh got failed` 
else 
echo "dbscript.ksh got succeed"
fi

The $? shell variable holds the exit code of the LAST command executed, i.e. the echo ... Fail in above. Save the individual exit codes into separate variables right after the respective command . . . return either to the caller.

Could you please help on How to do Save the individual exit codes into separate variables right after the respective command . . . return either to the caller.

Already done in post#4. Try

return $EXIT_DB2

EDIT: or, as proposed by rbatte1 below, exit $EXIT_DB2 .

If you detect an error in dbscript.ksh and you want to pass that back to main.ksh, the usual way to is for dbscript.ksh to exit with a return code. You can do this:-

db2 "export to $exportfilepath/abc.ixf of IXF select * from table with UR" #Here I am selecting from various tables and conditions.There is an error and it is working fine
EXIT_DB2=$?
if [[ $EXIT_DB2 -ne 0 ]]; then
        echo "Run Status is : Fail"
        exit $EXIT_DB2
else
        echo "Run Status is : Success" 
        exit $EXIT_DB2            # Will be zero, but nice to be consistent
fi

I hope that this helps,
Robin

1 Like

If the exit $EXIT_DB2 exists in either branch, you might consider using it after the if construct.

1 Like

Thanks for input.I faced one Issue here.Here I am exporting the data from tables to abc.ixf.Before export operation[I mean db2 select] is completed it is executing next command EXIT_DB2=$?.
I want EXIT_DB2=$? to be executed only after completion of db2 export command

db2 "export to $exportfilepath/abc.ixf of IXF select * from table with UR" #Here I am selecting from various tables and conditions.There is an error and it is working fine
EXIT_DB2=$? #I want to trigger this one once above export operation is completed
if [[ $EXIT_DB2 -ne 0 ]]; then
        echo "Run Status is : Fail"
        exit $EXIT_DB2
else
        echo "Run Status is : Success" 
        exit $EXIT_DB2            # Will be zero, but nice to be consistent
fi

---------- Post updated 02-07-18 at 01:24 AM ---------- Previous update was 02-06-18 at 03:51 AM ----------

Hi All,

Could anybody help on this issue?

It is hard to help on an issue if we don't understand what issue is troubling you.

You have shown us code with comments saying it is working fine.

What is the problem?

Is db2 another shell script you have written? Are you saying that it doesn't return a non-zero exit code when it fails?

Are you saying that db2 starts a database operation asynchronously and returns before the operation completes? If this is your problem, you're stuck. A parent can wait for its child to exit and gather its exit status. A grandparent can't get the exit status of any of its grandchildren unless that grandchild's parent made it available to the grandparent.

I want to trigger EXIT_DB2=$? command after completion of db2 export command but the problem here is EXIT_DB2=$? is triggering before completion of export process

db2 "export to $exportfilepath/abc.ixf of IXF select * from table with UR" 
EXIT_DB2=$? #I want to trigger this one once above export operation is completed but this command is triggering before export operation is completed
if [[ $EXIT_DB2 -ne 0 ]]; then
        echo "Run Status is : Fail"
        exit $EXIT_DB2
else
        echo "Run Status is : Success" 
        exit $EXIT_DB2            # Will be zero, but nice to be consistent
fi

I repeat: If db2 is starting a database operation asynchronously and exiting before that database operation completes, there is no way for a script that invokes db2 to determine what the exit code was (or will be) for the database operation when it completed (or when it completes later).

If you would like to show us the source code for db2 we might be able to help you fix it so that your script can get the data it wants.

2 Likes