# System() help

**URL:** <https://rubytalk.org/t/system-help/25125>\
**Category:** ruby-talk\
**Created:** [21 February 2006 20:47 UTC](https://rubytalk.org/t/system-help/25125 "2006-02-21T20:47:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_Bystritsky1](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@Gennady\_Bystritsky1](https://rubytalk.org/u/Gennady_Bystritsky1)\
**Post date:** [21 February 2006 20:47 UTC](https://rubytalk.org/t/system-help/25125/1 "2006-02-21T20:47:19Z")

</div>

> From: Gennady Bystritsky  
> 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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^ stdout, of  
course

> **···**
>
> > -----Original Message-----  
> > would be to use backquotes:
> > 
> > @myresult = `ls`
> > 
> > If you want to collect stderr as well, depending on you  
> > system shell you can do something like:
> > 
> > @myresult = `command_writing_to_stderr 2\>&1`
> > 
> > Or you can deal with forking and exec-ing a command  
> > explicitelly, using  
> > pipe() and redirecting stdout/stderr anyway you like (you may  
> > also want to take a look at popen()).
> > 
> > Gennady.
> > 
> > \> -----Original Message-----  
> > \> From: list-bounce@example.com  
> > \> [mailto:list-bounce@example.com] On Behalf Of zac elston  
> > \> Sent: Tuesday, February 21, 2006 12:27  
> > \> To: ruby-talk ML  
> > \> Subject: system() help  
> > \>  
> > \> are there any docs describing how to get stderr vs stdout from a  
> > \> system()?  
> > \>  
> > \> @myresult = system(mycmd)  
> > \> works only if I have stdout returned but not stderr.  
> > \>  
> > \> thanks  
> > \>  
> > \> --  
> > \> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Damphyr](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@Damphyr](https://rubytalk.org/u/Damphyr)\
**Post date:** [23 February 2006 15:21 UTC](https://rubytalk.org/t/system-help/25125/2 "2006-02-23T15:21:15Z")

</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")

> **···**
>
> #  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#You can then access the output and the return value for the command.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#This is meant as a last-resort replacement for popen3 (because of problems with VC++6.0 and the Ruby One-Click Installer).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#\_exec\_time\_ provides the Time spent running the command.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class ExecCmd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :output,:cmd,:exec\_time  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#When a block is given, the command runs before yielding  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize cmd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@output=""  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@exec\_time=0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cmd=cmd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cmd\_run=cmd+" 2\>&1" unless cmd=~/2\>&1/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Runs the command  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def run  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1=Time.now  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.popen(@cmd\_run) do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@output=f.read  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@process=Process.waitpid2(f.pid)[1]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@exec\_time=Time.now-t1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @process.success?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Returns false if the command hasn't been executed yet  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def run?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false unless @process  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Returns the exit code for the command.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Returns nil if the command hasn't run yet.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def exitcode  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @process.exitstatus if @process  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Returns true if the command was succesfull.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Will return false if the command hasn't been executed  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def success?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @process.success? if @process  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> Gennady Bystritsky wrote:
> 
> > > -----Original Message-----  
> > > From: Gennady Bystritsky 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
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^ stdout, of  
> > course
> > 
> > > would be to use backquotes:
> > > 
> > > @myresult = `ls`
> > > 
> > > If you want to collect stderr as well, depending on you system shell you can do something like:
> > > 
> > > @myresult = `command_writing_to_stderr 2\>&1`
> > > 
> > > Or you can deal with forking and exec-ing a command explicitelly, using  
> > > pipe() and redirecting stdout/stderr anyway you like (you may also want to take a look at popen()).
> > > 
> > > Gennady.
> > > 
> > > > -----Original Message-----  
> > > > From: list-bounce@example.com  
> > > > [mailto:list-bounce@example.com] On Behalf Of zac elston  
> > > > Sent: Tuesday, February 21, 2006 12:27  
> > > > To: ruby-talk ML  
> > > > Subject: system() help
> > > > 
> > > > are there any docs describing how to get stderr vs stdout from a system()?
> > > > 
> > > > @myresult = system(mycmd)  
> > > > works only if I have stdout returned but not stderr.
> > > > 
> > > > thanks
> > > > 
> > > > --  
> > > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> [http://www.braveworld.net/riva](http://www.braveworld.net/riva)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> [http://www.freemail.gr](http://www.freemail.gr) - Ã¤Ã¹Ã±Ã¥ÃÃ­ ÃµÃ°Ã§Ã±Ã¥Ã³ÃÃ¡ Ã§Ã«Ã¥ÃªÃ´Ã±Ã¯Ã­Ã©ÃªÃ¯Ã½ Ã´Ã¡Ã·ÃµÃ¤Ã±Ã¯Ã¬Ã¥ÃÃ¯Ãµ.  
> [http://www.freemail.gr](http://www.freemail.gr) - free email service for the Greek-speaking.
