Official website for Linux User & Developer
FOLLOW US ON:
Jul
19

Ruby development for system administrators

by Koen Vervloesem

Koen Vervloesem doesn’t like shell scripts that are difficult to maintain, therefore he uses Ruby for his sysadmin tasks. Do the same with his four-page guide…

This article originally appeared in issue 89 of Linux User & Developer magazine. Subscribe and save more than 30% and receive our exclusive money back guarantee – click here to find out more.

Get your first digital copy of the magazine for iPhone and iPad free – just search for ‘Linux User’ on the Apple App Store now! twitter follow us

Resources:
Ruby programming language
Ruby gems
REXML

Most Linux and UNIX system administrators use a diverse mix of shell scripts and tools like grep, awk, cut and so on. The classical approach has proven its merits, but these scripts are generally not easy to read or to maintain. One solution is to use a real programming language for system administration tasks. In a complex environment, system administration can become much easier with a real programming language instead of shell scripts. Traditionally, Perl has been very popular among sysadmins, but some people maintain that this is not much better than shell.

In this article, we choose Ruby, a feature-rich but simple object-oriented programming language known from the popular web application framework Ruby on Rails. The Ruby programming language has many built-in and external libraries that can come in handy for typical system administration tasks such as file manipulation and text processing, log file analysis, logging into other servers, and so on. The gentle learning curve of this programming language, coupled to the easy-to-read and maintainable form of the scripts, makes this a valid choice for sysadmins.

Simple but effective
Let’s start with some basic features of the language, to show you why Ruby is in many cases a better choice than Perl or a shell script. First of all, everything in Ruby is an object, even primitive types such as numbers and strings. For instance, an array of numbers is just an object, which has methods and properties. This has a very simple syntax. For example, this is how you get the length of an array:

[1, 2, 3, 4, 5].length

And this is how you get the last element of an array:

[1, 2, 3, 4, 5].last

A second powerful feature are code blocks, which allow for shorter and easier-to-grasp code. A code block is a function without a name, that can be passed to another function as a parameter. For example, if you have an array of numbers, you could double each one in one line:

numbers = [1, 2, 3, 4, 5]
doubles = numbers.collect { |n| 2*n }
Pages: 1 2 3 4
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • Dummy00001

      The guide is quite informative.

      But since your site engine does not support proper code formatting, examples require tweaking before use.

      Or Ruby’s really capable of interpreting the WinWord-style single quotes – “require ‘csv’”?

    • http://respadas.net Ruben Espadas

      Nice introduction, the said “when men were men….” is forgotten, but the tools help more than “prestige”. Thanks for this article.

    • http://www.mclosson.com Matthew Closson

      @Dummy00001 Ruby can use single or double quoted strings. Double quoted strings give you some extra functionality when it comes to string interpolation but it is a fairly standard Ruby practice to use single quoted otherwise.

    • Pingback: Issue 89 | Linux User

    • Pingback: Bookmarks for 8 nov 2011 from 15:36 to 21:57 | jtheo