# How to execute files or commands in ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235>\
**Category:** Ruby\
**Created:** [September 10, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235 "2008-09-10T19:58:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/1 "2008-09-10T19:58:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/2 "2008-09-10T20:10:33Z")

</div>

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** _

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/3 "2008-09-10T20:34:03Z")

</div>

2008/9/10 Vazquez, Eduardo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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 😉

–  
Angel A.

F3leR

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/4 "2008-09-10T21:41:34Z")

</div>

On Wed, Sep 10, 2008 at 2:27 PM, Angel A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > e.hostname  
> > To: ruby-talk ML
> 
> –  
> Angel A.
> 
> F3leR

You can also use:

returned\_data = `df`` # This will execute the

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/5 "2008-09-10T23:32:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 11, 2008, 8:44am UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/6 "2008-09-11T08:44:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 10, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/7 "2008-09-10T20:46:25Z")

</div>

On Wed, Sep 10, 2008 at 2:27 PM, Angel A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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…

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 11, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/8 "2008-09-11T16:51:34Z")

</div>

Dick D. wrote:

> On Wed, Sep 10, 2008 at 7:27 PM, Angel A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

---

<div class="post-metadata">

**Author:** ![F3leR\_F3leR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@F3leR\_F3leR](https://www.ruby-forum.com/u/F3leR_F3leR)\
**Post date:** [September 11, 2008, 10:31am UTC](https://www.ruby-forum.com/t/how-to-execute-files-or-commands-in-ruby/148235/9 "2008-09-11T10:31:41Z")

</div>

On Wed, Sep 10, 2008 at 7:27 PM, Angel A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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’ (`).
