Monday, October 5, 2009

Simplify your workflow using bash scripting

by Roman Leinwather

It has been only couple of months when John introduced me to bash scripting, specially creating aliases in bash_profile. For those of you who do not know how the aliases in bash profile work I will explain it very simple way. It is like giving a set of commands and name in one place and be able to type the name in terminal and all these commands will be executed.

As I have mentioned before I set up alias for every new project. There is a simple reason. When you are using git for pulling and pushing code into git repository you always need to be in the root folder of that project. So instead of typing cd /path/to/the/project I can just set alias my_project and assign the path to it. Which in reality means that I type the project name in the terminal and I am straight away in the project’s root.

You can do lot more than just setting aliases for all your projects. You could for example set your system command ls (which lists all the files in the folder) following way alias ls = ‘ls -lhAFG’. This brings you coloring full file details and so on, instead of just simple inline list. You could also setup your ssh connection to remote servers so you do not have to always type the user name and server’s address. Or even set the secret key you use for ssh connection to production server or services like Amazon S3 or EC2.

I have been using aliases this way for couple of weeks and it has been very useful and it simplified my work-flow a lot but then last week I had a cool idea. What if I could go even further and let my computer do even more work for me.

So here is the scenario

Every time I get new project (and it is quite often these days) I have to create folder with a project name in my projects directory and also sub-directories such are photoshop files, documentation and so on. I also need to create a folder on my localhost server and in most cases copy the WordPress files into that folder. I have decided to do all this from one line of my terminal. I have set alias called new_project_thrudigital_wordpress. I have also setup couple of others for my own projects but let me explain you how this one works.

First of all this is the code set in my bash_profile.

alias new_project_thrudigital_wordpress=’cd /Applications/MAMP/htdocs/;
mkdir $projectName;
cd ~/Install/wordpress/;
cp -R * /Applications/MAMP/htdocs/$projectName;
cd ~/Projects/ThruDigital/;
mkdir $projectName;
cd $projectName;
mkdir Documentation;
mkdir “Photoshop files”‘

If you are familiar with system commands you probably got the idea straight away. I am basically changing directory (cd), making directory (mkdir), copying files (cp). That is all what it is but the advantage that I only have to type this once is brilliant. As you can see I use variable $projectName which represents the actual name of the project. I have found out that I need to set the variable before I call the alias so this is one line of code I need to execute for new project.

projectName=”xy” new_project

This will do all the work. I could probably go even further and do even more cool stuff (I probably will ;)). Let me know what are you ideas. Do you know bash scripting? Have you consider using it? What could we use it for?

I am looking forward to your ideas.