LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   can a C function return value to Shell Script variable (http://www.linuxquestions.org/questions/programming-9/can-a-c-function-return-value-to-shell-script-variable-115904/)

yarnar 11-14-2003 06:28 AM

can a C function return value to Shell Script variable
 
Could you please suggest me how can I do this ?:confused:

I want to call a C function from a shell script.
It returns a value.
I want to catch and assign the value returned by the C function to a shall variable and echo it.

eg:
# catch C returns in shell script

shell_variable=C_function_get27by3
echo $shell_variable
# shell script ends...


The answer should be 9.

I'll be thankful to you if you can send a sample code of the above or a like.

Y0jiMb0 11-14-2003 06:35 AM

The variable '$?' stores the return value of the last executed program; perhaps it's what you are looking for...
Regards

jim mcnamara 11-14-2003 06:36 AM

Most unix commands (grep, ls, file, etc.) are written in C

If you are talking about a return status code:
Code:

myCfunction
myShellVar=$?
echo "$myShellVar"

Otherwise, do you mean that the C code prints something to the screen, like a number?

Code:

myShellVar=`myCfunction`
echo "$myShellVar"

[/code]

yarnar 11-15-2003 05:24 AM

Thank you friends esapcially mcnamara and Y0jiMb0, but I don't want the exit status.
I want the value returned by a C function caught into a shell variable.
-------I Want No Temporary Files Involved-----------
Clearly put, I want to use a C function in my script to get some thing complex...
because I know C better than Scripting.
I want the output of C function into my shallscript variable.
I you can do it please mail me the code you tried.

jim mcnamara 11-15-2003 06:13 AM

Okay. You cannot embed C code in shell language scripts. Each C module has to be a standalone executable (compiled) binary file.

What you want is something like Perl or Python. These are interpreted languages, not compiled, and if you need to, you can embed custom functions written in an interpreted language inside shell scripts.

If you know C, consider downloading and using Python. It's free.

www.python.org

Y0jiMb0 11-15-2003 06:36 AM

Why don't you do the whole thing in C?

Chu 11-15-2003 07:34 AM

Do it in Python.
If you know C, you'll catch onto Python like a school kid loves hating school ^_^

allanf 01-17-2010 02:26 PM

Quote:

Originally Posted by yarnar (Post 600677)
Thank you friends esapcially mcnamara and Y0jiMb0, but I don't want the exit status.
I want the value returned by a C function caught into a shell variable.
-------I Want No Temporary Files Involved-----------
Clearly put, I want to use a C function in my script to get some thing complex...
because I know C better than Scripting.
I want the output of C function into my shallscript variable.
I you can do it please mail me the code you tried.

1) create a main program for the function.
2) The main function calls the function and prints the result.
3) In the shell script do:
Code:

my_shell_variable="$(execution_of_my_wrapped_function any_args_as_needed)"

bigearsbilly 01-17-2010 02:52 PM

well all you can return to a shell script, using a return statement is cast to an unsigned char.
in $? that is.
i.e 0-255

what you do here is print the answer you want and capture that.
easy.

Sergei Steshenko 01-17-2010 04:01 PM

Quote:

Originally Posted by yarnar (Post 600677)
...
Clearly put, I want to use a C function in my script to get some thing complex...
...

Then, for example, learn Perl and, if you still think it's not enough, use Perl 'Inline::C" module - I am using it a lot computationally intensive stuff working in real time.

allanf 01-17-2010 11:18 PM

Code:

...

char* say_hello()
{
  return "hello world";
}

float calc_xyzzy()
{
    return 6.234
{

int main(int argc, char** argv)
{
  if (argc>1) {
      if (argv[1] == 1) {
        fprintf(stdout,"%s\n",say_hello());
      } else if ( argv[1] == 2) {
        fprintf(stdout,"%g\n",calc_xyzzy());
      }
    }
    return 0;
}


Code:

  my_var="$(wrapper 1)"
  other_var="$(wrapper 2)"


smeezekitty 01-18-2010 02:27 AM

i hope you mean:
Code:

...

char* say_hello()
{
  return "hello world";
}

float calc_xyzzy()
{
    return 6.234
{

int main(int argc, char** argv)
{
  if (argc>1) {
      if (argv[1] =='1') {
        fprintf(stdout,"%s\n",say_hello());
      } else if ( argv[1] == '2') {
        fprintf(stdout,"%g\n",calc_xyzzy());
      }
    }
    return 0;
}


carbonfiber 01-18-2010 02:46 AM

There are a number of things wrong with allanf's code.

smeezekitty: argv[1] is of type char *. I hope you mean argv[1][0].

In any case, one could very well use an array of pointers to functions + pass the index into the array as an argument of the program + use setenv().

allanf 01-19-2010 09:55 AM

Code:

bash$ cat testing.c
#include <stdio.h>

char* say_hello()
{
  return "hello world";
}

float calc_xyzzy()
{
    return 6.234;
}

int main(int argc, char** argv)
{
  if (argc>1) {
      if (argv[1][0] =='1') {
        fprintf(stdout,"%s\n",say_hello());
      } else if ( argv[1][0] == '2') {
        fprintf(stdout,"%g\n",calc_xyzzy());
      }
    }
    return 0;
}
bash$ gcc -o testing testing.c
bash$ ./testing 1
hello world
bash$ ./testing 2
6.234
bash$ var_1="$(./testing 1)"
bash$ var_2="$(./testing 2)"
bash$ echo $var_1
hello world
bash$ echo $var_2
6.234
bash$

The person knows C was asking how to connect results to a shell variable. So in 45.57893763 seconds typed in a segment of code to show printing resutls of function and how to use shell script. So now it is a complete working example.


By the way this took less than 1 minute of time.

carbonfiber 01-19-2010 10:13 AM

What's your point? And why are you using fprintf?


All times are GMT -5. The time now is 01:56 AM.