Reading from $stdout?

I recently posted something to log stdout AND stderr:

require ‘open3’
def doit(command)
logfile = command.shift
threads =
Open3.popen3(command.join(’ ')) { |inp,out,err|
fh = File.open(logfile,“w”)

  threads <<  Thread.new(out) { |out|
     out.each { |line| puts line; fh.print line }
  }
  threads <<  Thread.new(err) { |err|
     err.each { |line| puts line; fh.print line }
  }
  threads.each { |t| t.join }

}
$? >> 8
end

exit doit(ARGV)

for example: system.rb mylslog ls

will log everything in mylslog while printing it to the stdout.

hope this helps,

db

···

On Sat, Apr 05, 2003 at 07:21:50AM +0900, meinradrecheis wrote:

hi rubyists,

is it possible to trap everything that was written to the $stdout by
system ‘ls’ for instance?

i d like to somehow read what was written to the output stream for
graphical output.

how would you do this?

Thanks,

  • Meinrad


Apr 5 Thomas Hobbes born, 1588, philosopher
Apr 4 Tandy Corp. acquires Radio Shack, 1963 (9 stores)
Apr 4 Gandalf visits Bilbo (LOTR)
Apr 4 Martin Luther King assassinated in Memphis, Tennessee, 1968
Apr 4 NATO Established, 1949
Apr 6 Joseph Smith founds Mormon Church, 1830
Apr 4 Ching Ming Festival in Hong Kong
Apr 4 Liberation Day in Hungary
Apr 4 National Day in Senegal
Apr 5 Arbor Day in South Korea
Apr 5 Tomb Sweeping Day in Taiwan
Apr 6 Chakri Memorial Day in Thailand
Apr 6 Victory Day in Ethiopia
Apr 4 Muddy Waters (McKinley Morganfield) is born in Rolling Fork,
Mississippi, 1915
Apr 6* Daylight Savings Time begins; clocks move forward (1st Sunday of April)
Apr 4* Omer 7th day
Apr 4* Pesach (Last Day of Passover; 8th day of Pesach)
Apr 4* Yizkor
Apr 5* Omer 8th day
Apr 6* Omer 9th day
Apr 6* Parashat Shemini
Apr 6* Shabbat Mevarekhim

----- End forwarded message -----


Apr 5 Thomas Hobbes born, 1588, philosopher
Apr 4 Tandy Corp. acquires Radio Shack, 1963 (9 stores)
Apr 4 Gandalf visits Bilbo (LOTR)
Apr 4 Martin Luther King assassinated in Memphis, Tennessee, 1968
Apr 4 NATO Established, 1949
Apr 6 Joseph Smith founds Mormon Church, 1830
Apr 4 Ching Ming Festival in Hong Kong
Apr 4 Liberation Day in Hungary
Apr 4 National Day in Senegal
Apr 5 Arbor Day in South Korea
Apr 5 Tomb Sweeping Day in Taiwan
Apr 6 Chakri Memorial Day in Thailand
Apr 6 Victory Day in Ethiopia
Apr 4 Muddy Waters (McKinley Morganfield) is born in Rolling Fork,
Mississippi, 1915
Apr 6* Daylight Savings Time begins; clocks move forward (1st Sunday of April)
Apr 4* Omer 7th day
Apr 4* Pesach (Last Day of Passover; 8th day of Pesach)
Apr 4* Yizkor
Apr 5* Omer 8th day
Apr 6* Omer 9th day
Apr 6* Parashat Shemini
Apr 6* Shabbat Mevarekhim

Daniel Bretoi wrote:

I recently posted something to log stdout AND stderr:

require ‘open3’
def doit(command)
logfile = command.shift
threads =
Open3.popen3(command.join(’ ')) { |inp,out,err|
fh = File.open(logfile,“w”)

  threads <<  Thread.new(out) { |out|
     out.each { |line| puts line; fh.print line }
  }
  threads <<  Thread.new(err) { |err|
     err.each { |line| puts line; fh.print line }
  }
  threads.each { |t| t.join }

}
$? >> 8
end

exit doit(ARGV)

for example: system.rb mylslog ls

will log everything in mylslog while printing it to the stdout.

hope this helps,

db

thanks for reply, but …

i m not familiar with open3,
i would prefer a simpler solution if possible. i would be surprised if
this isn t possible in ruby :slight_smile: