Chapter 15. Advanced Scripting

Unix and POSIX have long promised compatibility and portability, and long struggled to deliver it. Thus, one of the biggest problems for advanced scripters is writing scripts that are portable; i.e., that can work on any machine that has bash installed. Writing scripts that run well on a wide variety of platforms is much more difficult than we wish it were. There are many variations from one system to another that can get in the way; for example, bash itself isn’t always installed in the same place, and many common Unix commands have slightly different options (or give slightly different output) depending on the operating system. In this chapter, we’ll look at several of those problems and show you how to solve them with bash.

Many of the other things that are periodically needed are not as simple as we’d like them to be, either. So, we’ll also cover solutions for additional advanced scripting tasks, such as automating processes using phases, sending email from your script, logging to syslog, using your network resources, and a few tricks for getting input and redirecting output.

Although this chapter is about advanced scripting, we’d like to stress the need for clear code, written as simply as possible and documented. Brian Kernighan, one of the first Unix developers, put it well:

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough ...

Get bash Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.