The *nix Rube Goldberg machine – functions!

Learn your shell!

So a couple weeks ago I finally decided to learn how to use bash functions. I knew they existed but I decided to ignore them for the longest time.

Often times you find yourself typing the same things over and over again into the shell. In our app we deploy 3 windows services and managing them can get really tedious.

~/lecode>sc query DovetailEmail-default
~/lecode>sc stop DovetailEmail-default
~/lecode>sc delete DovetailEmail-default

~/lecode>sc query DovetailRulesEngine-default
~/lecode>sc stop DovetailRulesEngine-default
~/lecode>sc delete DovetailRulesEngine-default

~/lecode>sc query DovetailMessaging-default
~/lecode>sc stop DovetailMessaging-default
~/lecode>sc delete DovetailMessaging-default

That’s a lot of typing, granted its not that bad when you use your bash history. But you end up hitting the up arrow and changing one word a lot.


So when you keep repeating yourself over and over again you can quickly write a function that can save you precious keystrokes

~/lecode>function fsc() {
for name in Email RulesEngine Messaging
sc $1 Dovetail$name-$2

Now I can happily type
~/lecode>fsc query default
~/lecode>fsc stop default
~/lecode>fsc start default

It works for other repetitive tasks as well, ie the steps to deploy our app is as follows

~/lecode>rm -rf mydefault
~/lecode>bottles create-all
~/lecode>bottles bundle mydefault -profile default
~/lecode>mydefault/BottleRunner.exe deploy -profile default

Again… pretty easy to just use my bash history and hit the up arrow to run all the commands
but its really easy to make a function

~/lecode>function fdeploy() {
rm -rf my$1
bottles create-all
bottles bundle my$1 -profile $1
my$1/BottleRunner.exe deploy -profile $1

Then bam!

~/lecode>fdeploy default
~/lecode>fdeploy storyteller
~/lecode>fdeploy hrdemo

See thats way mo ‘betta than creating a .bat file cause then you have run in cmd.exe, you have to ignore it in git repository, clean up after yourself later….

And I just don’t like that, not one bit.


About Ryan Rauh

My name is Ryan Rauh and I like shiny things. If I've seen it on twitter more the twice most likely I've looked at it or tried it. I really like memes and other silly and seemingly useless things out on the internet. I blog about things *I* think are cool and interesting, I hope you will find them cool and interesting too.
This entry was posted in Shell and tagged . Bookmark the permalink. Follow any comments here with the RSS feed for this post.