Solved

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

Posted on 2008-10-21
13
467 Views
Last Modified: 2013-12-26
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!
0
Comment
Question by:tsteph
  • 7
  • 5
13 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 22771399
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

0
 

Author Comment

by:tsteph
ID: 22771672
$? 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
0
 

Author Comment

by:tsteph
ID: 22771674
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Tintin
ID: 22771695
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

0
 

Author Comment

by:tsteph
ID: 22771713
no need for tick " ' " marks around the executable statement?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22771735
backticks are only needed if you want to capture the output to a variable (as you originally had)
0
 

Author Comment

by:tsteph
ID: 22771767
So the output is returned only in the $? variable....you cannot assign it to a user defined variable as I tried?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 22771801
If you don't use backticks or >, then the output will go to STDOUT (which in most cases is the display).

$? contains the return code of the last command.  So another way of writing the last section of code I gave you is:


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

Open in new window

0
 

Author Closing Comment

by:tsteph
ID: 31508504
This was exactly what I needed. Thanks very much!
0
 

Author Comment

by:tsteph
ID: 22772138
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22772923
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 22773802
exit(-1) would be equivalent to exit(255)
0
 

Author Comment

by:tsteph
ID: 22776347
Thanks! this explains the value of 255 I got last night which didn't make sense until now...thanks again!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question