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
464 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

776 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