Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > redirect stdout for Kernel.system()?

Reply
Thread Tools

redirect stdout for Kernel.system()?

 
 
Neil Spring
Guest
Posts: n/a
 
      12-16-2003
I'd like to be able to say:

$stdout=File.open("foo.out", "w")
Kernel.system("/bin/echo", "foo")

intead of:

Kernel.system("/bin/echo foo > foo.out")

which requires two processes; forking a "/bin/sh" for
something as simple as redirection is sad.

In ruby1.6.8, this works. In ruby1.8.1 and current cvs, the
behavior is to redirect puts output to the file (yay!), but
the stdout of the child process is not affected (boo.).

I think it is valuable to have the output of system() go to
the currently defined $stdout. From before I found ruby,
perl will allow 'open(STDOUT, ">ick.out"); system("/bin/echo
perl is icky")'.

Is this a bug, intended behavior, or known ambiguity?
Is there an alternative method I haven't thought of?

(test process output follows, maybe it's just me.)

thanks,
-neil


(in the output below, the duplicated messages are probably
due to buffering during the fork)

rf:/tmp> cat ~/foo.rb
#!/usr/bin/ruby

$stdout=File.open("foo.out", "w")
puts "fooble"
Kernel.system("echo", "foo")

Thread.new {
$stdout=File.open("bar.out", "w")
puts "barble"
Kernel.system("echo", "bar")
}.join

Process.fork {
$stdout=File.open("baz.out", "w")
puts "bazzle"
Kernel.system("echo", "baz")
}
Process.wait

rf:/tmp> rm *.out

rf:/tmp> ruby1.8 ~/foo.rb
foo
bar
baz

rf:/tmp> cat baz.out
bazzle

rf:/tmp> cat bar.out
barble
barble

rf:/tmp> cat foo.out
fooble
fooble

rf:/tmp> ruby1.8 --version
ruby 1.8.1 (2003-11-11) [i386-linux]

rf:/tmp> ruby --version
ruby 1.6.8 (2003-07-09) [i386-linux]

rf:/tmp> rm *.out

rf:/tmp> ruby ~/foo.rb

rf:/tmp> cat *.out
barble
bar
bazzle
baz
fooble
foo



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-16-2003

You can use IO.popen for that:
http://whytheluckystiff.net/ruby/pic....html#IO.popen

robert

"Neil Spring" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> I'd like to be able to say:
>
> $stdout=File.open("foo.out", "w")
> Kernel.system("/bin/echo", "foo")
>
> intead of:
>
> Kernel.system("/bin/echo foo > foo.out")
>
> which requires two processes; forking a "/bin/sh" for
> something as simple as redirection is sad.
>
> In ruby1.6.8, this works. In ruby1.8.1 and current cvs, the
> behavior is to redirect puts output to the file (yay!), but
> the stdout of the child process is not affected (boo.).
>
> I think it is valuable to have the output of system() go to
> the currently defined $stdout. From before I found ruby,
> perl will allow 'open(STDOUT, ">ick.out"); system("/bin/echo
> perl is icky")'.
>
> Is this a bug, intended behavior, or known ambiguity?
> Is there an alternative method I haven't thought of?
>
> (test process output follows, maybe it's just me.)
>
> thanks,
> -neil
>
>
> (in the output below, the duplicated messages are probably
> due to buffering during the fork)
>
> rf:/tmp> cat ~/foo.rb
> #!/usr/bin/ruby
>
> $stdout=File.open("foo.out", "w")
> puts "fooble"
> Kernel.system("echo", "foo")
>
> Thread.new {
> $stdout=File.open("bar.out", "w")
> puts "barble"
> Kernel.system("echo", "bar")
> }.join
>
> Process.fork {
> $stdout=File.open("baz.out", "w")
> puts "bazzle"
> Kernel.system("echo", "baz")
> }
> Process.wait
>
> rf:/tmp> rm *.out
>
> rf:/tmp> ruby1.8 ~/foo.rb
> foo
> bar
> baz
>
> rf:/tmp> cat baz.out
> bazzle
>
> rf:/tmp> cat bar.out
> barble
> barble
>
> rf:/tmp> cat foo.out
> fooble
> fooble
>
> rf:/tmp> ruby1.8 --version
> ruby 1.8.1 (2003-11-11) [i386-linux]
>
> rf:/tmp> ruby --version
> ruby 1.6.8 (2003-07-09) [i386-linux]
>
> rf:/tmp> rm *.out
>
> rf:/tmp> ruby ~/foo.rb
>
> rf:/tmp> cat *.out
> barble
> bar
> bazzle
> baz
> fooble
> foo
>
>
>


 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-16-2003
Hi,

At Tue, 16 Dec 2003 15:53:43 +0900,
Neil Spring wrote:
> I'd like to be able to say:
>
> $stdout=File.open("foo.out", "w")
> Kernel.system("/bin/echo", "foo")
>
> intead of:
>
> Kernel.system("/bin/echo foo > foo.out")
>
> which requires two processes; forking a "/bin/sh" for
> something as simple as redirection is sad.


$stdout.reopen("foo.out")

--
Nobu Nakada


 
Reply With Quote
 
Neil Spring
Guest
Posts: n/a
 
      12-19-2003
On Wed, Dec 17, 2003 at 03:47:52AM +0900, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,


>> I'd like to be able to say:
>>
>> $stdout=File.open("foo.out", "w")
>> Kernel.system("/bin/echo", "foo")
>>


> $stdout.reopen("foo.out")


thanks! that was exactly what I needed.

-neil


 
Reply With Quote
 
Andrew Johnson
Guest
Posts: n/a
 
      12-24-2003
On Tue, 16 Dec 2003 15:53:43 +0900, Neil Spring <(E-Mail Removed)>
wrote:
> I'd like to be able to say:
>
> $stdout=File.open("foo.out", "w")
> Kernel.system("/bin/echo", "foo")


This gives the desired behavior with 1.8.1p3 and Linux:

oldout = $stdout.dup

$stdout.reopen("foo.out", "w")
puts "fooble"
Kernel.system("echo", "foo")

$stdout.reopen(oldout)
puts 'back to normal'
Kernel.system("echo", "done")

regards,
andrew
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM
redirect stdout & stderr tomcat 4.1 VisionSet Java 6 09-20-2003 01:37 AM



Advertisments