Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
472 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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 85

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-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.

877 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