LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 11-14-2003, 06:28 AM   #1
yarnar
LQ Newbie
 
Registered: Nov 2003
Location: Bangalore,India.
Posts: 20

Rep: Reputation: 0
Thumbs up can a C function return value to Shell Script variable


Could you please suggest me how can I do this ?

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.
 
Old 11-14-2003, 06:35 AM   #2
Y0jiMb0
Member
 
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361

Rep: Reputation: 30
The variable '$?' stores the return value of the last executed program; perhaps it's what you are looking for...
Regards
 
Old 11-14-2003, 06:36 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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]
 
Old 11-15-2003, 05:24 AM   #4
yarnar
LQ Newbie
 
Registered: Nov 2003
Location: Bangalore,India.
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
Old 11-15-2003, 06:13 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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
 
Old 11-15-2003, 06:36 AM   #6
Y0jiMb0
Member
 
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361

Rep: Reputation: 30
Why don't you do the whole thing in C?
 
Old 11-15-2003, 07:34 AM   #7
Chu
Member
 
Registered: Nov 2003
Location: Australia
Distribution: Slackware 9.1
Posts: 166

Rep: Reputation: 30
Do it in Python.
If you know C, you'll catch onto Python like a school kid loves hating school ^_^
 
Old 01-17-2010, 02:26 PM   #8
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

Rep: Reputation: 20
Quote:
Originally Posted by yarnar View Post
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)"
 
Old 01-17-2010, 02:52 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 01-17-2010, 04:01 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by yarnar View Post
...
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.
 
Old 01-17-2010, 11:18 PM   #11
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

Rep: Reputation: 20
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)"

Last edited by allanf; 01-17-2010 at 11:24 PM. Reason: tabs caused to to be taken
 
Old 01-18-2010, 02:27 AM   #12
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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;
}
 
Old 01-18-2010, 02:46 AM   #13
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
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().
 
Old 01-19-2010, 09:55 AM   #14
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

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

Last edited by allanf; 01-19-2010 at 09:57 AM. Reason: show the complete solution rather than an aid to get to the complete solution.
 
Old 01-19-2010, 10:13 AM   #15
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
What's your point? And why are you using fprintf?
 
  


Reply



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
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07: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
return key on shell script chupacabra Programming 2 10-22-2002 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration