LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-18-2011, 02:19 AM   #1
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Rep: Reputation: 0
Unhappy 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

thanks in advance

regards
shankar
 
Old 02-19-2011, 04:33 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 02-21-2011, 12:04 AM   #3
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
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

file.c
int main(void)
{

char buf[128]="\0";
int fd,ret;
char cmd[]="/root/prac/script.sh";
fd=open("mypipe",O_RDWR); //opening pipe
printf("open :%d\n",fd);
bzero(buf,128);

if(fork() == 0)
{
system(cmd); // executing script file
exit(1);
}

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

thx in adv

~shankar
 
Old 02-21-2011, 12:53 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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
 
Old 02-21-2011, 01:00 AM   #5
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
the read operation in shell script has been tried in this many ways

read -rp line <$pipe

read line <$pipe

read $line <$pipe

~shankar
 
Old 02-21-2011, 01:04 AM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
This is fine:
Code:
read line <$pipe
 
Old 02-21-2011, 02:31 AM   #7
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
here is the code code with with above comments addressed but still not working...!

file.c
int main(void)
{
char buf[128]="\0";
int fd,ret,fd2;
char cmd[]="/root/prac/script.sh";

perror("enter");
fd=open("mypipe",O_RDWR);
perror("open1");
fd2=open("myrdpipe",O_RDWR);
perror("open");
printf("open :%d\n",fd);
bzero(buf,128);
printf("ipd of parent:%d\n",getpid());

if(fork() == 0)
{
printf("ipd of child:%d\n",getpid());
system(cmd);
exit(1);
}
printf("wating on read operation\n");
ret=read(fd,buf,128);
printf("read bytes:%s\n",buf);
bzero(buf,128);
ret=write(fd2,"hai",strlen("hai"));
sleep(1);//for synch. purpose
printf("exiting.....\n");
close(fd);
close(fd2);
return 0;
}

script.sh


pipe="/root/prac/mypipe"
pipe_rd="/root/prac/myrdpipe"


echo "Done ...." > $pipe

sleep 1

echo "in shell read"

read line <$pipe_rd
echo $line


echo "in shell read over"
 
Old 02-21-2011, 02:39 AM   #8
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
instead of read line if use cat its working
i..e..
cat <$pipe_rd
but cat is making process to wait for EOF so this is not right one i guess

if you know any option in cat command which makes process to read only a single line/block without asking EOF please share with me...!

but i would like to know about why "read" not working for me..!

all your suggestions are greatly accepted please share your ideas

bye the way i dont know how to open a pipe(existing one) in shell script if you know that pls share that also

thankyou
~shankar
 
Old 02-21-2011, 03:35 AM   #9
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
FYI

opening fifo in O_RDONLY is making the process block...!!!(dont know the reason)

by doing below changes
*read -n 3 line <$pipe_rd (read 3 chars)
it is working fine..!

but i want generic(with out any size) read operation, if we give size greater than written data size during read again its making the process block.

Last edited by shankar.489; 02-21-2011 at 04:29 AM.
 
Old 02-21-2011, 04:35 AM   #10
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Okay, just a few things:

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
 
Old 02-21-2011, 06:44 AM   #11
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
Talking thanks

i agree with your first two points but i guess these were no where affecting my program flow..!

statement 3
is solved my issue thank you very much

regards
~shankar

 
  


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
Shell Script Java File Communication ShellScripting Linux From Scratch 2 04-20-2008 02:12 PM
shell script to check ftp communication yuva_mca Linux - General 2 12-01-2005 07:15 AM
Communication between shell script and program ZooL Programming 6 08-14-2004 05:44 AM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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