How to execute files or commands in ruby?

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!

Overkill I know

BEGIN SCRIPT

class Example
def hostname
puts hostname
end

def date
puts date
end

def message
puts echo 'just put you command between the tick marks'
end
end

e = Example.new
e.hostname
e.date
e.message

END SCRIPT

2008/9/10 Vazquez, Eduardo [email protected]

puts date
e.message

“NOTICE: This email message is for the sole use of the intended
recipient(s) and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited. If you
are not the intended recipient, please contact the sender by reply email and
destroy all copies of the original message.”

That don’t work for me.
Only print in the screen the command.

But I found the solution:

exec ‘the command’

Have a nice day :wink:


Angel A.

F3leR

On Wed, Sep 10, 2008 at 2:27 PM, Angel A. [email protected]
wrote:

e.hostname
To: ruby-talk ML


Angel A.

F3leR

You can also use:

returned_data = `df`` # This will execute the

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

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

On Wed, Sep 10, 2008 at 2:27 PM, Angel A. [email protected]
wrote:

e.hostname
To: ruby-talk ML


Angel A.

F3leR

You can also use:

returned_data = `df`` # This will execute the command, df and the
output of the command will be found on variable returned_data

Regards…

Dick D. wrote:

On Wed, Sep 10, 2008 at 7:27 PM, Angel A. [email protected]
wrote:

That don’t work for me.
Only print in the screen the command.

That sounds like you’re using standard quotes (’ or ").
You need to use ‘backticks’ (`).

In windows, I always use system(“cmd”) because the backticks(`) doesn’t
work most of the time.

On Wed, Sep 10, 2008 at 7:27 PM, Angel A. [email protected]
wrote:

That don’t work for me.
Only print in the screen the command.

That sounds like you’re using standard quotes (’ or ").
You need to use ‘backticks’ (`).