Re: system() help

-----Original Message-----
From: Gennady B.
Sent: Tuesday, February 21, 2006 12:45
To: ruby-talk ML
Subject: Re: system() help

system() does not do anyting with stdout or stdin, and it
returns true or false depending on the command exit code. If
you want to collect the program’s stdin, the easiest way
^^^^^ stdout, of
course

Well all of this is nice if you work on Unix/Linux/OS X.
On windows the case is a bit harder:
There’s a mismatch between compilers (VC6.0 vs VC7.0) with the
one-click-installer and popen3 so that it crashes badly.
1.8.4 might fix it if it switches to VC7.0.
So no way to separate stdout from stderr.
I used the following for windows:

#Executes a command on a dos shell, redirecting stderr to stdout
(“2>&1”)
#
#You can then access the output and the return value for the
command.
#
#This is meant as a last-resort replacement for popen3 (because of
problems with VC++6.0 and the Ruby O.-Click Installer).
#
#exec_time provides the Time spent running the command.
class ExecCmd
attr_reader :output,:cmd,:exec_time
#When a block is given, the command runs before yielding
def initialize cmd
@output=“”
@exec_time=0
@cmd=cmd
@cmd_run=cmd+" 2>&1" unless cmd=~/2>&1/
if block_given?
run
yield self
end
end

   #Runs the command
   def run
     t1=Time.now
     IO.popen(@cmd_run) do |f|
       @output=f.read
       @process=Process.waitpid2(f.pid)[1]
     end
     @exec_time=Time.now-t1
     return @process.success?
   end
   #Returns false if the command hasn't been executed yet
   def run?
     return false unless @process
     return true
   end
   #Returns the exit code for the command.
   #
   #Returns nil if the command hasn't run yet.
   def exitcode
     return @process.exitstatus if @process
     return nil
   end
   #Returns true if the command was succesfull.
   #
   #Will return false if the command hasn't been executed
   def success?
     return @process.success? if @process
     return false
   end
 end

Gennady B. wrote:

you want to collect the program’s stdin, the easiest way

Sent: Tuesday, February 21, 2006 12:27


Posted via http://www.ruby-forum.com/.


http://www.braveworld.net/riva