Recent Posts

November 2009
M T W T F S S
« Oct   Dec »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Mobile Barcode Tool

This is a 2D-barcode containing the address of our mobile site.If your mobile has a barcode reader, simply snap this bar code with the camera and launch the site.

From Bash to Ruby – part 1

Bash - Changing from a proven, ubiquitous tool to a new one – need a reason?  In this case it’s time to explore Ruby - I’m not ready to change yet but I do want to see what Ruby might bring to the table.   I’m starting with a simple Ruby script (extracted from a sample in Ruby in Practice, by Jeremy McAnally & Assaf Arkin; the original script did the same image file conversion but also uploaded the file to an Amazon AWS ‘bucket’ and then ‘Tweeted‘ about the new image file.)

For most folks who have learned or used one programming language adding a new language is usually not so difficult.  In this case, before exploring the OO aspects of Ruby I want to understand where there is correlation between Ruby and Bash scripting solutions.

  1. Both scripts require a bogus argument (or they won’t run…)  Ruby has nice options for Argument Iteration as well as a ‘fail’ directive.
  2. Both scripts count the number of files processed.
  3. Both scripts use ‘isms’ to create new file names from old file names.
  4. Both scripts will call the same tool that copies, converts and modifies a JPEG image.
  5. Both scripts lack error checking and when run, would replace existing files

Notes: I am using this Ruby script and most likely a non-Rubyist coding style since it makes it simpler to compare/contrast with Bash…  Neither of these scripts are intended for production use – but they are useful in themselves and certainly they could easily be expanded or modified for other purposes.


Ruby version

#!/usr/bin/env ruby
#
fail "Usage:\n  ruby Script_NAME <NOP option>\n" unless ARGV.first

puts "Processing images"
count = 0  ### Ruby will complain if you leave this out...

### for each jpg file in the current folder
Dir['*.jpg'].each do |jpg|
   count += 1

   ### create a new file name using 'png' for the extension
   png = jpg.sub(/jpg$/, 'png')

   ### print the file names
   puts "#{jpg} => #{png}"

   ### use the 'convert' command with Ruby 'rand' (to vary rotation)
   ### scale the image 'down' and rotate
   `convert #{jpg} -resize 800 -bordercolor white -polaroid #{rand(20) -10} #{png}`
end
puts "Completed: #{count} files processed.."

Bash sample

#!/bin/bash
#
if [[ "$1" = "" ]] ; then echo "Usage: $0 <NOOP option>"; exit 1 ; fi

echo "Processing images"

### for each jpg file in the current folder
for FILE in *.jpg
do
   COUNT=$((COUNT +1))

   ### create a new file name using 'png' for the extension
   PNG=${FILE%.jpg}.png

   ### print the file names
   echo "${FILE} => ${PNG}"

   #RAND=$(ruby -e 'puts rand(20) -10') ## :)
   ### use Ruby above or Bash Built-in (below) to generate random number
   RAND=$[($RANDOM % 20) -10]

   ### use the 'convert' command with the random number (to vary rotation)
   ### scale the image 'down' and rotate
   convert ${FILE} -resize 800 -bordercolor white -polaroid ${RAND} ${PNG}
done
echo "Completed: ${COUNT} files processed.."
### for each jpg file in the current folder
Share and Enjoy:
  • LinkedIn
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Blogosphere News
  • Technorati
  • TwitThis
  • Live
  • Slashdot
  • Sphinn
  • Mixx
  • Yahoo! Buzz
  • StumbleUpon
  • Facebook
  • MSN Reporter
  • Reddit
  • RSS
  • Yahoo! Bookmarks

Related posts:

  1. Automating Standard Script Generation Some thoughts on Automating and standardizing how you might create...
  2. Migrating Static HTML pages to Wordpress CMS I previously posted about migrating static pages to Drupal –...
  3. Cobbler/Linux OS Provisioning via Virtual NICs (VirtualBox) This seems to be a topic of interest so here...
  4. Fedora – sort of broken KSH When you write perfect shell scripts you expect them to...
  5. Rails – where is the missing stuff? ROR (Ruby on Rails) is advertised as a data-centric development...

Leave a Reply - Please use your Real Name...

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>