Register
It is currently Sun Nov 23, 2014 2:01 pm

Interprocess communication


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat May 30, 2009 1:54 pm   

Joined: Sat May 30, 2009 1:37 pm
Posts: 1
Hi all,

this is my first message here :)

I have some hard time since two days with a (maybe) simple problem: I want to communication with a subprocess (say ftp for example) to send it commands, and to read its answers.
The first way I explored was file descriptors. I didn't found a way. FIFOs sound promising but, as you will see, I'm stuck.
By the way: I don't want to use the 'expect' program. 'expect' is designed to solve this kind of problem, but my problem isn't only interaction, and I don't want to learn yet another programming language (I'm not a programmer, only a sysadmin).

The scenario:
- launch the desired program (ftp in this example)
- send it a command (help)
- read the answer
- send another command (verbose)
- read the answer
- send another command (bye)

My problem is very simple: when I send a command with 'echo' (or with 'cat', same result), bash seems to close the corresponding FIFO. Then the launched program terminates. When I read the output, same problem.

Here is an example:
Code:
#!/bin/bash

TMPDIR=$(mktemp -d)     || { echo "mktemp error"; exit -1; }
FIFO_STDIN=$TMPDIR/fifo_stdin
FIFO_STDOUT=$TMPDIR/fifo_stdout
mkfifo $FIFO_STDIN $FIFO_STDOUT         || { echo "mkfifo error"; rm -rf $TMPDIR; exit -1; }

ftp <$FIFO_STDIN >$FIFO_STDOUT 2>&1  &
echo 'help' >$FIFO_STDIN
# the ftp program is already stopped here
read TEXT <$FIFO_STDOUT
echo "[$TEXT]"

echo 'verbose' >$FIFO_STDIN
read TEXT <$FIFO_STDOUT
echo "[$TEXT]"

echo 'bye' >$FIFO_STDIN

rm -rf $TMPDIR
exit 0


Anyone have an idea to keep the FIFOs open ? And/or another way to communicate ?
I also tested with this kind of stuff:

Code:
exec 5>&1
exec 6<&2
ftp >&5 <&6 2>&1 &
etc etc

But no more result.


Top
 Profile  
 PostPosted: Mon Jun 01, 2009 7:48 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
It's hard. The tool for the job is expect, why re-invent it?
:)


Top
 Profile  
 PostPosted: Mon Jun 01, 2009 9:58 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
DarthWavy wrote:
It's hard. The tool for the job is expect, why re-invent it?
:)


Expect is very nice, but i haven't seen many linux distros that supply it as a default option in any repositories :P
You might be able to sort that out with xargs or alot of annoying cases and if-then-else statements.

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], Yahoo [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP