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 Table of available anywhere Using proven techniques drawn • Contents from her acclaimed Silicon Valley UNIX classes, • Examples 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 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 Number of echo $%var (tcsh only) characters Special Variables: PID of the $$ process Exit status $status, $? Last background job Arrays: Assigning arrays N/A $$ $! (tcsh only) $? $! set x = ( a b c ) N/A Accessing echo $x[1] $x[2] array elements All elements echo $x or $x[*] N/A No of echo $#x elements Command Substitution: Assigning set d = `date` output of command to variable Accessing values echo $d N/A echo $d[1], $d[2], echo $#d N/A d=`date` echo $d Command Line Arguments (Positional Parameters): Accessing $1, $2 $9 $argv[1], $argv[2] or $1, $2 Setting positional parameters No of command line arguments N/A set a b c set `date` echo $1 $2 $# $#argv $# (tcsh) No of $%1, $%2, (tcsh) characters in $arg[number] Metacharacters for Filename Expansion: Matches for: Single ? character Zero or more * characters One [abc] character from a set One [a–c] character from a range of characters in a set One character not N/A (csh) N/A ? * [abc] [a–c] [!abc] in the set [^abc] (tcsh) ? matches zero or one occurrences of any pattern in the parentheses The vertical bar represents an or condition; e.g., either 2 or 9 Matches abc21, abc91, or abc1 Filenames ^pattern (tcsh) not matching a pattern I/O Redirection and Pipes: Command cmd > file output redirected to a file Command cmd >> file output redirected and appended to a file Command cmd < file input redirected from a file cmd > file cmd >> file cmd < file Command (cmd > /dev/tty)>&errors cmd 2>errors errors redirected to a file Output and cmd >& file cmd > file 2>&1 errors redirected to a file Assign output and ignore noclobber here document cmd >| file N/A cmd