Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-11-2008, 05:28 AM   #1
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, 12:32 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

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

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

Rep: Reputation: 64
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:
output=$(./add 1 11)
echo "the error level was $? ; the output from the program was: $output"

Last edited by matthewg42; 03-11-2008 at 12:54 PM.
Old 03-11-2008, 05:58 PM   #4
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,433

Rep: Reputation: 203Reputation: 203Reputation: 203
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.


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 06:54 PM
How can I get the return value of a function in a shellscript? [KIA]aze Programming 8 07-05-2007 09:18 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 02:05 PM
can a function return a string? hubabuba Programming 13 03-06-2005 03:51 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 11:02 AM > Forums > Non-*NIX Forums > Programming

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

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