[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!
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.
Help in communication between shell script and binary files
Hi Guys,
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
Have a look at ipcs along with ipcmk and ipcrm, which will allow you to manipulate IPC objects from the script.
However, if you want to actually communicate with the shell script, why wouldn't you use a named pipe? Almost all data in the script is going to be a stream isn't it?
Last edited by neonsignal; 02-19-2011 at 04:36 PM.
see the below code iam able to achieve write operation from script(write) to process(read) using named pipes..!
but reading in script file By writing in process(file.c) is not happening i wrote this code to the best of my knowledge please help in this as early as possible
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);
printf("read bytes:%s\n",buf);
bzero(buf,128);
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);
printf("exiting.....\n");
getchar();
close(fd);
return 0;
}
script.sh
pipe="/root/prac/mypipe"
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 $line
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 02: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 04:42 AM.
Reason: clarify
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.