I made the mistake of claiming to be a bit of a whizz with bash. Someone wrote to me and asked me how I did that. The short answer is that I’ve been using it or its predecessors for a very long time. This article is a bit of a meander, but I end by suggesting that In summary you need to understand how to implement sequence, selection, and iteration and how to use getopts and arrays.
I started with csh and then when I worked for Pyramid Technology, I was encouraged to shift to the Bourne shell, and then the Korn shell. I ended up at Sun Microsystems after the launch of Solaris and so the Korn shell remained my scripting language of choice.
At Sun, I missed the opportunity of learning both Perl and Java, although in order to write GUI programmes I picked up TCL/TK and wish . I use TCL/TK so infrequently that I always have a massive start-up cost when I need to write a program in it.
I left Sun in 2009 and while I had limited exposure to Linux during that time I soon made-up for that lack of experience. But bash is not that different from the Bourne or Korn shells, at least not today.
So I have been shell programming for quite a while. Before picking up shell most of my programming had either been in Cobol or SQL. The reason I mentioned this is it the Cobol programmes I wrote were primarily batch programmes and so I learned the basics of using sequence, selection, and iteration. These are the core logical structures, together with backtracking and boundary clash resolution, popularised by Michael A. Jackson in his book, “Principles of Program Design”. When learning a new language, which I have not done for a long time, I look to map the new language constructions to one of these three micro patterns.
For instance a restaurant menu might be considered a sequence of courses, each with a selection of dishes, or as an iteration of courses, again, each with a selection of dishes; the model gets more complicated if side plates are ordered as separate items.
Apart from learning selection and iteration control, there are two tools I’ve found very useful. These are getopts and arrays . getopts , together with case , allows powerful and succinct command line argument processing. Associative and indexed arrays allow pseudo dictionary and list processing. Within my ubuntu-tools repository in my examples folder I have a script called tgetops which illustrates both command line and array processing.
I also like the “&&” and “||” constructs, particularly for writing error handling code but it took me a long time to unlearn my Cobol habits and use these, and my bash remains quite verbose; I was very slow to adopted pipelines, although when I need it I use it. Bash also has a very useful left/right pattern extraction from environment variables.
The book I used to help me learn the Korn shell was, Anatole Olczak. 1997. The Korn Shell: User and Programming Manual (1st. ed.). Addison-Wesley Longman Publishing Co., Inc., USA., although I still have the 3rd edition dated 2001. Google is much better today and I haven’t looked at the book in years.
I don’t really write enough to claim to be a whizz but I can usually make it do what I want. Today I need to write some disk tidying scripts for a windows laptop and I was curious as to whether I should install bash on my windows system or seek to learn PowerShell, mind you I have seen quite sophisticated job control suites written in dos batch language. The advice I received was to install bash.
In summary, understand how to implement sequence, selection, and iteration and also how to use getopts and arrays.
Originally published but now amended at https://www.linkedin.com. This version gives better credit to Michael Jackson, and added a concise example of how it might be used to analyse a menu.