Preface
Every modern operating system has at least one shell and some have many. Some shells are command-line oriented, such as the shell discussed in this book. Others are graphical, like Windows Explorer or the Macintosh Finder. Some users will interact with the shell only long enough to launch their favorite application, and then never emerge from that until they log off. But most users spend a significant amount of time using the shell. The more you know about your shell, the faster and more productive you can be.
Whether you are a system administrator, a programmer, or an end user, there are certainly occasions where a simple (or perhaps not so simple) shell script can save you time and effort, or facilitate consistency and repeatability for some important task. Even using an alias to change or shorten the name of a command you use often can have a significant effect. We'll cover this and much more.
As with any general programming language, there is more than one way to do a given task. In some cases, there is only one best way, but in most cases there are at least two or three equally effective and efficient ways to write a solution. Which way you choose depends on your personal style, creativity, and familiarity with different commands and techniques. This is as true for us as authors as it is for you as the reader. In most cases we will choose a single method and implement it. In a few cases we may choose a particular method and explain why we think it's the best. We may also occasionally show more than one equivalent solution so you can choose the one that best fits your needs and environment.
There is also sometimes a choice between a clever way to write some code, and a readable way. We will choose the readable way every time because experience has taught us that no matter how transparent you think your clever code is now, six or eighteen months and 10 projects from now, you will be scratching your head asking your-self what you were thinking. Trust us, write clear code, and document ityou'll thank yourself (and us) later.
Who Should Read This Book
This book is for anyone who uses a Unix or Linux system, as well as system administrators who may use several systems on any given day. With it, you will be able to create scripts that allow you to accomplish more, in less time, more easily, consistently, and repeatably than ever before.
Anyone? Yes. New users will appreciate the sections on automating repetitive tasks, making simple substitutions, and customizing their environment to be more friendly and perhaps behave in more familiar ways. Power users and administrators will find new and different solutions to common tasks and challenges. Advanced users will have a collection of techniques they can use at a moment's notice to put out the latest fire, without having to remember every little detail of syntax.
Ideal readers include:
New Unix or Linux users who don't know much about the shell, but want to do more than point and click
Experienced Unix or Linux users and system administrators looking for quick answers to shell scripting questions
Programmers who work in a Unix or Linux (or even Windows) environment and want to be more productive
New Unix or Linux sysadmins, or those coming from a Windows environment who need to come up to speed quickly
Experienced Windows users and sysadmins who want a more powerful scripting environment
This book will only briefly cover basic and intermediate shell scriptingsee Learning the bash Shell by Cameron Newham (O'Reilly) and Classic Shell Scripting by Nelson H.F. Beebe and Arnold Robbins (O'Reilly) for more in-depth coverage. Instead, our goal is to provide solutions to common problems, with a strong focus on the "how to" rather than the theory. We hope this book will save you time when figuring out solutions or trying to remember syntax. In fact, that's why we wrote this book. It's one we wanted to read through to get ideas, then refer to practical working examples when needed. That way we don't have to remember the subtle differences between the shell, Perl, C, and so forth.
This book assumes you have access to a Unix or Linux system (or see , "Testing Scripts in VM ware") and are familiar with logging in, typing basic commands, and using a text editor. You do not have to be root to use the vast majority of the recipes, though there are a few, particularly dealing with installing bash, where root access will be needed.
About This Book
This book covers bash , the GNU Bourne Again Shell, which is a member of the Bourne family of shells that includes the original Bourne shell sh , the Korn shell ksh , and the Public Domain Korn Shell pdksh . While these and other shells such as dash , and zsh are not specifically covered, odds are that most of the scripts will work pretty well with them.
You should be able to read this book cover to cover, and also just pick it up and read anything that catches your eye. But perhaps most importantly, we hope that when you have a question about how to do something or you need a hint, you will be able to easily find the right answeror something close enoughand save time and effort.
A great part of the Unix philosophy is to build simple tools that do one thing well, then combine them as needed. This combination of tools is often accomplished via a shell script because these commands, called pipelines, can be long or difficult to remember and type. Where appropriate, we'll cover the use of many of these tools in the context of the shell script as the glue that holds the pieces together to achieve the goal.
This book was written using OpenOffice.org Writer running on whatever Linux or Windows machine happened to be handy, and kept in Subversion (see , "Processing Files with No Line Breaks."
GNU Software
bash , and many of the tools we discuss in this book, are part of the GNU Project (http://www.gnu.org/). GNU (pronounced guh-noo, like canoe) is a recursive acronym for "GNU's Not Unix" and the project dates back to 1984. Its goal is to develop a free (as in freedom) Unix-like operating system.
Without getting into too much detail, what is commonly referred to as Linux is, in fact, a kernel with various supporting software as a core. The GNU tools are wrapped around it and it has a vast array of other software possibly included, depending on your distribution. However, the Linux kernel itself is not GNU software.
The GNU project argues that Linux should in fact be called "GNU/Linux" and they have a good point, so some distributions, notably Debian, do this. Therefore GNU's goal has arguably been achieved, though the result is not exclusively GNU.
The GNU project has contributed a vast amount of superior software, notably including bash , but there are GNU versions of practically every tool we discuss in this book. And while the GNU tools are more rich in terms of features and (usually) friendliness, they are also sometimes a little different. We discuss this in , "Developing Portable Shell Scripts," though the commercial Unix vendors in the 1980s and 1990s are also largely to blame for these differences.
Enough (several books this size worth) has already been said about all of these aspects of GNU, Unix, and Linux, but we felt that this brief note was appropriate. See http://www.gnu.org for much more on the topic.