LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help in communication between shell script and binary files (https://www.linuxquestions.org/questions/linux-newbie-8/help-in-communication-between-shell-script-and-binary-files-863475/)

shankar.489 02-18-2011 02:19 AM

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

neonsignal 02-19-2011 04:33 PM

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?

shankar.489 02-21-2011 12:04 AM

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

neonsignal 02-21-2011 12:53 AM

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.

shankar.489 02-21-2011 01:00 AM

the read operation in shell script has been tried in this many ways

read -rp line <$pipe

read line <$pipe

read $line <$pipe

~shankar

neonsignal 02-21-2011 01:04 AM

This is fine:
Code:

read line <$pipe

shankar.489 02-21-2011 02:31 AM

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"

shankar.489 02-21-2011 02:39 AM

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

shankar.489 02-21-2011 03:35 AM

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.

neonsignal 02-21-2011 04:35 AM

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).

shankar.489 02-21-2011 06:44 AM

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

:cool:


All times are GMT -5. The time now is 06:46 AM.