Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
466 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Autosar OS Multicore Share Resources confusion ? 2 69
windows 10 pro lost profile. 10 43
AWK: Pythagoras bp script 18 27
Shell script issue 4 35
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

766 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