Saturday, January 16, 2010

Scripting in Ruby

I just read, or rather skimmed, the book, called Everyday Scripting with Ruby and it is awful. I had high expectations. I was expecting something like Perl for System Administration, where you right away get into hard core Perl scripting. This book is nothing like that! It is a really basic introduction to Ruby, and if you have any experience programming at all, DON'T BUY THIS BOOK.

OK, enough complaining! I am going to do what this book does not do. I am going to show a good way to write scripts in Ruby. If you want to learn more about Ruby, buy Programming Ruby, The Well-Grounded Rubyist and The Ruby Way.

Ruby is an excellent language. It is, in my opinion, the best scripting language and it beats both Perl and Python face down. If you are not scripting in Ruby, you are missing out. Ok, so here is the script.


The script is called sendgmail and it sends mail via gmail (surprise!).

This is how the command is used.

$ sendgmail --help
/usr/local/bin/sendgmail [options] attachments...
Options are ...
    -t, --to TO                      Send email to recipient
    -m, --message MESSAGE            Include the message.
    -s, --subject SUBJECT            Include the subject.
    -v, --verbose                    Log to standard output.
    -V, --version                    Display the program version.
    -h, -H, --help                   Display this help message.

And here is the code, liberally sprinkled with comments.

#!/usr/bin/env ruby
# Use the currently configured ruby version

# optparse contains OptionParser, ostruct: OpenStruct and growl Growl
require 'optparse'
require 'ostruct'
require 'growl'

# This is the name of the script that is called
# Whatever you name your script will be reflected here.
# This can be useful if you want to have the same script do many things
# Create differently named links to the script 
# and use the program name to select behavior.

# Create an OpenStruct to save the options.
# OpenStruct allows you to use options.my_option instead of 
# option['my_option'] with a normal hash
def options
  @options ||=

# This is the options of the program, see OptionParser
def program_options
          # The values of the array are,
          # [long_option, short_option and parameter, description, code to execute]          
          ['--to', '-t TO', "Send email to recipient",
           lambda { |value| = value }
          ['--message', '-m MESSAGE', "Include the message.",
           lambda { |value| options.message = value }
          ['--subject', '-s SUBJECT', "Include the subject.",
           lambda { |value| options.subject = value }
          ['--verbose', '-v', "Log to standard output.",
           lambda { |value| options.verbose = true }
          ['--version', '-V', "Display the program version.",
           lambda { |value|
             puts "#{program_name}, version #{PROGRAM_VERSION}"

option_parser = do |opts|
  opts.banner = "#{PROGRAM_NAME} [options] attachments..."
  opts.separator ""
  opts.separator "Options are ..."

  # Add the command on_tail, to make it appear as the last option in the list.
  opts.on_tail("-h", "--help", "-H", "Display this help message.") do
    puts opts

  program_options.each { |args| opts.on(*args) }

  # Parse the options and remove them from the ARGV array
rescue OptionParser::ParseError => error
  puts error.message
  puts option_parser

  puts 'Missing required argument --to or -t'
  puts option_parser

options.subject = 'No subject' unless options.subject
options.message = '' unless options.message

# Concatenate the options into a proper command
command = 'sendemail -o tls=yes -s -f -xu -xp secret'
command += "-t '#{}' -u '#{options.subject}' -m '#{options.message}' "

# Append the filenames with the -a option to send them as attachments
# Only the non options (the filenames) are left in ARGV
unless ARGV.empty?
  command += " -a #{ARGV.join(' ')}"

# Print the command to screen if using verbose mode.
puts command if options.verbose
system command

Growl.notify "#{options.subject}\n#{options.message}", :icon => :jpeg, :title => "Email sent to #{}"

The script requires you to install sendemail. It also requires you to install the growlnotify command line utility that is distributed with Growl and the gem that uses this utility, also conveniently named growl.

Install required dependencies on OS X

# Install sendemail and capabilty to send secure mail, required by Gmail
sudo port install sendemail p5-net-ssleay p5-io-socket-ssl

# Install growl command utility from the mounted image
sudo sh /Volumes/Growl-1.2/Extras/growlnotify/

# Install the growl gem
sudo gem install growl

That’s it, a simple script that will allow you to send messages and files through
gmail from the command line.

No comments: