How to redirect a "system" standard output and standard error to avariable (Linux)

V

Venks

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

To clarify further I want to capture any standard output or standard error.

Thanks,

---------- Forwarded message ----------
From: Venks <[email protected]>
Date: Nov 27, 2007 3:58 PM
Subject: How to redirect a "system" standard output to a variable
To: (e-mail address removed)


Hi,

I need to redirect any standard output thrown when using a system call into
a Ruby variable. I tried to search for a solution but couldn't find anything
that worked. May be I am not using the right search terms.

Here is what I am trying to do.

begin

`rm somefile`

end

If "somefile" doesn't exist the OS throws the message "rm: cannot remove
`somefile': No such file or directory" which I want to capture into a
variable.

Thanks,
 
I

Ilan Berci

Venks said:
To clarify further I want to capture any standard output or standard
error.


irb(main):001:0> r = `rm somefile 2>&1`
=> "rm: cannot remove `somefile': No such file or directory\n"


ilan
 
P

Phrogz

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

To clarify further I want to capture any standard output or standard error.

Google "ruby popen3" and "ruby popen4"
 
M

Marc Heiler

To clarify further I want to capture any standard output or standard error.

I think the shortest and cleanest is

require 'systemu'

:)

It is a bit similar to the popen3 one i think but easier IMHO
 
W

weathercoach

I was dealing with this myself today. Here is the solution came
across which works for me. I'm combining the stderr with stdout from
the system call. You can remove the exitcode stuff if you dont need
it.

IO.popen("ls nofile 2>&1") {|f|
output = f.read
exitcode = Process.waitpid2(f.pid)[1] >> 8
puts "this is the output"
puts output
puts "this is the exit code"
puts exitcode
}

#./test.rb
this is the output
nofile: No such file or directory
this is the exit code
2


HTH. G.
 
K

Ken Bloom

Note: parts of this message were removed by the gateway to make it a
legal Usenet post.

To clarify further I want to capture any standard output or standard
error.

Thanks,

---------- Forwarded message ---------- From: Venks
<[email protected]> Date: Nov 27, 2007 3:58 PM
Subject: How to redirect a "system" standard output to a variable To:
(e-mail address removed)


Hi,

I need to redirect any standard output thrown when using a system call
into a Ruby variable. I tried to search for a solution but couldn't find
anything that worked. May be I am not using the right search terms.

Here is what I am trying to do.

begin

`rm somefile`

end

If "somefile" doesn't exist the OS throws the message "rm: cannot remove
`somefile': No such file or directory" which I want to capture into a
variable.

Thanks,

You need to use popen3, because the message is printed on standard error,
not standard out.

--Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top