Search

 


Time travelling into the future at a rate of 1s/s

Change Current Directory with Ruby script

Posted by Paul McConnon Sat, 27 Oct 2007 10:47:00 GMT

I was searching online for a method to change the current working directory of a Bash session using a ruby script.

Essentially I wanted to write a shortcut script that would take me straight to the directory of whatever Rails application I was working on.

I wanted a script called ‘go’ that I could use like this:

paul@ubuntu:~$ go plopcentral
paul@ubuntu:~/Development/Sites/PlopCentral$

The script would search a few predefined base paths for the folder and then CD to that folder.

I first tried writing in pure Ruby but unfortunately, executing a ruby script that changes the current working directory within the script, loses these changes when the script exits (this is by design and completely correct, a script should not be able to affect it’s parent process)

I came up with a workaround by making the ruby script return the location of the first folder it found and used a Bash function to change to the returned folder.

Now for the script go.rb, store this in your home folder

#!/usr/bin/ruby
# searches PATHS for a folder then cds to that folder
search = ARGV[0] #get search term
PATHS = ['~/Development','~/Documents']
result = nil
PATHS.each {|path|
    result = `ls -R #{path}/ | grep -m1 -i /#{search}`.gsub!(/:$/,'') unless result
}
puts "cd #{result}" if result

This uses the OS’s ls and grep to very quickly find a path with the search term.

This on it’s own is not enough, we need the OS to take that output and use it to change the current directory. To accomplish this we need to define a function in our ~/.bashrc file.

Add the following to the bottom of your ~/.bashrc file.
go()
{
    eval $(~/go.rb $1)
}

This function takes it’s first parameter and passes it to the Ruby script defined before, eval’s it and uses the output (if any) to change working directory of the current Bash session.

New you can jump straight to folders with

go path

where path is a partial part the folder you want to cd to.

This is a very simple use of the method but illustrates how to use ruby with Bash and could be extended so that it uses databases etc.

Imagine Activerecord or more complex Ruby libraries for use in Bash.

Note: For those very familiar with shell programming there are bound to be pure Bash ways to accomplish this. I am more familiar with Ruby and wanted to use it.

Tags , , ,  | 40 comments | no trackbacks

Comments

  1. Avatar Watch Series said over 2 years later:
    This is far easier for a user to simply log onto a webpage without registering than to have to spend time registering and downloading often large programs.
  2. Avatar Drug Treatment said over 2 years later:
    Thanks mate. This is such good info for my research. I will bookmark your post here on Digg.
  3. Avatar buy essay online said over 2 years later:
    This is really a very simple use of the method!
  4. Avatar payday loans online said over 2 years later:
    Very interesting and informative article I think. I have to admit that I always follow your website because it is full of various information about everything. Reading this post about the Ruby script I have known many new information which I have not known before. Some parts of your article was a little bit difficult for me to understand but when I read everything twice, everything was clear for me. Thanks a lot one more time for the useful information, without you I would never known about such a thing ever.
  5. Avatar search engine optimization said over 2 years later:
    You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
  6. Avatar search engine optimization said over 2 years later:
    You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
  7. Avatar Cherry said over 2 years later:
    Thanks a lot one more time for the useful information, without you I would never known about such a thing ever. secret life of the american teenager season 2 episode 22
  8. Avatar sibat said over 2 years later:
    Official balenciaga handbags Online Shop - offering you balenciaga inspired fashion and lifestyle for women balenciaga handbags
  9. Avatar kjkjk said over 2 years later:
    Thanks for the script, I wsite! coll
  10. Avatar free shemale cams said over 2 years later:
    I can you this script on webcam sites, thanks!
  11. Avatar mitre saw bench said over 2 years later:
    Some user left spam, please get rid of it,
  12. Avatar table bench saw said over 2 years later:
    Cool post.
  13. Avatar circular saw benches said over 2 years later:
    I will read this over and over
  14. Avatar The Employment Law Group said over 2 years later:
    Its a pleasure reading your post.
  15. Avatar louis vuitton handbags said over 2 years later:
    This blog is very rich, content, will often come around. correctalittle.com
  16. Avatar sports betting said over 2 years later:
    It's a good post.
  17. Avatar Online Casino said over 2 years later:
    I love this post.
  18. Avatar Web Hosting said over 2 years later:
    I love this post. Expecting more like this.
  19. Avatar credit card said over 2 years later:
    Awesome.
  20. Avatar Insurance said over 2 years later:
    I read it. Its good.
  21. Avatar biofuel said over 2 years later:
    Good post.
  22. Avatar auto insurance said over 2 years later:
    You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
  23. Avatar meralyn said over 2 years later:
    just click this to paly.....casino en ligne
  24. Avatar Sylvia Jordan said over 2 years later:
    casino en ligne francais
  25. Avatar car hire kerry said over 2 years later:
    car hire kerry
  26. Avatar host guide said over 2 years later:
    host guide
  27. Avatar web hosting guide said over 2 years later:
    web hosting guide
  28. Avatar web hosting news said over 2 years later:
    web hosting news
  29. Avatar casino online said over 2 years later:
    casino online
  30. Avatar sports betting online said over 2 years later:
    sports betting online
  31. Avatar car hire france said over 2 years later:
    car hire france
  32. Avatar car hire ireland said over 2 years later:
    car hire ireland
  33. Avatar car hire italy said over 2 years later:
    car hire italy
  34. Avatar car hire portugal said over 2 years later:
    car hire portugal
  35. Avatar Hire a car Bergamo said over 2 years later:
    Hire a car Bergamo
  36. Avatar New Cars said over 2 years later:
    New Cars
  37. Avatar Hotel Accommodation said over 2 years later:
    Hotel Accommodation
  38. Avatar Car dvd players said over 2 years later:
    Eonon Manufacturer is an online trading firm based in Hong Kong. The objective is to cater for the needs of car audio,car dvd,car video,small & medium enterprises, home users and item collectors.
  39. Avatar xbox live said over 2 years later:
    At acemegastore we specialize in XBox , xbox 360 accessories , xbox 360 controller. We also have a full range of major brand modded consoles. We are you on stop gaming shop
  40. Avatar Canvas Print said over 2 years later:
    Canvas Prints & Photos: We create stunning canvas prints by printing your photos on canvas. For Canvas Photo, Canvas Pictures, Canvas Print, Canvas Printing visit photobloc.com.

Trackbacks

Use the following link to trackback from your own site:
http://www.plopcentral.com/trackbacks?article_id=1358

(leave url/email »)

   Preview comment