4/25/2011

Bash exercises

At work we have some legacy, proprietary, in-house built HTML Template System for web-page generation. This solution looks reasonable and solves the tasks it was written for. But it causes one small problem to me, as to Developer/User of this System. It has no Source Version Control Tool "beyond". Just two servers. One for development/testing and the other for production. All source code is located just in folders on both Servers. And there are scripts to sync folders between Servers.

That evolves a bunch of problems (according to my restless mind):

1) Process demands to make backups before you upload changes on dev/test Server. You can change Template directly on Server, but anyway you need a backup.

2) You have to ask your colleagues if somebody works on some ticket for this System or not. Just to eliminate possible collisions.

3) It is quite boring and hard to remember all files you've changed locally. Just not to forget to upload them on Server.

I decided to write a couple of scripts using Bash, Scp and Git to make my life quite easy. The Idea is quite simple:

a) You provide to the script the ticket number, Server-Doc-Base and Template folder for download. The script creates local folder with name of the ticket number, copy all files from Template folder on Server to your local machine and commit everything in Git. Using aliases and functions in .bashrc you can define a bunch of "shortcuts" and eliminate the second parameter (Server-Doc-Base) for your scripts. This parameter is changed quite rarely and we can fix it.

b) The whole magic, actually, will be done by Git. After we change a few files and want to test our changes, we just ask Git about local changes, check those files on Server (they should not be changed while we were working on them) and upload them with final commit to local Git.

That is it. Git allows you to do many very interesting things, this is why I like it.

But actually I would like to mention a small issue I encountered during work with Bash. Unfortunately, this code:
COUNTER=0 git status | while read LINE
do
 ((COUNTER++))
 echo $COUNTER done echo $COUNTER
will work not as you would expect it. The last counter value will be ZERO! I had spent quite long time before I understood what was going on and found this very helpful post.
But anyway, Bash is a powerful tool, use it, like it, and it will help you to make your daily routine much easy.

2 comments:

Unknown said...

Usually you have to write: COUNTER=$(git status | wc -l)

denlion said...

There was additional logic inside the loop. Counter was just one of the tasks actually...