Pro Bash Programming: Scripting the GNU/Linux Shell
Copyright 2009 by Chris F.A. Johnson
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-1997-2
ISBN-13 (electronic): 978-1-4302-1998-9
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Lead Editor: Frank Pohlmann
Technical Reviewer: Ed Schaefer
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Project Manager: Kylie Johnston
Copy Editor: Kim Wimpsett
Compositor: ContentWorks, Inc.
Indexer: Julie Grady
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail .
For information on translations, please contact Apress directly at 233 Spring Street, New York, NY 10013. E-mail .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk SaleseBook Licensing web page at http://www.apress.com/info/bulksales.
The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
Contents at a Glance
Contents
About the Author
After almost 20 years in magazine and newspaper publishing, variously as writer, editor, graphic designer, and production manager, Chris F.A. Johnson now earns his living composing cryptic crossword puzzles, teaching chess, designing and coding web sites, and programming...and writing books about shell scripting. His first book, Shell Scripting Recipes: A Problem-Solution Approach, was published by Apress in 2005.
Introduced to Unix in 1990, Chris learned shell scripting because there was no C compiler on the system. His first major project was a menu-driven, user-extensible database system with a report generator. Constantly writing scripts for any and all purposes, his recent shell projects have included utilities for manipulating crossword puzzles and preparing chess resources for his students.
About the Technical Reviewer
Ed Schaefer is an ex-paratrooper, an ex-military intelligence officer, an ex-oil field service engineer, and a past contributing editor and columnist for Sys Admin. He's not a total has-been. He earned a BSEE from South Dakota School of Mines and an MBA from USD.
Currently, he fixes microstrategy and teradata problemswith an occasional foray into Linuxfor a Fortune 50 company.
Introduction
Although most users think of the shell as an interactive command interpreter, it is really a programming language in which each statement runs a command. Because it must satisfy both the interactive and programming aspects of command execution, it is a strange language, shaped as much by history as by design.
Brian Kernighan and Rob Pike, The UNIX Programming Environment, Prentice-Hall, 1984
The shell is a programming language. Don't let anyone tell you otherwise. The shell is not just glue that sticks bits together. The shell is a lot more than a tool that runs other tools. The shell is a complete programming language!
When a Linux user asked me about membership databases, I asked him what he really needed. He wanted to store names and addresses for a couple of hundred members and print mailing labels for each of them. I recommended using a text editor to store the information in a text file, and I provided a shell script to create the labels in PostScript. (The script, ps-labels
, appeared in my first book, Shell Scripting Recipes: A Problem-Solution Approach.)
When the SWEN worm was dumping hundreds of megabytes of junk into my mailbox every few minutes, I wrote a shell script to filter them out on the mail server and download the remaining mail to my home computer. That script has been doing its job for several years.
I used to tell people that I did most of my programming in the shell but switched to C for anything that needed the extra speed. It has been several years since I have needed to use C, so I no longer mention it. I do everything in the shell.
A shell script is as much a program as anything written in C, Python, or any other language. Just because shell scripts are easier to write doesn't mean they should take a backseat to compiled programs or other scripting languages. I use the terms script and program interchangeably when referring to tasks written in the shell.
Why the Shell?
Some Linux users do all of their work in a GUI environment and never see a command line. Most, however, use the shell at least occasionally and know something about Unix commands. It's not a big step from there to saving oft-repeated commands in a script file. When they need to extend the capabilities of their system, the shell is the natural way to go.
The shell also has important advantages over other programming languages:
- It interfaces simply and seamlessly with the hundreds of Unix utilities.
- It automatically expands wildcards into a list of file names.
- Lists contained in a variable are automatically split into their constituent parts.
Just the Shell, Ma'am, Just the Shell
While most shell programs do call external utilities, a lot of programming can be done entirely in the shell. Many scripts call just one or two utilities for information that is used later in the script. Some scripts are little more than wrappers for other commands such as awk, grep
, or sed
.
This book is about programming in the shell itself. There's a sprinkling of the second type, where the script gets information (such as the current date and time) and then processes it. The third type gets barely more than a cursory nod.
A Brief History of sh
The Bourne shell was the first Unix shell in general use. It was much more limited than today's shells, so it was primarily a tool to run other tools. It had variables, loops, and conditional execution, but the real work was done almost entirely by external utilities.
The C shell, csh
, added command history, arithmetic, and other features that made it popular as a command-line interpreter, but it was not suitable for more than trivial scripts.
Next page