Expert Shell Scripting.pdf

(1439 KB) Pobierz
660567747 UNPDF
660567747.001.png
Expert Shell Scripting
■■■
Ron Peters
660567747.002.png
Expert Shell Scripting
Copyright © 2009 by Ron Peters
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-1841-8
ISBN-13 (electronic): 978-1-4302-1842-5
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: Brian Culp
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary
Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben
Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Candace English
Associate Production Director: Kari Brooks-Copony
Production Editor: Liz Berry
Compositor: Pat Christenson
Proofreader: Lisa Hamilton
Indexer: Julie Grady
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
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 orders-ny@springer-sbm.com , or
visit http://www.springeronline.com .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://
www.apress.com .
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 Sales–eBook 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 . You will need to answer
questions pertaining to this book in order to successfully download the code.
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1
■ ■ ■
Basic Scripting Techniques
CHAPTER 1 Shell Script Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 Standard Function Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
CHAPTER 3 Date and Time Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
CHAPTER 4 Comparisons and Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
CHAPTER 5 Accepting Command-Line Options, Switches,
and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
CHAPTER 6 Testing Variables and Assigning Defaults . . . . . . . . . . . . . . . . . . . . . . 37
CHAPTER 7 Indirect Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
CHAPTER 8 Shell Process Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
CHAPTER 9 Data Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
CHAPTER 10 Piping Input to read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
PART 2
■ ■ ■
System Interaction and Advanced
Techniques
CHAPTER 11 Math from the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
CHAPTER 12 cron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
CHAPTER 13 Self-Linked Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
CHAPTER 14 Throttling Parallel Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
CHAPTER 15 Command-Line Editing and History . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
CHAPTER 16 Scripting from the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
iv
CHAPTER 17 Automating User Input with expect . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
660567747.003.png
CHAPTER 18 User Input Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
CHAPTER 19 Instant Keyboard Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
CHAPTER 20 Directory Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
CHAPTER 21 A Brief Tour of the X Display Environment . . . . . . . . . . . . . . . . . . . . 131
CHAPTER 22 X Navigation Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
CHAPTER 23 Command-Line E-mail Attachments . . . . . . . . . . . . . . . . . . . . . . . . . . 151
CHAPTER 24 Text-Processing One-Liners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
CHAPTER 25 Editing Files in Place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
CHAPTER 26 Evaluating Variables in a Flat File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
CHAPTER 27 Read Piped Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
CHAPTER 28 Free-Format Output Using cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
CHAPTER 29 Automating Interactive Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
PART 3
■ ■ ■
Useful Scripts
CHAPTER 30 Automating E-Mail with procmail . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
CHAPTER 31 Process-Management Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
CHAPTER 32 Managing File Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
CHAPTER 33 Processes Running from inittab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
CHAPTER 34 Automatic RCS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
CHAPTER 35 Colorful /proc Reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
CHAPTER 36 Password-Aging Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
CHAPTER 37 A Pseudo–shadow File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
CHAPTER 38 Linux Gold-System Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
CHAPTER 39 System Snapshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
CHAPTER 40 Removing Large Files and Log Rolling . . . . . . . . . . . . . . . . . . . . . . . . 261
CHAPTER 41 Core Finder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
CHAPTER 42 Network Adapter Failover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
APPENDIX A Test Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
APPENDIX B Special Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
APPENDIX C Other Shell-Scripting Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Zgłoś jeśli naruszono regulamin