[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: system() help

Gennady Bystritsky

2/21/2006 8:47:00 PM



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


1 Answer

Vassilis Rizopoulos

2/23/2006 3:21:00 PM

0

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 One-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 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
> ^^^^^ 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-....
>>>
>>>
>>
>
>


--
http://www.braveworl...

____________________________________________________________________
http://www.f... - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.f... - free email service for the Greek-speaking.