Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2007, 1:37 AM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 8th, 2007, 2:19 AM   #2
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
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)
glimmy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:09 PM.

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