This website uses cookies. If you continue to use this site we will assume that you are happy with this. When you close this box we will save this information in a cookie to ensure you'll never be bothered with this information box. ×

Ruby for Admins is designed to be a source of information and inspiration for all Sysadmins, DBAs, Application Administrators, who are tired and bored scripting with XX century, ancient languages, tools and shells, and who want to learn to use modern programming languages and techniques. If you already know Ruby, you might find here an inspiration for using it in your daily work. If you don't know it yet, you'll be able to learn the basics here - but only the basics, do not expect full Ruby course.

New chapters:
Lambda Functions (almost 2 years ago)
Config files (almost 2 years ago)
Passing Arguments to Script (over 2 years ago)
Background Jobs (over 2 years ago)

The format of this webpage is blog-o-book, which is not exactly a blog nor a book. You can browse and read it as a book, starting with the Chapter One or read it as a blog, browsing the articles, adding a comments (keep an eye on this symbol), linking to selected paragraphs. It is all up to you. And last but not least, it is free!

Go to the First Chapter


To give you an idea how quick and easy is to work with Ruby, let's take a look at almost-real-world example. Assume you want to develop a shell script, which creates a series of SQLs to kill all the active Oracle sessions. In the Shell, you might write it like this:

sids_and_serials=`sqlplus -L -S $1/$2@$3 << EOF
  set feed off
  set head off
  set pages 0
  set line 1300
  set termout off
  select sid || ',' || serial# from v\\$session where status='ACTIVE';
if [ "$ret_code" -ne 0 ]; then
  echo "Can't connect to the database!"
  for s in $sids_and_serials; do
    echo "alter system kill session '$s';";

In Ruby, the same script would look like this:

#!/usr/bin/env ruby
require 'oci8'
dbconnection =[0], ARGV[1], dbname=ARGV[2])
dbconnection.exec("select sid, serial# from v$session where status='ACTIVE'") do |row|
  puts "alter system kill session '#{row[0].to_i}, #{row[1].to_i}';"

First at all, there is no need to run sqlplus anymore - thanks to oci8 gem (gems are Ruby libraries and oci8 is the one for Oracle connectivity) loaded in the second line with require command. In the Shell Script, you should check the return code and break the script if sqlplus fails, to ensure the script will not continue. In Ruby, there is no need to do it: in case when cannot connect to the database generates an exception and break the program flow. As you probably guess, ARGV is the array with command line arguments, puts is an equivalent of echo in shell, and #{} is used for string formatting. But where is the for loop? Well, we will try to solve this mystery in the next chapters.


This book is written by Tomek 'Grych' Gryszkiewicz. Do not hestitate to contact me at, check my page or just add a comment here:

©2014 Tomek Gryszkiewicz. All rights reserved.