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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
11-14-2003, 06:28 AM
|
#1
|
LQ Newbie
Registered: Nov 2003
Location: Bangalore,India.
Posts: 20
Rep:
|
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.
|
|
|
11-14-2003, 06:35 AM
|
#2
|
Member
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361
Rep:
|
The variable '$?' stores the return value of the last executed program; perhaps it's what you are looking for...
Regards
|
|
|
11-14-2003, 06:36 AM
|
#3
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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]
|
|
|
11-15-2003, 05:24 AM
|
#4
|
LQ Newbie
Registered: Nov 2003
Location: Bangalore,India.
Posts: 20
Original Poster
Rep:
|
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.
|
|
|
11-15-2003, 06:13 AM
|
#5
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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
|
|
|
11-15-2003, 06:36 AM
|
#6
|
Member
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361
Rep:
|
Why don't you do the whole thing in C?
|
|
|
11-15-2003, 07:34 AM
|
#7
|
Member
Registered: Nov 2003
Location: Australia
Distribution: Slackware 9.1
Posts: 166
Rep:
|
Do it in Python.
If you know C, you'll catch onto Python like a school kid loves hating school ^_^
|
|
|
01-17-2010, 02:26 PM
|
#8
|
Member
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Rep:
|
Quote:
Originally Posted by yarnar
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)"
|
|
|
01-17-2010, 02:52 PM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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.
|
|
|
01-17-2010, 04:01 PM
|
#10
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by yarnar
...
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.
|
|
|
01-17-2010, 11:18 PM
|
#11
|
Member
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Rep:
|
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
|
|
|
01-18-2010, 02:27 AM
|
#12
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
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;
}
|
|
|
01-18-2010, 02:46 AM
|
#13
|
Member
Registered: Sep 2009
Location: Sparta
Posts: 237
Rep:
|
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().
|
|
|
01-19-2010, 09:55 AM
|
#14
|
Member
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Rep:
|
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.
|
|
|
01-19-2010, 10:13 AM
|
#15
|
Member
Registered: Sep 2009
Location: Sparta
Posts: 237
Rep:
|
What's your point? And why are you using fprintf?
|
|
|
All times are GMT -5. The time now is 04:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|