Ruby > Shell Scripts

7f9969d3b17c6fd9d604ad2c09a06d58?s=47 Tristan Hume
February 06, 2013

Ruby > Shell Scripts

Presentation I gave at the Ottawa Ruby Group on replacing shell scripts with Ruby.

7f9969d3b17c6fd9d604ad2c09a06d58?s=128

Tristan Hume

February 06, 2013
Tweet

Transcript

  1. Ruby > Shell Scripts By Tristan Hume

  2. Ruby without the web! Automate ALL the things!

  3. Ruby + Unix = :D

  4. A Smooth Transition • Backticks: `ps -ax`.lines.map(&:split) • Handy Methods:

    ◦ File.foreach("words.txt") {|line| puts line.capitalize} • Dir[]: ◦ Dir['**/*.rb'].each {|file| code_files << file }
  5. But I like all those little utilities...

  6. Have No Fear: FileUtils is here! require "fileutils" FileUtils.cp some_file,

    some_file + ".bak" FileUtils.rm_rf "trash" FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true FileUtils.chown nil, 'bin', Dir['/usr/bin/*'], :verbose => true
  7. In Which Ruby's Scripting Prowess Becomes Fully Apparent. += :Ruby_Awesomness

  8. My All-Time Favorite Little Gem gem install progress

  9. Computing: 12.35% (ETA: 2.4m) >> data.map.with_progress("Computing...") {|item| ...

  10. Awesomeness Comes In Many Forms data.with_progress do |item| data.with_progress.map do

    |item| # Any enumerable and any enumerable method (1..100000).with_progress.each_cons do |item| 10.times_with_progress('Counting to 10') do |i| (1..10).with_progress('Outer').map do |a| (1..10).with_progress('Middle').map do |b| # Nesting! end end
  11. An exhilarating wrapper for all things shell. (you can use

    IRB as your login shell if you really want to...) gem install rush
  12. require 'rush' file = Rush['/tmp/myfile'] file.write "hello" puts file.contents file.destroy

    puts Rush.my_process.pid puts Rush.processes.size puts Rush.bash("echo SHELL COMMAND | tr A-Z a-z") puts Rush.launch_dir['*.rb'].search(/Rush/).entries.inspect # Remote Execution local = Rush::Box.new('localhost') remote = Rush::Box.new('my.remote.server.com') local_dir = local['/Users/adam/myproj/'] remote_dir = remote['/home/myproj/app/']
  13. Another hardcore Ruby shell. gem install chitin

  14. $ chitin ~/src/chitin % ls ~/src/chitin % ll = ls

    -:al ~/src/chitin % hg.stat ~/src/chitin % hg.stat | wc | L {|i| i.size } ~/src/chitin % git.status > '/tmp/test.out' ~/src/chitin % 44 - 2 ~/src/chitin % _ + 5 ~/src/chitin % wget "http://thume.ca/" ~/src/chitin % _ # run the same command again # You can also use Chitin as a library Shell Commands in Valid Ruby
  15. Better than no GUI at all. Bonus: Crappy Tk GUIs

  16. "This is all crappy Unix stuff, what's in it for

    me?" - Possibly some audience member Using Windows?
  17. WIN32OLE FTW! require 'win32ole' excel = WIN32OLE.new('Excel.Application') excel.visible = true

    workbook = excel.Workbooks.Add(); worksheet = workbook.Worksheets(1); worksheet.Range("A1:D1").value = ["North"," South","East","West"]; worksheet.Range("A2:B2").value = [5.2, 10]; worksheet.Range("C2").value = 8; worksheet.Range("D2").value = 20;
  18. Thank God. Never touch Visual Basic for Applications Again!

  19. In which WIN32OLE and Tk GUI saves the day. Case

    Study
  20. TL;DR • Ruby has awesome utilities for scripting • There

    are some great gems like "progress" to help. • Never touch Visual Basic or Bash Scripting again, you won't regret it.
  21. Tristan Hume http://thume.ca/ Thanks