Forum: Ruby Passing a string as stdin to a popen

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3a3fe02be09e3103aea5e22d62478f38?d=identicon&s=25 Pedro Côrte-Real (Guest)
on 2006-05-31 17:31
(Received via mailing list)
Say I have some content in a ruby string and I want to pass it as
stdin input to a command. I then want to read the output.

I tried this:

IO.popen("mycommand", mode='r+') do |io|
  io.write mytext
  result = io.read
end

This resulted in a "Broken Pipe" exception. What's the correct way to do
this?

Thanks,

Pedro.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-31 20:02
(Received via mailing list)
On May 31, 2006, at 11:30 AM, Pedro Côrte-Real wrote:

> This resulted in a "Broken Pipe" exception. What's the correct way
> to do this?
>
> Thanks,
>
> Pedro.
>

I'm not sure _exactly_ what the problem is, but I imagine that
  io.write mytext
  io.close_write # let the process know you've given it all the data
  result = io.read

will fix things right up.
3a3fe02be09e3103aea5e22d62478f38?d=identicon&s=25 Pedro Côrte-Real (Guest)
on 2006-06-01 12:00
(Received via mailing list)
On 5/31/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
> I'm not sure _exactly_ what the problem is, but I imagine that
>   io.write mytext
>   io.close_write # let the process know you've given it all the data
>   result = io.read
>
> will fix things right up.

Yes, this works. The problem with this is that since I only read after
I write the whole thing the process might block since noone is reading
from it and not write anything. Is there some way I can connect the io
object I get from popen to another io object I create myself. So as to
create an actual pipe in ruby?

Pedro.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-06-01 12:35
(Received via mailing list)
2006/6/1, Pedro Côrte-Real <pedrocr@gmail.com>:
> from it and not write anything. Is there some way I can connect the io
> object I get from popen to another io object I create myself. So as to
> create an actual pipe in ruby?

I suspect the error was caused because the process had terminated
already:

>> IO.popen("ls", "r+") {|io| sleep 5; io.write "test"}
Errno::EPIPE: Broken pipe
        from (irb):1:in `write'
        from (irb):1
        from (irb):1
        from :0

Does the process you start maybe not read its stdio?  Or stop reading
after some time has passed without input?  Where exactly do you get
the exception?  Is it during read or write?

If you need to read and write interleavingly then I suggest you use a
second thread.

Regards

robert
3a3fe02be09e3103aea5e22d62478f38?d=identicon&s=25 Pedro Côrte-Real (Guest)
on 2006-06-01 13:34
(Received via mailing list)
On 6/1/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> after some time has passed without input?  Where exactly do you get
> the exception?  Is it during read or write?

Yes, that was it. I was passing some wrong options to the program and
it was exiting without reading it's input. It's solved now. What I'm
doing is running "xsltproc" to convert some XML into HTML with a XSLT.
I guess a better idea would be to do it directly in ruby but ubuntu's
libxslt-ruby1.8 seems to be broken.

Pedro.
This topic is locked and can not be replied to.