Sunday, June 23, 2013

Review of Linux Shell Scripting Cookbook

The main challenge in learning Linux shell scripting is the "tyranny of choice". A standard Linux shell gives you access to hundreds of commands. Some commands are very popular and generally useful while some are quite esoteric. Learning all these commands would take a lot of time and is counter productive. So when learning the Linux shell, one is faced with the question of which commands to learn. Aside from the hundreds of commands, Linux commands, especially the most useful ones, have lots of options. Most of the time, one could be productive by learning only the commonly used options. So because of the large number of choices when learning the Linux shell, a linear approach of learning the commands and the options one by one would not be practical.

The Linux Shell Scripting Cookbook uses a problem-based approach instead of the answer-based approach. It starts with what you want to do and provides the solution. How to do it; which commands to run and the options that goes for each command. It also discusses variations of the solutions. Learning in this manner, in my opinion, is a perfect fit for learning the Linux shell. It eliminates the need to make choices on what commands and option to learn. You determine what you need to do, look at the solution and eventually build a set of commands and options based on your needs. Thus, making you productive right away. After learning a set of commands, you'll be able to better judge which commands are more useful to you and can pursue to master those commands. In my case, I realised that I would be more productive with a deeper understanding of sed and awk.


Aside from being a very good starting point to learn the Linux shell, the cookbook approach also serves as a very good reference. Had the book used the answer-based approach, content is organised by commands, you would have to figure out which commands would probably be applicable to your problem.

My favorite chapter in the book which I always keep coming back to is chapter 4, "Texting and Driving". Every now and then, I have to process data in text files and because the discussion of sed, awk, grep, and regular expressions in this chapter is concise and focuses on what is most useful, I could afford to do a quick read of the topics, before I start working. I noticed that the use-cases tackled are quite close to what I often need to do.

All things considered, I highly recommend this book to beginners and intermediate users of the Linux shell. However, I think the title of the book can be quite misleading. At first, I thought that the book was only useful to learn shell scripting, but it is also very useful as a Linux command reference.

Saturday, June 22, 2013

Important Difference Between urllib and urllib2, Handling of Internal Server Errors

To be honest, I always ignored learning the difference between urllib and urllib2. I always thought that as long as I use urllib2 I'm okay. However, when I helped a friend debug code. I found a difference which I think might trip some newbie Python developers.

My friend used the urllib.urlopen function. At first we assumed that this function would raise an exception when it gets an Internal Server Error. The code uses the exception to automatically retry an http request, because the API server sometimes returns an Internal Server Error when its overloaded. After, noticing that requests were not being retried, I became suspicious that an exception is not raised. True enough, after some test, urllib.urlopen does not raise and exception when it gets an Internal Server Error. Luckily, the urllib2.urlopen function raises and exception when it gets an Internal Server Error. So the solution was to simply use urllib2.