UNIX Shells by Example, Third Edition

 
   
•  Table of Contents
•  Examples
UNIX® Shells by Example, Third Edition
By Ellie Quigley
   
Publisher : Prentice Hall PTR
Pub Date : October 01, 2001
ISBN : 0-13-066538-X
Pages : 1040

Five UNIX shells, three essential utilities, one indispensable resource!

  • Learn UNIX shell programming the easy way, using hands-on examples

  • Covers all five leading UNIX shells-C, Bourne, Korn, bash, and tcsh

  • By best-selling author Ellie Quigley, Silicon Valley's top UNIX instructor

The best-selling UNIX Shells by Example continues to be the only book you need to learn UNIX shell programming. UNIX Shells by Example, Third Edition adds thorough coverage of the new bash and tcsh shells to the full explanations in Quigley's famous treatment of the C, Bourne, and Korn shells and the awk, sed, and grep utilities, making this the most complete UNIX shell programming book available anywhere. Using proven techniques drawn from her acclaimed Silicon Valley UNIX classes, Quigley transforms you into an expert-level shell programmer. You'll learn what the shells are, what they do, and how to program them, as well as how and when to use awk, sed, and grep. Code examples, completely revised and classroom-tested for this edition, explain concepts first-hand and can serve as the basis for your own projects.

Explains the C, Bourne, Korn, bash, and tcsh shells in one cohesive way-you'll understand which shell to use and why Details the essential awk, sed, and grep programming utilities Offers proven teaching methods from a top UNIX shell instructor Provides source code and data files for all examples on the CD-ROM, so you can experiment with them on your own system UNIX system administrators, application developers, and power users will turn to this book again and again, both as a vital classroom learning tool and as a favorite reference manual.

777

Copyright
    Preface
      ACKNOWLEDGMENTS
   
    Chapter 1.  Introduction to UNIX Shells
      Section 1.1.  Definition and Function
      Section 1.2.  System Startup and the Login Shell
      Section 1.3.  Processes and the Shell
      Section 1.4.  The Environment and Inheritance
      Section 1.5.  Executing Commands from Scripts
   
    Chapter 2.  The UNIX Toolbox
      Section 2.1.  Regular Expressions
      Section 2.2.  Combining Regular Expression Metacharacters
   
    Chapter 3.  The grep Family
      Section 3.1.  The grep Command
      Section 3.2.  grep Examples with Regular Expressions
      Section 3.3.  grep with Pipes
      Section 3.4.  grep with Options
      Section 3.5.  egrep (Extended grep)
      Section 3.6.  Fixed grep or Fast grep
      UNIX TOOLS LAB EXERCISE
   
    Chapter 4.  sed, the Streamlined Editor
      Section 4.1.  What Is sed?
      Section 4.2.  How Does sed Work?
      Section 4.3.  Addressing
      Section 4.4.  Commands and Options
      Section 4.5.  Error Messages and Exit Status
      Section 4.6.  sed Examples
      Section 4.7.  sed Scripting
      UNIX TOOLS LAB EXERCISE
   
    Chapter 5.  The awk Utility: awk as a UNIX Tool
      Section 5.1.  What Is awk?
      Section 5.2.  awk's Format
      Section 5.3.  Formatting Output
      Section 5.4.  awk Commands from Within a File
      Section 5.5.  Records and Fields
      Section 5.6.  Patterns and Actions
      Section 5.7.  Regular Expressions
      Section 5.8.  awk Commands in a Script File
      Section 5.9.  Review
      UNIX TOOLS LAB EXERCISE
   
    Chapter 6.  The awk Utility: awk Programming Constructs
      Section 6.1.  Comparison Expressions
      Section 6.2.  Review
      UNIX TOOLS LAB EXERCISE
   
    Chapter 7.  The awk Utility: awk Programming
      Section 7.1.  Variables
      Section 7.2.  Redirection and Pipes
      Section 7.3.  Pipes
      Section 7.4.  Closing Files and Pipes
      Section 7.5.  Review
      UNIX TOOLS LAB EXERCISE
      Section 7.6.  Conditional Statements
      Section 7.7.  Loops
      Section 7.8.  Program Control Statements
      Section 7.9.  Arrays
      Section 7.10.  awk Built-In Functions
      Section 7.11.  Built-In Arithmetic Functions
      Section 7.12.  User-Defined Functions (nawk)
      Section 7.13.  Review
      UNIX TOOLS LAB EXERCISE
      Section 7.14.  Odds and Ends
      Section 7.15.  Review
      UNIX TOOLS LAB EXERCISE
   
    Chapter 8.  The Interactive Bourne Shell
      Section 8.1.  Startup
      Section 8.2.  Programming with the Bourne Shell
      BOURNE SHELL LAB EXERCISES
   
    Chapter 9.  The C Shell
      Section 9.1.  The Interactive C Shell
      Section 9.2.  Programming with the C Shell
      C SHELL LAB EXERCISES
   
    Chapter 10.  The Korn Shell
      Section 10.1.  Interactive Korn Shell
      Section 10.2.  Programming with the Korn Shell
      KORN SHELL LAB EXERCISES
   
    Chapter 11.  The Interactive bash Shell
      Section 11.1.  Introduction
      Section 11.2.  Command Line Shortcuts
      Section 11.3.  Variables
      BASH SHELL LAB EXERCISES
   
    Chapter 12.  Programming with the bash Shell
      Section 12.1.  Introduction
      Section 12.2.  Reading User Input
      Section 12.3.  Arithmetic
      Section 12.4.  Positional Parameters and Command Line Arguments
      Section 12.5.  Conditional Constructs and Flow Control
      Section 12.6.  Looping Commands
      Section 12.7.  Functions
      Section 12.8.  Trapping Signals
      Section 12.9.  Debugging
      Section 12.10.  Processing Command Line Options with getopts
      Section 12.11.  The eval Command and Parsing the Command Line
      Section 12.12.  bash Options
      Section 12.13.  Shell Built-In Commands
      BASH SHELL LAB EXERCISES
   
    Chapter 13.  The Interactive TC Shell
      Section 13.1.  Introduction
      Section 13.2.  The TC Shell Environment
      Section 13.3.  Command Line Shortcuts
      Section 13.4.  Job Control
      Section 13.5.  Metacharacters
      Section 13.6.  Redirection and Pipes
      Section 13.7.  Variables
      Section 13.8.  Arrays
      Section 13.9.  Special Variables and Modifiers
      Section 13.10.  Command Substitution
      Section 13.11.  Quoting
      Section 13.12.  Built-In Commands
      TC SHELL LAB EXERCISES
   
    Appendix A.  Useful UNIX Utilities for Shell Programmers
      at—at, batch—execute commands at a later time
      awk—pattern scanning and processing language
      banner—make posters
      basename—with a directory name delivers portions of the pathname
      bc—processes precision arithmetic
      bdiff—compares two big files
      cal—displays a calendar
      cat—concatenates and displays files
      chmod—change the permissions mode of a file
      chown—changes owner of file
      clear—clears the terminal screen
      cmp—compares two files
      compress—compress, uncompress, zcat compress, uncompress files, or display expanded files
      cp—copies files
      cpio—copy file archives in and out
      cron—the clock daemon
      crypt—encodes or decodes a file
      cut—removes selected fields or characters from each line of a file
      date—displays the date and time or sets the date
      diff—compares two files for differences diff [–bitw] [–c | –Cn
      du—summarizes disk usage
      echo—echoes arguments
      egrep—searches a file for a pattern using full regular expressions
      expr—evaluates arguments as an expression
      fgrep—search a file for a character string
      file—determines the type of a file by looking at its contents
      find—finds files
      finger—displays information about local and remote users
      fmt—simple text formatters
      fold—folds long lines
      ftp—file transfer program
      getopt(s)—parses command line options
      grep—searches a file for a pattern
      groups—prints group membership of user
      id—prints the username, user ID, group name and group ID
      jsh—the standard, job control shell
      line—reads one line
      logname—gets the name of the user running the process
      lp—sends output to a printer (AT&T)
      lpr—sends output to a printer (UCB)
      lpstat—print information about the status of the LP print service (AT&T)
      lpq—print information about the status of the printer (UCB)
      ls—lists contents of directory
      mail—mail, rmail—read mail or send mail to users
      mailx—interactive message processing system
      make—maintains, updates, and regenerates groups of related programs and files
      mesg—permits or denies messages resulting from the write command
      mkdir—creates a directory
      more—browse or page through a text file
      mv—move or rename files
      nawk—pattern scanning and processing language
      newgrp—log in to a new group
      news—prints news items
      nice—runs a command at low priority
      nohup—makes commands immune to hangups and quits
      od—octal dump
      pack—pack, pcat, unpack—compresses and expands files
      passwd—changes the login password and password attributes
      paste—merges same lines of several files or subsequent lines of one file
      pcat—(see pack)
      pg—displays files a page at a time
      pr—prints files
      ps—reports process status
      pwd—displays the present working directory name
      rcp—remote file copy
      rlogin—remote login
      rm—removes files from directories
      rmdir—removes a directory
      rsh—starts a remote shell
      ruptime—shows the host status of local machines
      rwho—who is logged in on local machines
      script—creates a typescript of a terminal session
      sed—streamlined editor
      size—prints section sizes in bytes of object files
      sleep—suspends execution for some number of seconds
      sort—sort and/or merge files
      spell—finds spelling errors
      split—splits a file into pieces
      strings—finds any printable strings in an object or binary file
      stty—sets the options for a terminal
      su—become superuser or another user
      sum—calculates a checksum for a file
      sync—updates the superblock and sends changed blocks to disk
      tabs—set tab stops on a terminal
      tail—displays the tail end of a file.
      talk—allows you to talk to another user
      tar—stores and retrieves files from an archive file, normally a tape device
      tee—replicates the standard output
      telnet—communicates with a remote host
      test—evaluates an expression
      time—displays a summary of time used by this shell and its children
      timex—times a command; reports process data and system activity
      touch—updates access time and/or modification time of a file
      tput—initializes a terminal or queries the terminfo database
      tr—translates characters
      true—provide successful exit status
      tsort —topological sort
      tty—gets the name of the terminal
      umask—sets file-creation mode mask for permissions
      uname—prints name of current machine
      uncompress—restores files to their original state after they have been compressed using the compress command
      uniq—reports on duplicate lines in a file
      units—converts quantities expressed in standard scales to other scales
      unpack—expands files created by pack
      uucp—copy files to another system, UNIX-to-UNIX system copy
      uuencode—uuencode, uudecode—encode a binary file into ASCII text in order to send it through e-mail, or convert it back into its original form
      wc—counts lines, words, and characters
      what—extracts SCCS version information from a file by printing information found after the @(#) pattern
      which—locates a command and displays its pathname or alias (UCB)
      whereis—locates the binary, source, and manual page files for a command (UCB)
      who—displays who is logged on the system
      write—writes a message to another user
      xargs—constructs an argument list(s) and executes a command
      zcat—uncompress a compressed file to standard output. Same as uncompress –c
   
    Appendix B.  Comparison of the Shells
      Section B.1.  The Shells Compared
      Section B.2.  tcsh versus csh
      Section B.3.  bash versus sh
   
    Appendix C.  Steps for Using Quoting Correctly
      Section C.1.  Backslash
      Section C.2.  Single Quotes
      Section C.3.  Double Quotes
      Section C.4.  Combining Quotes
      Section C.5.  Setting the Shell Variable

Copyright

Library of Congress Cataloging-in-Publication Data

Quigley, Ellie.

UNIX shells by example / Ellie Quigley. -- 3rd ed.

p. cm.

ISBN 0-13-066538-X

1. UNIX (Computer file) 2. UNIX Shells. I. Title.

QA76.76.O63 Q54 2001

005.4'32--dc21

2001050075

© 2002 by Prentice Hall P T R

Prentice-Hall, Inc.

Upper Saddle River, NJ 07458

Prentice Hall books are widely used by corporations and government agencies for training, marketing, and resale.

The publisher offers discounts on this book when ordered in bulk quantities. For more information, contact: Corporate Sales Department, Phone: 800-382-3419; Fax: 201-236-7141; E-mail: corpsales@prenhall.com; or write: Prentice Hall PTR, Corp. Sales Dept., One Lake Street, Upper Saddle River, NJ 07458.

All products or services mentioned in this book are the trademarks or service marks of their respective companies or organizations.

All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher.

Printed in the United States of America

10 9 8 7 6 5 4 3 2 1

Credits

Production editor/compositor:

Vanessa Moore

Acquisitions editor:

Mark Taub

Marketing manager:

Bryan Gambrel

Manufacturing manager:

Maura Zaldivar

Editorial assistant:

Sarah Hand

Cover design director:

Jerry Votta

Cover designer:

Anthony Gemmellaro

Project coordinator:

Anne R. Garcia

Pearson Education Ltd.

Pearson Education Australia PTY, Limited

Pearson Education Singapore, Pte. Ltd

Pearson Education North Asia Ltd.

Pearson Education Canada, Ltd.

Pearson Educación de Mexico, S.A. de C.V.

Pearson Education — Japan

Pearson Education Malaysia, Pte. Ltd.

Pearson Education, Upper Saddle River, New Jersey

Dedication

This book is dedicated to my papa, Archibald MacNichol Main, Jr., the best father in the world.

Preface

Playing the "shell" game is a lot of fun. This book was written to make your learning experience both fun and profitable. Since the first edition was published, I have heard from many of you who have been helped by my book to realize that shell programming doesn't need to be difficult at all! Learning by example makes it easy and fun. In fact, due to such positive feedback, I have been asked by Prentice Hall to produce this new, updated version to include two additional and popular shells, the Bash and TC shells. Although often associated with Linux systems, the Bash and TC shells are freely available to anyone using UNIX as well. In fact, today many UNIX users prefer these shells to the traditional UNIX shells because they offer an enhanced and flexible interactive environment, as well as improved programming capabilities.

Writing UNIX Shells by Example is the culmination of 19 years of teaching and developing classes for the various shells and those UNIX utilities most heavily used by shell programmers. The course notes I developed for teaching classes have been used by the University of California Santa Cruz and University of California Davis UNIX programs, Sun Microsystems Education, Apple Computer, DeAnza College, and numerous vendors throughout the world. Depending on the requirements of my client, I normally teach one shell at a time rather than all of them. To accommodate the needs of so many clients, I developed separate materials for each of the respective UNIX shells and tools.

Whether I am teaching "Grep, Sed, and Awk," "Bourne Shell for the System Administrator," or "The Interactive Korn Shell," one student always asks, "What book can I get that covers all the shells and the important utilities such as grep, sed, and awk? Should I get the awk book, or should I get a book on grep and sed? Is there one book that really covers it all? I don't want to buy three or four books in order to become a shell programmer."

In response, I can recommend a number of excellent books covering these topics separately, and some UNIX books that attempt to do it all, but the students want one book with everything and not just a quick survey. They want the UNIX tools, regular expressions, all three shells, quoting rules, a comparison of the shells, exercises, and so forth, all in one book. This is that book. As I wrote it, I thought about how I teach the classes and organized the chapters in the same format. In the shell programming classes, the first topic is always an introduction to what the shell is and how it works. Then we talk about the UNIX utilities such as grep, sed, and awk, the most important tools in the shell programmer's toolbox. When learning about the shell, it is presented first as an interactive program where everything can be accomplished at the command line, and then as a programming language where the programming constructs are described and demonstrated in shell scripts. (Since the C and TC shells are almost identical as programming languages, there are separate chapters describing interactive use, but only one chapter discussing programming constructs.) When shell programming classes are over, whether they last two days or a week or even a semester, the students are proficient and excited about writing scripts. They have learned how to play the shell game. This book will teach how to play the same game whether you take a class or just play by yourself.

Having always found that simple examples are easier for quick comprehension, each concept is captured in a small example followed by the output and an explanation of each line of the program. This method has proven to be very popular with those who learned Perl programming from my first book, Perl by Example, and UNIX Shells by Example now has been well-received for those who needed to write, read, and maintain shell programs.

The five shells are presented in parallel so that if, for example, you want to know how redirection is performed in one shell, there is a parallel discussion of that topic in each of the other shell chapters. For a quick comparison chart, see Appendix B of this book.

It is a nuisance to have to go to another book or the UNIX man pages when all you want is enough information about a particular command to jog your memory on how the command works. To save you time, Appendix A contains a list of useful commands, their syntax and definitions. Examples and explanations are provided for the more robust and often-used commands.

The comparison chart in Appendix B will help you keep the different shells straight, especially when you port scripts from one shell to another, and serve as a quick syntax check when all you need is a reminder of how the construct works.

One of the biggest hurdles for shell programmers is using quotes properly. The section on quoting rules in Appendix C presents a step-by-step process for successful quoting in some of the most complex command lines. This procedure has dramatically reduced the amount of time programmers waste when debugging scripts with futile attempts at matching quotes properly.

I think you'll find this book a valuable tutorial and reference. The objective is to explain through example and keep things simple so that you have fun learning and save time. Since the book replicates what I say in my classes, I am confident that you will be a productive shell programmer in a short amount of time. Everything you need is right here at your fingertips. Playing the shell game is fun. You'll see!

Ellie Quigley (ellieq@ellieq.com)

ACKNOWLEDGMENTS

I would like to thank and acknowledge the following people, without whose help this book would not have been published:

Mark Taub, my acquisitions editor, and Vanessa Moore, my production editor at Prentice Hall; Beth Gerra, Roberta Harvey, and Gary Wilson for reviewing the original material; Steve Hansen for hardware and software support. Finally, I would like to thank all my students at UC Santa Cruz, UC Davis, and Sun Microsystems for their feedback.