Link to home
Start Free TrialLog in
Avatar of tsteph
tstephFlag for United States of America

asked on

How do I execute and capture the return value of a C executable from a UNIX Kshell Script?

I am trying to run a C executable from within a Korn Shell Unix script.  The C executable is to create a csv file and returns 0, 1, or -1 depending on success, warning, or failure.  I am able to execute the code successfully from Kshell because the csv file is created. But I am unable to capture the return code (0,1,or -1) in a Kshell variable for testing purposes.  Below is a code snippet I am using now that successfully runs and executes the C code but does not capture the return code.  Parameters passed to the C code (named " cexecutable" for purposes of discussion) are a variable $harness and output directory "/home/user/me/outdir/":


   output=`/home/user/me/cexecutable $harness /home/user/me/outdir/ `
    echo "OUTPUT is = " $output >> $outfile
    if [ $output -eq 0 ]; then
        echo $output " - CSV success!" >> $outfile............
and so on...
$output does not seem to capture the return code...not sure if I have a syntax error or it is a C issue.

Thanks for the help in advance!
Avatar of Tintin
Tintin

The return code is contained in $?


output=`/home/user/me/cexecutable $harness /home/user/me/outdir/`
 
if [ $? -eq 0 ]
then
    echo "$output - CSV success!" >>$outfile
else
   echo "CSV not successful" >>$outfile
fi

Open in new window

Avatar of tsteph

ASKER

$? worked!  Thanks!  so I guess assigning output='cexecutable' does nothing? I should probably rewrite the snippet as such:

`/home/user/me/cexecutable $harness /home/user/me/outdir/`
if [ $? -eq 0 ]
then
    echo "$output - CSV success!" >>$outfile
else
   echo "CSV not successful" >>$outfile
fi
Avatar of tsteph

ASKER

OOPS..I mean:


`/home/user/me/cexecutable $harness /home/user/me/outdir/`
if [ $? -eq 0 ]
then
    echo "CSV success!" >>$outfile
else
   echo "CSV not successful" >>$outfile
fi
If you don't want the output from the command stored in a variable, then you can simply do
if /home/user/me/cexecutable $harness /home/user/me/outdir/
then
   echo "CSV success!" >>$outfile
else
   echo "CSV not successful" >>$outfile
fi

Open in new window

Avatar of tsteph

ASKER

no need for tick " ' " marks around the executable statement?
backticks are only needed if you want to capture the output to a variable (as you originally had)
Avatar of tsteph

ASKER

So the output is returned only in the $? variable....you cannot assign it to a user defined variable as I tried?
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tsteph

ASKER

This was exactly what I needed. Thanks very much!
Avatar of tsteph

ASKER

Tintin,
Just wanted to ask, the return code of the last command, is that either 0 or 1 always? The C executable is suppose to return 0,1,or -1, and it appears to only return 0 even when I force a case where the c code should return a 1 or -1.  It appears that if the c code runs successfully a 0 is returned regardless of the return value from the C code...does this make sense?  I am not sure if I am getting the return value from the C code or just a success or fail value from the shell that the code ran and did not get a runtime error.  Can you shed some light?  Thanks
The return/exit status of a command is determined by the command itself (whether it is a shell script or a C binary).

If your C program has a

exit(1)

Then that will mean that $? will be set to 1 from the calling script.
exit(-1) would be equivalent to exit(255)
Avatar of tsteph

ASKER

Thanks! this explains the value of 255 I got last night which didn't make sense until now...thanks again!