Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Friday, October 3, 2008

Using Shell Commands in Ruby

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

3. Press ESC key on your keyboard to exit insert mode
4. 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 get 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

Go back to the table of contents for 'Ruby on Linux'

Your first ruby code

Create a file in your linux using the following command

shell> touch helloworld.rb

or open up vi editor with the name of the new file to be created

shell> vi helloworld.rb

In the file that is opened, write your code by taking the following steps

1. Press i key on your keyboard
2. You will enter the write mode and now you can write your text [puts "Hello World"]

Now save and close down the file by taking the following steps

1. Press ESC key on your keyboard
2. Write :wq and Press Enter

The file will get saved and you will be thrown out to the shell prompt.

On shell prompt write the following command

shell> ruby helloworld.rb

You will shown the following output

Hello World

Thats it to writing your first ruby script and executing it on linux.

Go back to the table of contents for 'Ruby on Linux'

Setting up Ruby on Linux

The following are the steps that I performed to install ruby on my linux machine.


I went to http://www.ruby-lang.org/en/downloads/ There I found out a section with the heading Ruby Source Code

I pick the url of the link for the file using the right click and choosing the 'copy link location' in my firefox.

Then I went to the shell on my linux and put the following command

shell> wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz

This downloaded the ruby source code to my root directory. I extracted the file using the following command

shell> tar -xvf ruby-1.8.7-p72.tar.gz

This command extracted the source archieve to ruby-1.8.7-p72 directory. Then I enter into this ruby-1.8.7-p72 directory and executed the following command

shell> ./configure

This is actually not a command rather a script, that is run to check the required configurations for the installation purposes. At the successful run of this script I executed the following command

shell> make

This make command compiled the code. At the end of the make command I executed the following command

shell> make install

This make install command installed the ruby and placed the ruby files in appropriate folders.

Thats all to what I did for installing ruby on a linux machine.

You can confirm your ruby installation by using the following command

shell> ruby -v

Go back to the table of contents for 'Ruby on Linux'

Ruby on Linux

Ruby on Linux

This is an effort to collect a guide for doing ruby development in a collection of step by step tutorials. The following is table of contents list for links to the articles in this same blog. You will see some articles completed while others are in process. Thanks

Table of Contents

1. Setting up Ruby on Linux (Completed)
2. Your First Ruby Code (Completed)
3. Connecting to MySQL database via Ruby (In process)
4. Using Shell Commands in Ruby (Completed)
5. Getting System information using Ruby code (In process)
6. More to come ...

Free Advertising