Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   Start another program from Ruby and view ouput? (http://www.programmingforums.org/showthread.php?t=12959)

Jessehk Apr 8th, 2007 12:37 AM

Start another program from Ruby and view ouput?
 
I have a stupid question.
I know in Ruby you can get the output of a command by encasing itin backquotes.

For example:
:

$ irb
irb(main):001:0> output = `uname -r`
=> "2.6.20-ARCH\n"


Now, my question. Say I have a program like apt-get (pacman in this case).
What I want to do is start the program from inside a Ruby script and see the output as it's running.

I think this is possible, because I've seen it done in other scripts (though not necessarily Ruby). Any ideas? :)

glimmy Apr 8th, 2007 1:19 AM

You could try using IO :: popen:

:

$ irb
irb(main):001:0> io = IO::popen('echo 1 && sleep 10 && echo 2')
=> #<IO:0x4028c0fc>
irb(main):002:0> while io
irb(main):003:1> print io.readline
irb(main):004:1> end
1
2
EOFError: end of file reached
        from (irb):3:in `readline'
        from (irb):3
        from :0

Of course you would have to filter out the exception it gets when it reaches EOF

(It's hard to tell by just reading this but there is a delay between when it prints 1 and 2 when I did it)


All times are GMT -5. The time now is 1:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC