The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling a C-function froma Perl script jisha Shell Programming and Scripting 9 05-08-2008 12:59 AM
calling one function from another shell script trichyselva UNIX for Dummies Questions & Answers 1 01-17-2008 12:28 AM
Help needed in function calling in a script jisha Shell Programming and Scripting 3 01-15-2008 07:48 AM
calling c++ function from script Lebamb High Level Programming 3 06-24-2003 09:53 AM
calling a c function from shell seshagiri UNIX for Dummies Questions & Answers 4 12-06-2000 09:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Arrow A solution for Calling a C-function froma shell script

Hi,
I have searched the forum for the query, But i didnt find an exact answer.

I have a script(1.sh) and a c program(sample.c)
sample.c contains many function definitions.( run(), find(), add() etc).
I want to call functions in sample.c from 1.sh and use the return value in 1.sh
********************
1.sh

echo "this is sample"
.....
.....
find("nomber")
....
....
*********************

sample.c

main()
{
find()
{
....
.....
}

add()
{
....
....
}



}


Thanks in advance
Js

Last edited by jisha; 01-28-2008 at 04:59 AM.. Reason: I found the answer
  #2 (permalink)  
Old 01-28-2008
infyanurag infyanurag is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 63
hi
As far as i know you cannot call a function from a shell script.
  #3 (permalink)  
Old 01-28-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Thanks,
Am came to know about the same. But is their any way to implement my need?.I mean any way to make this work out with out using perl or python or anything else, but by just using shell and c ???
Thaknks in advance
Js
  #4 (permalink)  
Old 01-28-2008
jisha jisha is offline
Registered User
  
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 144
Arrow

Hi ,

I have found a solution for this and it is working !!
Let the two files involved here be sample.c and 1.sh
Open the sample.c
(My program was as below)

# include <stdio.h>
void pline(void);
main(int argc,char *argv[])
{
int i,j;

printf("Wow Entered main\n");

i=strcmp(argv[0],"pline"); /*just to test*/
j=strcmp(argv[1],"pline"); /*just to test*/
printf("i=%d \n j=%d\n",i,j);

if(strcmp(argv[1],"pline") == 0)
{
pline();
printf("done\n");
}
}

void pline(void)
{
int i;
for(i=1;i<5;i++)
printf("Test \n");
printf("\n");
}

Now save the sample.c and execute it so that we get the .exe file
The command for this is :
gcc -o sample.exe sample.c

Now this will give us sample.exe file

Now open the 1.sh

echo " test script to call the c function"
sample.exe pline

save the script and execute it !!!!

Note that either we have to include the patch of sample.exe in the $PATH (environment variable) or give the complete path of sample.exe in the example script as below:


echo " test script to call the c function"
/home/name/script/sample.exe pline

thanks
Js
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0