LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-14-2011, 02:13 AM   #1
tanniru
LQ Newbie
 
Registered: Mar 2006
Posts: 24

Rep: Reputation: 0
Can we call a C function or C executable binary from Shell script?


Hello,

I am working on some feature in linux where I need to call a C function(which does opening a binary file, gets checksum value in the file and do some structure manipulation) to it.


Is it possible to call a C function or C executable binary from linux shell script?
 
Old 10-14-2011, 02:48 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I don't think it is possible to call a function from a library.
But you can write a simple C program that calls that function and then call your program from a shell script.

Last edited by millgates; 10-14-2011 at 02:49 AM.
 
Old 10-14-2011, 03:10 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by tanniru View Post
Is it possible to call a C function or C executable binary from linux shell script?
The latter is very possible; for example, whenever in your script you write 'grep', 'wc' or 'cat', it will execute a binary program.
 
Old 10-14-2011, 08:49 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just to expand on millgates' comment; any command executed by the shell executes either a shell built-in command, or an external command. The 'command' is simply the name of some executable which could be binary object code (i.e. a compiled program), or a script. The shell or any other part of the system doesn't care whether the program was written in C or any other language.
You can write simple programs in C, which invoke whatever functions you want. Presumably, you want the program to output something related to the action it performs. Data can be written to stdout with the likes of printf(), where it then becomes available to your shell/script, as input to other applications via shell pipes. Also, your program can terminate with an exit() code that can be tested in the calling shell, to convey relative success/failure status of your program. By convention, programs that return zero are indicating 'success', and non-zero return status indicates some sort of error or failure. Collectively, these behaviors contribute to the Linux concept of many small tools that can be combined to create a larger overall application that serves a particular purpose. Such is the beauty and elegance of the Linux standard IO idiom.

--- rod.
 
  


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
convert shell script to a binary executable madhi Programming 13 07-24-2012 09:36 AM
Call Shell Function from mysql santhoshvkumar Programming 2 04-18-2011 02:15 PM
[SOLVED] CGI shell scripting: is it possible to call function from other script? joost1 Programming 2 03-16-2011 02:09 AM
Unix Function call to get executable's path vharishankar Programming 13 04-20-2010 12:53 PM
problem of function call address in executable file valpa Programming 12 09-05-2008 08:36 AM

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

All times are GMT -5. The time now is 07:54 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