# Re: system() help

**URL:** <https://www.ruby-forum.com/t/re-system-help/71578>\
**Category:** Ruby\
**Created:** [February 21, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/re-system-help/71578 "2006-02-21T21:49:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [February 21, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/re-system-help/71578/1 "2006-02-21T21:49:54Z")

</div>

> -----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

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [February 23, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/re-system-help/71578/2 "2006-02-23T16:23:19Z")

</div>

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.ruby-forum.com/).

–  
[http://www.braveworld.net/riva](http://www.braveworld.net/riva)
