Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to redirect a "system" standard output and standard error to avariable (Linux)

Reply
Thread Tools

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

 
 
Venks
Guest
Posts: n/a
 
      11-27-2007
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 <(E-Mail Removed)>
Date: Nov 27, 2007 3:58 PM
Subject: How to redirect a "system" standard output to a variable
To: http://www.velocityreviews.com/forums/(E-Mail 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,

 
Reply With Quote
 
 
 
 
Ilan Berci
Guest
Posts: n/a
 
      11-27-2007
Venks wrote:
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      11-27-2007
On Nov 27, 2:17 pm, Venks <(E-Mail Removed)> wrote:
> 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"
 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      11-27-2007
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
weathercoach@gmail.com
Guest
Posts: n/a
 
      11-28-2007
On Nov 27, 1:55 pm, Marc Heiler <(E-Mail Removed)> wrote:
> > To clarify further I want to capture any standard output or standard error.



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.
 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      12-06-2007
On Tue, 27 Nov 2007 16:17:30 -0500, Venks wrote:

> 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
> <(E-Mail Removed)> Date: Nov 27, 2007 3:58 PM
> Subject: How to redirect a "system" standard output to a variable To:
> (E-Mail 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

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do I access avariable named "return"? Holger Wirtz Python 2 06-21-2005 02:21 PM
RE: Possible to import a module whose name is contained in avariable? Steven Reddie Python 2 03-07-2005 11:32 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM
Re: How to redirect the standard output to a file? KC Wong Java 0 08-19-2003 08:18 AM



Advertisments