Can we call a C function or C executable binary from Shell script?
ProgrammingThis 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.
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.