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

Thursday, October 2, 2008

Hard or Soft link

How a Hard link is created?

A hard link to a file is created using the following command

root@xyz# ln file1 file2

The above command makes file2 a hard link to a file represented by the hard link file1.

How a Soft link is created?

The soft link to a file is created using the following command

root@xyz# ln -s file1 file3

The above command makes file3 a soft link to a file represented by the hard link file1

How a file with multiple hard links is deleted?

A file with multiple hard links is only deleted after all its hard links are deleted. Then the question is, Is there any way that we can remove all of the hard links in a single command? The answer to this question is ...*

How a file with a soft link is deleted?

The file with a soft link is deleted normally by using the rm command. In this case the soft link becomes broken, i.e. not linked to anything.

What happens to the soft links if a file is moved from its original location?

In case a file is moved from its original location the soft link pointing to this file becomes broken. If the pointed file is moved back to its original location, the broken link becomes active again.

What happens to the soft links if a file is removed?

If a file is removed the soft links continue to exists but becomes broken.

What happens to the hard links a file is moved from its original location?

It makes no difference. By moving a file having multiple hard links means moving a hard link. So if a hard link is moved to some other place, it does not effect any other hard link pointing to the same file.

What happens to the hard links if a file is removed?

It just is not possible to remove a file having at least one hard link. If you remove one hard link, that particular link is deleted not the actual file. The actual file is only deleted when the last hard link pointing to the file is removed.

How can we list all of the hard links or soft links to a particular file?

List Hard Links

You can list all of the hard links to a file 'file1' by using the following command

root@xyz# find [directory to search] -samefile [file name (which can be any hard link) as argument]

for example

root@xyz# find / -samefile file1

or

root@xyz# find . -samefile file1

You can also use the inode number for searching the hard links to it

for example

root@xyz# find . -inum [inode number]

You can find the inode number of a file by using the following command

root@xyz# ls -i

List Soft Links

You can list the soft links to a file using the following command

root@xyz# find -lname file1

It is better to put a * as prefix in the file name like below

root@xyz# find -lname "*file1"

Because we have not mentioned the directory where the search should be made so the search will be made in the current directory. We can mention the directory where the search should be made in the following way

root@xyz# find . -lname "*file1"

or

root@xyz# find / -lname "*file1"


For more details you can have a look at the following resources

  1. Hard and Symbolic link [A free registration will be required for the above link]
  2. Symbolic link
  3. Hard link

Free Advertising