Stephen G. Kochan Patrick Wood Unix Shell Programming Third Edition 800 East 96th Street, Indianapolis, Indiana 46240 Unix Shell Programming, Third Edition Acquisitions Editor Katie Purdum Copyright © 2003 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a Development Editor retrieval system, or transmitted by any means, electronic, mechanical, Scott Meyers photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the Managing Editor use of the information contained herein. Although every precaution Charlotte Clapp has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any Copy Editor liability assumed for damages resulting from the use of the information Geneil Breeze contained herein. Indexer International Standard Book Number: 0-672-32490-3 Erika Millen Library of Congress Catalog Card Number: 2002115932 Proofreader Printed in the United States of America Jessica McCarty First Printing: March 2003 Technical Editor 06 05 4 Michael Watson Interior Designer Trademarks Gary Adair All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing Cover Designer cannot attest to the accuracy of this information. Use of a term in this Gary Adair book should not be regarded as affecting the validity of any trademark or service mark. Page Layout Susan Geiselman Warning and Disclaimer Every effort has been made to make this book as complete and as accu- rate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected] Contents at a Glance 1 Introduction.................................................................................................1 2 A Quick Review of the Basics.......................................................................5 3 What Is the Shell?.......................................................................................41 4 Tools of the Trade.......................................................................................53 5 And Away We Go........................................................................................99 6 Can I Quote You on That?.......................................................................115 7 Passing Arguments....................................................................................133 8 Decisions, Decisions.................................................................................145 9 ‘Round and ‘Round She Goes...................................................................183 10 Reading and Printing Data.......................................................................209 11 Your Environment....................................................................................235 12 More on Parameters..................................................................................267 13 Loose Ends................................................................................................287 14 Rolo Revisited...........................................................................................307 15 Interactive and Nonstandard Shell Features............................................325 A Shell Summary..........................................................................................363 B For More Information...............................................................................403 Index.........................................................................................................407 Table of Contents 1 Introduction 1 2 AQuick Review of the Basics 5 Some Basic Commands ...............................................................................5 Displaying the Date and Time: The date Command .........................................................................5 Finding Out Who’s Logged In: The who Command ........................5 Echoing Characters: The echo Command .........................................6 Working with Files .......................................................................................6 Listing Files: The ls Command ..........................................................7 Displaying the Contents of a File: The cat Command .....................7 Counting the Number of Words in a File: The wc Command .........8 Command Options ............................................................................8 Making a Copy of a File: The cp Command .....................................9 Renaming a File: The mv Command .................................................9 Removing a File: The rm Command ...............................................10 Working with Directories ..........................................................................10 The Home Directory and Pathnames ..............................................11 Displaying Your Working Directory: The pwd Command ..............12 Changing Directories: The cd Command ........................................13 More on the ls Command ...............................................................16 Creating a Directory: The mkdir Command ...................................18 Copying a File from One Directory to Another ..............................18 Moving Files Between Directories ....................................................19 Linking Files: The ln Command ......................................................20 Removing a Directory: The rmdir Command .................................24 Filename Substitution ................................................................................24 The Asterisk ......................................................................................24 Matching Single Characters .............................................................26 Standard Input/Output and I/O Redirection ............................................28 Standard Input and Standard Output ..............................................28 Output Redirection ..........................................................................30 Input Redirection .............................................................................32 Pipes ...........................................................................................................33 Filters ................................................................................................35 Standard Error ............................................................................................35 More on Commands .................................................................................36 Typing More Than One Command on a Line ................................36 Sending a Command to the Background ........................................36 The ps Command .............................................................................37 Command Summary .................................................................................38 Exercises .....................................................................................................38 3 What Is the Shell? 41 The Kernel and the Utilities ......................................................................41 The Login Shell ..........................................................................................42 Typing Commands to the Shell ................................................................45 The Shell’s Responsibilities ........................................................................46 Program Execution ...........................................................................46 Variable and Filename Substitution .................................................48 I/O Redirection .................................................................................49 Pipeline Hookup ...............................................................................51 Environment Control .......................................................................51 Interpreted Programming Language ................................................52 4 Tools of the Trade 53 Regular Expressions ...................................................................................53 Matching Any Character: The Period (.) .........................................54 Matching the Beginning of the Line: The Caret (^) ........................55 Matching the End of the Line: The Dollar Sign ($) ........................55 Matching a Choice of Characters: The [...] Construct ....................57 Matching Zero or More Characters: The Asterisk (*) .......................59 Matching a Precise Number of Characters: \{...\} ...........................62 Saving Matched Characters: \(...\) ..................................................64 cut ..............................................................................................................67 The -d and -f Options ......................................................................69 paste ...........................................................................................................72 The -d Option ...................................................................................73 The -s Option ...................................................................................74 sed ..............................................................................................................74 The -n Option ..................................................................................76 Deleting Lines ...................................................................................77 vi UNIX SHELL PROGRAMMING tr .................................................................................................................78 The -s Option ...................................................................................81 The –d Option ..................................................................................82 grep ............................................................................................................83 Regular Expressions and grep ..........................................................86 The -v Option ...................................................................................87 The -l Option ....................................................................................88 The -n Option ..................................................................................89 sort .............................................................................................................89 The -u Option ...................................................................................90 The -r Option ...................................................................................90 The -o Option ...................................................................................90 The -n Option ..................................................................................91 Skipping Fields .................................................................................92 The -t Option ...................................................................................92 Other Options ..................................................................................93 uniq ............................................................................................................94 The -d Option ...................................................................................95 Other Options ..................................................................................96 Exercises .....................................................................................................97 5 And Away We Go 99 Command Files ..........................................................................................99 Comments ......................................................................................102 Variables ...................................................................................................103 Displaying the Values of Variables .................................................104 The Null Value ................................................................................107 Filename Substitution and Variables .............................................108 The ${variable} Construct ..............................................................110 Built-in Integer Arithmetic ......................................................................110 Exercises ...................................................................................................112 6 Can I Quote You on That? 115 The Single Quote .....................................................................................115 The Double Quote ...................................................................................119 The Backslash ..........................................................................................121 Using the Backslash for Continuing Lines ....................................122 The Backslash Inside Double Quotes .............................................123 Contents vii Command Substitution ...........................................................................124 The Back Quote ..............................................................................124 The $(...) Construct ........................................................................125 The expr Command .......................................................................129 Exercises ...................................................................................................131 7 Passing Arguments 133 The $# Variable ..............................................................................134 The $* Variable ...............................................................................135 A Program to Look Up Someone in the Phone Book ....................136 A Program to Add Someone to the Phone Book ...........................138 A Program to Remove Someone from the Phone Book ................139 ${n} .................................................................................................141 The shift Command ................................................................................141 Exercises ...................................................................................................143 8 Decisions, Decisions 145 Exit Status ................................................................................................145 The $? Variable ...............................................................................146 The test Command ..................................................................................149 String Operators .............................................................................150 An Alternative Format for test .......................................................154 Integer Operators ...........................................................................155 File Operators .................................................................................157 The Logical Negation Operator ! ...................................................158 The Logical AND Operator –a ........................................................158 Parentheses .....................................................................................159 The Logical OR Operator –o ...........................................................159 The else Construct ...................................................................................160 The exit Command .................................................................................162 A Second Look at the rem Program ...............................................163 The elif Construct ....................................................................................164 Yet Another Version of rem ...........................................................167 The case Command .................................................................................169 Special Pattern Matching Characters .............................................171 The -x Option for Debugging Programs ........................................173 Back to the case ..............................................................................175 The Null Command : ..............................................................................177 The && and || Constructs ........................................................................177 Exercises ...................................................................................................180 viii UNIX SHELL PROGRAMMING 9 ’Round and ’Round She Goes 183 The for Command ...................................................................................183 The $@ Variable ..............................................................................187 The for Without the List ................................................................188 The while Command .....................................................................189 The until Command ................................................................................191 More on Loops .........................................................................................196 Breaking Out of a Loop ..................................................................196 Skipping the Remaining Commands in a Loop ............................198 Executing a Loop in the Background ............................................199 I/O Redirection on a Loop .............................................................199 Piping Data Into and Out of a Loop ..............................................200 Typing a Loop on One Line ...........................................................200 The getopts Command ............................................................................201 Exercises ...................................................................................................205 10 Reading and Printing Data 209 The read Command .................................................................................209 A Program to Copy Files ................................................................209 Special echo Escape Characters ......................................................212 An Improved Version of mycp .......................................................212 A Final Version of mycp .................................................................214 A Menu-Driven Phone Program ....................................................219 The $$ Variable and Temporary Files ............................................224 The Exit Status from read ...............................................................225 The printf Command ..............................................................................228 Exercises ...................................................................................................233 11 Your Environment 235 Local Variables .........................................................................................235 Subshells .........................................................................................236 Exported Variables ...................................................................................237 export -p .........................................................................................241 PS1 and PS2 .............................................................................................241 HOME, James ...........................................................................................242 Your PATH ................................................................................................243 Your Current Directory ............................................................................251 CDPATH ..........................................................................................252 Contents ix More on Subshells ...................................................................................253 The . Command .............................................................................254 The exec Command .......................................................................257 The (...) and { ...; } Constructs ........................................................258 Another Way to Pass Variables to a Subshell .................................262 Your .profile File ......................................................................................262 The TERM Variable ..................................................................................264 The TZ Variable ........................................................................................264 Exercises ...................................................................................................265 12 More on Parameters 267 Parameter Substitution ............................................................................267 ${parameter} ...................................................................................268 ${parameter:-value} ........................................................................268 ${parameter:=value} ........................................................................269 ${parameter:?value} ........................................................................270 ${parameter:+value} ........................................................................270 Pattern Matching Constructs .........................................................270 ${#variable} .....................................................................................273 The $0 Variable ........................................................................................273 The set Command ...................................................................................274 The -x Option .................................................................................274 set with No Arguments ..................................................................275 Using set to Reassign Positional Parameters ..................................276 The -- Option ..................................................................................277 Other Options to set ......................................................................280 The IFS Variable .......................................................................................280 The readonly Command .........................................................................283 The unset Command ...............................................................................284 Exercises ...................................................................................................284 13 Loose Ends 287 The eval Command .................................................................................287 The wait Command .................................................................................289 The $! Variable ...............................................................................290 The trap Command .................................................................................290 trap with No Arguments ................................................................292 Ignoring Signals ..............................................................................292 Resetting Traps ...............................................................................293