The *nix Rube Goldberg Machine


Learn your shell!

Last time I posted a quick intro into basic shell programming.

“Bash isn’t programming, its scripting…”

“Shut up troll, its programming”

This time I’d like to quickly explain loops.

I’ll give you a quick example of how you can use a loop to do some simple things.

Lately I’ve been working on FubuMVC and the project has a *few* repositories and it can be a pain in the ass to update all of the git repositories. Luckily bash is here to help.

I happened to have all Fubu related project in one directory so bash makes this pretty simple.

~> cd ~/lecode/fubuproj

~/lecode/fubuproj> ls

<br /> .<br /> fubumvc<br /> FubuFastPack<br /> ripple<br /> bottles<br /> fubucore<br />

Ok now lets test out a simple for loop.

~/lecode/fubuproj> for d in `ls`; do echo $d; done

<br /> .<br /> fubumvc<br /> FubuFastPack<br /> ripple<br /> bottles<br /> fubucore<br />

Ok awesome, now we are getting some where.

“But what if I have files as well as directories…”

~/lecode/fubuproj> for d in `ls -d */`; do echo $d; done

When you are working in bash, there are a lot of gotchas that can catch you off guard. One of those gotchas is aliases… I have ls aliased to `ls –color -xXw80` to make it look the way I want it to by default. The gotcha is –color that will bit you when you try and pipe color into other things. So to avoid aliases you can point to ls directly from /bin/ls

~/lecode/fubuproj> for d in `/bin/ls -d */`; do echo $d; done

Now you can cd into the directory and run `git status` to make sure you don’t have any outstanding changes.

~/lecode/fubuproj> for d in `/bin/ls -d */`; do cd $d; git status; cd ..; done

If everything is checked, I’ll make sure they are all on the master branch

~/lecode/fubuproj> for d in `/bin/ls -d */`; do cd $d; git checkout master; cd ..; done

Fetch the latest changes…

~/lecode/fubuproj> for d in `/bin/ls -d */`; do cd $d; git fetch; cd ..; done

And catch up my master branch

~/lecode/fubuproj> for d in `/bin/ls -d */`; do cd $d; git rebase origin/master; cd ..; done

Nice… Now all my projects are up to date.

HerpDerp

-Ryan

The *nix Rube Goldberg Machine!