LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-11-2008, 04:28 AM   #1
yhacks
LQ Newbie
 
Registered: Mar 2008
Posts: 1

Rep: Reputation: 0
Smile How to get return value from C function using shell


Hi, I have written script which prints output from the executable to standard output as well as in a file. Here "add" is an c executable which returns
some value based on inputs.

But if tee is not used "$?" returns the return value from add exe.
If tee is used it is simply retuning 0.

echo "Running program output"
myLine="./add 1 11"
`expr "$myLine"`| tee outputfile
echo "Succ/Fail : $?"

Used PIPESTATUS also but seems not working when running the script using sh.
 
Old 03-11-2008, 11:32 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe this will work:
Code:
echo "Running program output"
myLine="./add 1 11"
result=
{ eval $myLine; result=$?; } | tee outputfile
echo "Succ/Fail : $result"
ta0kira

Last edited by ta0kira; 03-11-2008 at 11:40 AM.
 
Old 03-11-2008, 11:48 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The convention is to use the error level of the program (i.e. the return value from it's main() function) as a diagnostic to tell if a program ran successfully or not. You should only return positive integers from 0 to 128 from your program, so as a calculator it's not very useful to use this value to return the result of your calculation.

Instead you should print the output of the calculation on stdout (i.e. use printf in your C program), and use the return value from the main function to indicate success or failure. e.g. return non-0 if there was an error, like the user not providing any numbers to add, or some parameter which is not a valid number.

Error messages should be printed to stderr. This was you can capture the correct output with the $(command) syntax (see below), error messages will be displayed on the terminal, and you will have the $? variable to check if there was an error.

Then you could call it like this from a bash script:
Code:
output=$(./add 1 11)
echo "the error level was $? ; the output from the program was: $output"

Last edited by matthewg42; 03-11-2008 at 11:54 AM.
 
Old 03-11-2008, 04:58 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by yhacks View Post
But if tee is not used "$?" returns the return value from add exe.
If tee is used it is simply retuning 0.
$? is set to the exit code of the last command executed in a pipeline.
so the tee masks your program

it is customary to return zero for success, non-zero for an error.
the exit code is only for exit status, the lower order byte
is an error code, the higher order is a bitmask containing signal
status and maybe other stuff like core file production.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
How can I get the return value of a function in a shellscript? [KIA]aze Programming 8 07-05-2007 08:18 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 01:05 PM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM


All times are GMT -5. The time now is 10:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration