How to execute files or commands in ruby?

F

F3leR F3leR

[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!
 
A

Angel Alonso

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

2008/9/10 Vazquez said:
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***



-----Original Message-----
From: F3leR F3leR [mailto:[email protected]]
Sent: Wednesday, September 10, 2008 1:51 PM
To: ruby-talk ML
Subject: 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!
"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 ;)
 
R

Ruby Student

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

2008/9/10 Vazquez said:
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***



-----Original Message-----
From: F3leR F3leR [mailto:[email protected]]
Sent: Wednesday, September 10, 2008 1:51 PM
To: ruby-talk ML
Subject: 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!
"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 ;)


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...
 
R

Ruby Student

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

2008/9/10 Vazquez said:
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***



-----Original Message-----
From: F3leR F3leR [mailto:[email protected]]
Sent: Wednesday, September 10, 2008 1:51 PM
To: ruby-talk ML
Subject: 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!
"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 ;)


You can also use:

returned_data = `df`` # This will execute the
 
K

Kyle Schmitt

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
 
J

James Dinkel

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
 
D

Dick Davies

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' (`).
 
S

Shin guey Wong

Dick said:
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.
 

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,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top