Shell Scripting

I’m inspired. I have watched a couple of Gary Bernhardt screencasts on Destroy All Software - particularly on Unix scripting and I guess I am a inspired and quite happy that I already can put into practice what I learned.

Case 1

Had to investigate a production issue couple of days ago. I tailed the log file and saw a lot of noise from New Relic agent - which I am not really interested in. So I need to remove these New Relic agent lines so that I concentrate on finding the real errors.

Sed to the rescue:

sed 's/New\ Relic\ Agent\ not\ running\.//g' importing.log| sed '/^$/d' > importing_cleaned.log

The first sed command, replaces the string “New Relic Agent not running” with empty string. This unfortunatelly leaves the newlines behind. The second sed command takes care of that, /d at the end is for delete, the pattern that we want to delete is ^$. What’s ^$? Regex speak, ^ is the beginning of the line and $ is the end of the line. So ^$ with nothing between them represent an empty line.

There’s a better way to achieve what I did above for sure, I am just happy that I can get to learn a little bit about sed.

Case 2

Naughty me, making changes to the code without creating test for it nor running the full test suite. When I remember to do that, surprise surprise I broke some tests. I didn’t know for sure which commit that introduces this bug, so I want to run the broken spec on the last few commits.

The following script did just that - this one is truly inspired by Gary’s screencast (note: broken down to few lines for readability):

git log -5 --pretty=format:'%h' |    
while read revision; 
    do echo "Checking out $revision"; 
    git co $revision -b temp_branch; 
    be rspec spec/requests/customer_request_spec.rb; 
    git co master; 
    git br -D temp_branch; 
done

Again could have been done better.

What the command above does is:
Firstly find the hashes of the last 5 revisions, pipe the result to the while loop. Inside the loop, check out the revision into a new branch, run the spec on that branch (be is my alias for bundle exec), check out master and delete the branch - repeat.

Summary

Unix has powertools, learn them.
Might be useful:
The Unix Chainsaw - Cascadia Ruby 2011
Destroy All Software