Bash Cookbook 2nd Edition, by Carl Albing, JP Vossen, Cameron Newham Review by Douglas Sims
Programmers learn that optimizing code for speed is slow and meticulous work but that it can pay off nicely in performance. We also learn that programs typically spend a small amount of execution time running through most of the code in a program and most of the time in a small section of code. Thus, optimizing that small section of code can give much more performance gain than tinkering with the rest.
So it goes also with the practice of programming. Whether I'm writing
something in Java or Perl or Python or even writing this book review, I
spend most of my time in either Vim or Bash, and thus becoming more familiar with these two programs is likely to help my overall ability to get things done.
There's another reason why paying a bit more attention to bash is A Very Good Thing for most programmers - or at least most programmers I know. We all grew up with, or went through an extended phase of using Windows in the '90s. Now most people have moved on to better things but we still carry with us some of the bad habits from those days. We got used to the MS-DOS
shell, which isn't good for much more than just starting other programs and when we moved to a more unixey-world we assumed that the shell there was the same. But it isn't. bash in particular is a refined environment well tailored for doing whatever a shell is supposed to do - not just starting programs, but managing the execution cycle of programs - starting, foregrounding, backgrounding, re-nicing, redirecting input and output, monitoring and signaling - and not just of creating and deleting directories
but managing data.
bash Cookbook quite sensibly doesn't restrict itself to just the internal
commands and syntax which comprise bash or writing scripts with bash. It goes far beyond that and includes recipes containing many common unix commands which are not, strictly speaking, part of bash but which are consistent with the book's focus on processing files and streams - grep, sed and awk and even a bit of perl. This book is a bit light on examples of using bash for administrative tasks like crunching logfiles, dns management, and interactive commands. For example, kill is mentioned only briefly; top not at all while there are several recipes for fg, bg, nohup, etc.
In addition to 19 chapters of recipes, this book contains several appendices of reference info, including the most comprehensive list of file test operators I've ever seen (p. 506), complete reference to printf and strftime (pp. 509-514), as well as a thorough index. For instance, have you ever gotten an error message "Argument list too long" when trying to do something like this: rm /var/spool/mqueue/* ? There's an index entry in bash Cookbook for that exact phrase. It leads to an entry (p. 343) with several options for solving the problem: either use xargs, find, or a for loop.
I read through the book very quickly when I first got it - not trying to
remember everything, just closely enough to know what sorts of problems it could help me to solve. I've been using it as a reference when I need to solve a specific problem. After finding the solution I've gone back and read more closely the specific chapters containing the solution. For instance, I wanted to call a program repeatedly, iterating through a range of dates as the argument. I might have written something in perl, but first I checked recipe 11.3, "Automating Date Ranges." Despite the title it didn't list a recipe which did exactly what I wanted, but as I read through chapter 11 I found some other ideas which did help me to solve the problem.
My biggest complaint with the bash Cookbook is one that almost all that the O'Reilly books share. After a bit of use the corners start to curl up, starting with the covers and working in towards the middle.
I really quite liked this book. I've learned enough clever techniques to
feel a bit smarter than I was before I started the book and I've also got a reference book now that will show me a better, easier, and quicker way of doing the things I do frequently. And the book has passed my ultimate test of usability - I've been carrying it around in my backpack for over a month. The corners are curled up and there's a big coffee stain on the cover. I like it.