Tuesday, October 4, 2011

substring in unix shell scripts

Recently I need to handle quite some unix shell scripts and find some tricks about strings that is less known.

1. substring
Most of my searches resulted using awk. I don't mind using it, but it make the script less readable. Luckily there is a easy way:
one_str="this is a string"
sub_str=${one_str:5:2} # ${org_str_var:0_based_index:length}
# now sub_str contains "is"
2. string search and replacement
Again, most search results say you should use "sed" or other external commands. But below line will also do the trick:
alpha="This is a test string in which the word \"test\" is replaced." beta="${alpha/test/replace}" #replace only the 1st insance
beta="${alpha//test/replace}" #replace all insances

Friday, September 23, 2011

.NET/windows Debugger list.

It's been quite a while since my last post. Anyway, I don't have any followers and this is just for myself to keep some ideas here.

This link gives a description of several debuggers/tools and their best usage scenarios. Though it seems to be quite powerful, I really hope some tools that can do debugger record and play like the Java world has now.

Friday, July 15, 2011

Git as SVN client: rollback to previous version

Git is a very power content based DCVS. I use it as a SVN client, since my company is still using SVN. I've been quite happy until yesterday: I made a mistake in a commit and need to roll it back. The problem is I already checked in to SVN and it broke some unit tests I did not cover on my local run, but the CI server did. I'm used to local reset to revert some bad changes, but this will only help when you have not pushed changes to remote sites:
git reset HEAD~
Above command will reset your master pointing to the previous commit. From Git's point of view, it's done. The current HEAD (master) now points to the desired copy of the codes you want to have. But the problem is once you do a synchronization with SVN, it's going to be reset back to the copy you want to get rid of. This is because Git is treating master tag as a pointer. When you do the above reset, it just move the pointer back without doing any real commit. So here what we really need is a "real" commit, which "git revert" provides.
git revert HEAD
After this, if you look at the history, you'll find a commit with the new result. Now synchronize with SVN and you'll find a new commit in it and the changes you made has been rolled back.

Note, git revert should only use when you have pushed your changes to remote sites. Then it gives you a chance to fix the problem. If your change is still local, git reset is recommended.

Maybe this is stupid, but it took me a while to figure this out, hope this helps some one out there who is also bothered by this.