Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a C function from Korn shell script

Status
Not open for further replies.

shieh

MIS
Sep 17, 2002
30
US
Is there anyone help me on this?
If I have a C program (fun1) that normally called by another C as fun1(abc). I would like to know how to call fun1 from a Korn shell script. Thanks ahead.


Neil
 
You would have to turn fun1 into a program with a main(), compile it, then call that compiled program from your Korn shell script. How you turn it into a complete C program depends on what the function does.

For example, if your function just adds two numbers and looks like...
[tt]
int fun1(int a, int b)
{
return (a+b);
}
[/tt]
Then to turn it into a complete program, you would do something like...
[tt]
#include <stdio.h>
int main(int argc, char ** argv)
{
printf(&quot;%d&quot;, fun1(argv[1], argv[2]) );
exit(0);
}
[/tt]
(Note that there is NO error handling in this example!!!) Compile and link it with your fun1() function. Name the compiled program fun1. Then you can call it from a shell script like...
[tt]
VAR1=23
VAR2=45
ADDEDVAR=`fun1 ${VAR1} ${VAR2}`
[/tt]
This is just a very simple example. How you write the C program to be called from a shell script will vary greatly depending on what the function does!

Hope this helps.

 
Thank you very much for your help.

Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top