bash aliases
Bash aliases are such lovely things, so I thought I would share some of my favorites and maybe even get some feedback.
Most of my aliases are there to make software development more enjoyable and less time consuming. As such, most of them deal with the projects I have in my project directories. Here are some of the more interesting ones:
for bigdir in $(ls ~/Developer/Projects/ | cut -d' ' -f7)
do
for dir in $(ls ~/Developer/Projects/$bigdir)
do
alias cd$dir="cd ~/Developer/Projects/$bigdir/$dir"
alias m$dir="cd$dir && mate ."
alias s$dir="cd$dir && ss"
alias g$dir="~/bin/svngrowl $dir"
alias o$dir="osascript -e 'tell application \"Terminal\"'
-e 'tell application \"System Events\"'
-e 'tell process \"Terminal\" to keystroke \"t\" using command down'
-e 'tell process \"Terminal\" to keystroke \"t\" using command down'
-e 'end tell'
-e 'do script \"s$dir\" in first tab of front window'
-e 'do script \"m$dir\" in second tab of front window'
-e 'do script \"g$dir\" in third tab of front window'
-e 'do script \"localhost\" in second tab of front window'
-e 'tell second tab of front window to set selected to true'
-e 'end tell'"
done
done
alias localhost="open http://localhost:3000"
alias addall="svn st | grep ? | cut -d' ' -f7 | xargs svn add"
alias removeall="svn st | grep ? | cut -d' ' -f7 | xargs rm -Rf"
for var in "server" "generate" "console" "destroy" "plugin"
do
alias s${var:0:1}="script/$var"
done
The first for loop is probably the most useful set of aliases that I have. It starts off by going through all the projects that live in my various project directories. Then, for each project it gives me a cdproject, mproject, sproject, gproject (very special), and oproject (less special, but still useful).
cdproject
- cd's into the project directory
- opens the project in TextMate
- starts a server for the project by calling script/server (which is what the alias ss does in a rails project)
- starts an svngrowl process for the project (which I'll explain later)
- Uses the command line osascript, which invokes applescript, to manipulate Terminal and Firefox
#!/bin/sh
source ~/.profile
alias | grep cd$@= | awk "{gsub(/cd$@='cd |'/, \"\"); print}" |
awk "{gsub(/~/, \"/Users/`whoami`\"); print}" | xargs ~/bin/svngrowl.rb
And now the ruby script:
#!/usr/bin/env ruby
begin
directory = ARGV[0]
project = directory.split("/").last
ignore = directory.gsub(project, "")
loop do
updates = `svn up #{directory}`.gsub(ignore, "").split("\n")
last_log = `svn log #{directory} --limit 1`
revision = updates.pop.scan(/\d+/)
committer = last_log.split("\n")[1].split(" | ")[1]
priority = updates.join("\n").include?("db/migrate") ? 1 : 0
message = last_log.split("\n")[2..-2].join("\n")
unless updates.empty?
`growlnotify -s -t "#{project} Update - #{committer}"
-m "#{message + "\n\n" + updates.join("\n")}" -p #{priority} --image ~/bin/mph.png`
end
sleep 180
end
rescue
`~/bin/svngrowl.rb #{directory}`
end
All the bash script does is finds the proper directory for the project in question and passes it to the ruby script. The ruby script in turn does an svn up on the directory, collects all the interesting data, and runs growlnotify (the command line growl utility) displaying all of the interesting data. There is also a rescue statement in there that just restarts the process if something goes wrong (an error is thrown if svn can't update properly, which can happen if the network drops out or if you do svn st at the same time that this process is doing svn up). I also used the --image options to pass in a beautiful picture of Mr. Potato Head to use as the icon shown on the notification.
Please feel free to use any of these to make your work/play more productive (except for the picture of Mr. Potato Head... that one is mine!)
Update: My aliases, along with some other handy scripts can now be accessed courtesy of github: utilities.
Posted by Steve on Saturday, November 03, 2007