[SOLVED] Help in communication between shell script and binary files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Help in communication between shell script and binary files
Iam executing a shell script from my code and i need to "establish communication between shell script and my program(i..e.. my script iam executing and code should communicate each other while executing)"
in one shot i want to implement IPC`s in shell script..!
bye the way iam using system() call to run the shell script.
please suggest me possible solution(s) apart from popen() i know this could be the solution but i really expecting IPC IN SHELL SCRIPT
printf("wating on read operation\n");
ret=read(fd,buf,128); // reading the data written by the script file(script.sh)
printf("read return value %d\n",ret);
ret=write(fd,"hai",strlen("hai")); // writting the data into named pipe //which is going to be read in script file.
printf("ret val of write%d\n",ret);
echo "Done ...." > $pipe
sleep 1 #for synchronization between the process`s
echo "in shell read"
read -rp line <$pipe #this is the piece of code which iam struck
echo "in shell read over"
read operation in script file is not working i dont know wat kind of issue this is please help me in this
Thanks for posting some code, that clarifies your question.
You might need to read more about pipes first. A pipe is not the same thing as a file; it is an IPC object. Like many Unix objects, it may be accessible through a file name (eg a 'named' pipe), but that does not make it a file.
Your example opens a normal file, so you will not get the synchronization advantages of a pipe. To make a named pipe, you would use mkfifo from the script or the mkfifo call from the C program.
The other issue to be aware of is that you would normally use a different object for communicating in each direction, whereas your example only has a single file (being written to by both processes).
If the communications is half-duplex (ie, the ends never talk at the same time), then you can use a single object, but you may want to close and reopen in the right mode (read or write) between uses. I'd recommend keeping it simple to start with and just use two objects.
Last edited by neonsignal; 02-21-2011 at 03:00 AM.
Reason: fix grammar
1) You don't need to open the pipe in the C program until after the fork (because the child process does not need the descriptors to be open). The script will implicitly open them when it does the echo and the read in the script.
2) Unlike files, pipes can only work if both ends have opened the pipe. This means that if your main thread (the C program) exits too early, the script might not have a chance to read the characters from the pipe before it is closed by the C program.
3) There are also buffering considerations with pipes (or files for that matter). The read is stuck waiting on the end of line (which is why reading 3 characters worked), even though the C end is unbuffered. If you are going to read lines rather than characters, you will need to add a line end '\n' on the write. (Or you could use the streamed I/O in the C program instead and use fflush).
Last edited by neonsignal; 02-21-2011 at 05:42 AM.