Tech Blog :: shell


Aug 10 '11 4:06pm
Tags

Mac shell trick: have Growl notify you when something happens

Let's say you're waiting for some event to happen, and that event is detectable in your unix terminal. Examples: a DNS record to propagate (use ping and grep), a blog post to appear in an aggregator (curl and grep), etc. Instead of checking it every minute, you can have your terminal run a loop until it finds the condition you've set, and when it does, it'll notify you with Growl.

I'll use the DNS-test example here:

To check if domain.com is mapped to 1.2.3.4, we can run,
ping -c1 domain.com | grep "1.2.3.4"; echo $?

Or to check for the appearance of some HTML on a web page, we can do,
curl -s http://somesite.com | grep "some content"; echo $?

The $? at the end checks the exit code - 0 being success, non-0 being error.

Now put that in a loop, running every 30 seconds, with a growl popup when it succeeds:

while true; do
  FOUND=`ping -c1 domain.com | grep "1.2.3.4"; echo $?`; 
  if [[ "$FOUND" -eq "0" ]]; then growlnotify -t "Alert" -m "FOUND" -s; break; fi; 
  sleep 30; 
done

Or in one line,
while true; do FOUND=`ping -c1 domain.com | grep "1.2.3.4"; echo $?`; if [[ "$FOUND" -eq "0" ]]; then growlnotify -t "Alert" -m "FOUND" -s; break; fi; sleep 30; done

Nov 10 '09 3:33pm
Tags

Shell shortcut for SVN URL

This took me some time to figure out. It's a shell command to extract the URL from an svn info command. Run this (or put it in .bash_profile):

alias svnurl="svn info | egrep '^URL: (.*)' | sed s/URL\:\ //"

Then you can do things like svn ls $(svnurl) or svn log $(svnurl) to run commands on the remote repository. I expect this'll save me a bunch of time in the future.

And this is another one I've used for a while, to view changes in a working copy, especially useful when there are externals that output text even when there are no changes:

alias svnnew='svn st | grep -e "^[!M?~ACDR ]"'