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
462 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
Comment Utility
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
Comment Utility
$? 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
Comment Utility
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
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
Comment Utility
no need for tick " ' " marks around the executable statement?
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
backticks are only needed if you want to capture the output to a variable (as you originally had)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:tsteph
Comment Utility
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
Comment Utility
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
Comment Utility
This was exactly what I needed. Thanks very much!
0
 

Author Comment

by:tsteph
Comment Utility
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
Comment Utility
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
Comment Utility
exit(-1) would be equivalent to exit(255)
0
 

Author Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now