[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to execute files or commands in ruby?

F3leR F3leR

9/10/2008 5:51:00 PM

[Note: parts of this message were removed to make it a legal post.]

Hi!
This is mi first message i'm new and i'm spanish if you are spanish please
contact me.

My question is:

How I can execute files or commands in the system?

Thanks for reading and have a nice day!

2 Answers

Kyle Schmitt

9/10/2008 9:25:00 PM

0

My favorite way of executing commands is
output=%x{command name}

So if you wanted the output of sar to slice and dice your machine's stats

sar_output=%x{sar}


ls_output=%x{ls} is equivalent to ls_output=`ls`, it's just easier to
read/notice in the file etc.


There are other ways of course, but they're more complex, and %x{}
should work for %90 of what you want to do

--kyle

James Dinkel

9/11/2008 6:38:00 AM

0

I use popen3 because I usually want to capture any output to stderr as
well as stdout. Here is an example:

Open3.popen3("commandname") do |input_stream, output_stream,
error_stream|
standard_output = output_stream.read
standard_error = error_stream.read
end

The variables "standard_output" and "standard_error" will be string
variables at that point, containing the text of the error and output
streams. I haven't had a need to use stdin, but I suspect calling the
'write' method on "input_stream" would be be the equivalent of piping
information to the command. However, it may also provide a method of
scripting an interactive command.

You can also use it without a block like this:

[input_stream, output_stream, error_stream] =
Open3.popen3("commandname")

You then have an array containing the IO streams, and then you could
call the 'read' method on the streams just as above, although I've
always used the block form.

James
--
Posted via http://www.ruby-....