Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

system() help

0 views
Skip to first unread message

zac elston

unread,
Feb 21, 2006, 3:27:08 PM2/21/06
to
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/.


Eero Saynatkari

unread,
Feb 21, 2006, 3:32:04 PM2/21/06
to
zac elston wrote:
> 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.

You might want to look at IO.popen or Open3 from the
stdlib (Open3.popen) if you need anything more than
your platform's C system() call.

> thanks


E

matt.s...@gmail.com

unread,
Feb 21, 2006, 3:35:12 PM2/21/06
to
Look up 'open3' in the standard library:
http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html

require "open3"
stdin, stdout, stderr = Open3.popen3('man ruby')

(You don't have to use stdin if you don't want to)

Berger, Daniel

unread,
Feb 21, 2006, 3:44:07 PM2/21/06
to

Use the block form. :)

Open3.popen3('man ruby'){ |stdin, stdout, stderr|
...
}

Closes the blocks automatically for you.

Regards,

Dan


Gennady Bystritsky

unread,
Feb 21, 2006, 3:47:19 PM2/21/06
to

> -----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-...@example.com
> > [mailto:list-...@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

Damphyr

unread,
Feb 23, 2006, 10:21:15 AM2/23/06
to
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


--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - δωρεάν υπηρεσία ηλεκτρονικού ταχυδρομείου.
http://www.freemail.gr - free email service for the Greek-speaking.


0 new messages