# Using Shell linux commands in Ruby!

**URL:** <https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083>\
**Category:** Ruby\
**Created:** [October 3, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083 "2008-10-03T12:41:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/1 "2008-10-03T12:41:50Z")

</div>

I have the following text that allows for having shell linux commands  
executed and their results manipulated inside ruby code. If anyone can  
tell me a better way of doing this, or he/she can mention the pros and  
cons of this way of using shell linux commands inside ruby. I will be  
very thankful to him/her. The following text is also available on the  
link

> **[Using Shell Commands in Ruby](http://linux-certification.blogspot.com/2008/10/using-shell-commands-in-ruby.html)**
>
> The following is a step by step guide how can you write shell linux commands inside ruby code and get their results in customized output cre...

The following is a step by step guide how can you write shell linux  
commands inside ruby code and get their results in customized output  
created by ruby.

First of all execute the following command on shell prompt

shell\> vi userinfo.rb

This will open up vi editor with a file named userinfo opened in it

1. Press i on your keyboard, this will switch the file userinfo into  
insert mode
2. Write the following code in it

#Code Starts  
def user  
user = `users`  
end

def groups  
groups = `groups`  
end

puts “You are #{user} and you belongs to the groups \n #{groups}”  
#Code Ends

1. Press ESC key on your keyboard to exit insert mode
2. Now write :wq and press enter key on your keyboard

This will throw you out to the shell prompt. On the shell prompt write  
the following command

shell\> ruby userinfo.rb

You will the customized output like this

You are root and you belong to the groups  
[A list of group will be displayed on this line]

Thats it to executing shell linux commands inside your linux code

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/2 "2008-10-03T12:50:15Z")

</div>

pros and cons ?

pro would be that you can write your own version of a binary

con would be that a shell is spawned to execute the command

Another way to execute a command would be through the system method. But  
then you couldn’t get the command output that easily , since system  
returns an exit code .

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/3 "2008-10-03T13:13:17Z")

</div>

Ch Asif wrote:

> 

Please also look at: %x{command}

such as :

dirlist=%x{ls -m}

etc.

Do note that you may have to chomp() the result in many cases - there  
will be the extra char at end (newline).

btw, system() won’t return the output.

There are other variations such as “exec” which will overlay your  
current process …

my favorite is %x{…}

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/4 "2008-10-03T19:06:15Z")

</div>

Lex W. wrote:

> Another way to execute a command would be through the system method.

See also IO#popen:

> > IO.popen(‘cat’, ‘w+’) {|f| f \<\< ‘hello’; f.close\_write; puts f.read }  
> > hello  
> > =\> nil
> 
> > puts $?.exitstatus  
> > 0  
> > =\> nil

> But then you couldn’t get the command output that easily,  
> since system returns an exit code .

Wrong. Obtaining the output of a command has nothing to do with the  
exit code:

> > `false`  
> > =\> “”
> 
> > puts $?.exitstatus  
> > 1  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/5 "2008-10-03T19:18:36Z")

</div>

> Wrong. Obtaining the output of a command has nothing to do with the  
> exit code:
> 
> > > `false`  
> > > =\> “”
> > 
> > > puts $?.exitstatus  
> > > 1  
> > > =\> nil

Interesting example of NOT using system .

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/6 "2008-10-03T20:12:00Z")

</div>

sorry , I thought ruby went along the same way as the other languages  
about system.

---

<div class="post-metadata">

**Author:** ![muasif80](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@muasif80](https://www.ruby-forum.com/u/muasif80)\
**Post date:** [October 3, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/using-shell-linux-commands-in-ruby/150083/7 "2008-10-03T19:50:15Z")

</div>

On Fri, Oct 3, 2008 at 6:48 AM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> pros and cons ?
> 
> pro would be that you can write your own version of a binary
> 
> con would be that a shell is spawned to execute the command
> 
> Another way to execute a command would be through the system method. But  
> then you couldn’t get the command output that easily , since system  
> returns an exit code .

Actually Kernel#system returns true or false

macbook:~ michaelguterl$ ri Kernel#system  
---------------------------------------------------------- Kernel#system  
system(cmd [, arg, …]) =\> true or false
