From: www-data Date: 2005-12-06T09:06:36+09:00 Subject: Re: getting stdout and stderr for system calls on windows I have the same problem, and i'm likely going to end up using this syntax inside a system call: (((dmake target | tee stdout.txt) 3>&1 1>&2 2>&3 | tee stderr.txt) 3>&1 1>&2 2>&3) 2>&1 | tee output.txt where output.txt will contain stderr + stdout stdout.txt will contain stdout stderr.txt will contain stderr There is a description of how this works here: http://www.cpqlinux.com/redirect.html Scroll down to "Capturing stderr with tee Swapping stderr and stdout" Ugly as hell but it does the trick... hopefully someone knows a cleaner way to do this within ruby? -- ara.t.howard wrote: > On Thu, 24 Nov 2005, Damphyr wrote: > >> system 'echo blabla' >> stdout. >> >> Cheers, >> V.- > > i was working on this at one point: > > harp:~ > cat a.rb > class Redirector > require "tempfile" > attr_accessor "ruby" > def initialize > @ruby = "ruby" > @script = tempfile > @script.write <<-ruby > stdout, stderr = ARGV.shift, ARGV.shift > File::unlink out rescue nil > File::unlink err rescue nil > STDOUT.reopen(open(stdout,"w")) > STDERR.reopen(open(stderr,"w")) > system(ARGV.join(' ')) > ruby > @script.close > end > def run command, redirects = {} > stdout = redirects.values_at("stdout", :stdout, "o", :o, > 1).compact.first > tout = nil > unless stdout > tout = tempfile > stdout = tout.path > end > stderr = redirects.values_at("stderr", :stderr, "e", :e, > 2).compact.first > terr = nil > unless stderr > terr = tempfile > stderr = terr.path > end > system "#{ @ruby } #{ @script.path } #{ stdout } #{ stderr } #{ > command }" > ret = IO::read(stdout), IO::read(stderr), $?.exitstatus > tout.close! if tout > terr.close! if terr > ret > end > def tempfile > Tempfile::new(Process::pid.to_s << rand.to_s) > end > end > > redirector = Redirector::new > > stdout, stderr, exitstatus = redirector.run "echo 42" > p [stdout, stderr, exitstatus] > > redirector.run "echo 42", 1 => "out", 2 => "err" > p [IO::read("out"), IO::read("err")] > > > harp:~ > ruby a.rb > ["42\n", "", 0] > ["42\n", ""] > > regards. > > -a -- Posted via http://www.ruby-forum.com/.