Home > Computer Technology, Problem Solving, Unix-Linux-Os > From Bash to Ruby – part 1

From Bash to Ruby – part 1

November 15th, 2009

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
  • Technorati
  • Live
  • Slashdot
  • Sphinn
  • Mixx
  • Yahoo! Buzz
  • StumbleUpon
  • Facebook
  • MSN Reporter
  • Reddit
  • RSS
  • Add to favorites
  • FriendFeed

Related posts:

  1. Linux Bash scripting 101 (part a) There are a number of choices for the user shell...
  2. Linux Bash Scripting 101 (part b) This post is part of a multi-post series on scripting...
  3. Eye-Fi WiFi part 2 (update time) There have been several news articles covering privacy issues and...
  4. Automating Standard Script Generation Some thoughts on Automating and standardizing how you might create...
  5. Music, Iterative Patterns and Problem Solving (part 4) Picking up from the last post... Patterns are key -...

Comments are closed.
________________________________________________
YOUR GeoIP Data | Ip: 38.107.179.221
Continent: NA | Country Code: US | Country Name: United States
Region: CA | State/Region Name: California | City: Glendora
(US only) Area Code: 626 | Postal code/Zip:
Latitude: 34.132099 | Longitude: -117.851097
Note - if using a mobile device your physical location may NOT be accurate...
________________________________________________

Georgia-USA.Com - Web Hosting for Business
____________________________________