Shell Scripting Recipes.pdf

(2465 KB) Pobierz
673985214 UNPDF
Shell Scripting Recipes
A Problem-Solution Approach
CHRIS F. A. JOHNSON
673985214.002.png
Shell Scripting Recipes: A Problem-Solution Approach
Copyright © 2005 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 (pbk): 1-59059-471-1
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: Jason Gilmore
Technical Reviewer: William Park
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Assistant Publisher: Grace Wong
Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Scott Carter
Production Manager: Kari Brooks-Copony
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreader: Linda Seifert
Indexer: James Minkin
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th
Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergarten-
str. 17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders@springer-ny.com , or visit
http://www.springer-ny.com . Outside the United States: fax +49 6221 345229, e-mail orders@springer.de ,
or visit http://www.springer.de .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://www.apress.com .
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liabil-
ity 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 in the Downloads section.
673985214.003.png
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1
The POSIX Shell and Command-Line Utilities . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Playing with Files: Viewing, Manipulating,
and Editing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
CHAPTER 3
String Briefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
CHAPTER 4
What’s in a Word? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
CHAPTER 5
Scripting by Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
CHAPTER 6
Loose Names Sink Scripts: Bringing Sanity to Filenames . . . . . . . 137
CHAPTER 7
Treading a Righteous PATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
CHAPTER 8
The Dating Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
CHAPTER 9
Good Housekeeping: Monitoring and Tidying Up
File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
CHAPTER 10
POP Goes the E-Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
CHAPTER 11
PostScript: More Than an Afterthought . . . . . . . . . . . . . . . . . . . . . . . . 229
CHAPTER 12
Screenplay: The screen–funcs Library . . . . . . . . . . . . . . . . . . . . . . . . . 251
CHAPTER 13
Backing Up the Drive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
CHAPTER 14
Aging, Archiving, and Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
CHAPTER 15
Covering All Your Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
CHAPTER 16
Home on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
CHAPTER 17
Taking Care of Business . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
CHAPTER 18
Random Acts of Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
CHAPTER 19
A Smorgasbord of Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
CHAPTER 20
Script Development Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
APPENDIX
Internet Scripting Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
iii
673985214.004.png
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1 The POSIX Shell and Command-Line Utilities . . . . . . . . . . . . . . 1
Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
getopts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Parameters and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Positional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Special Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
standard-vars—A Collection of Useful Variables . . . . . . . . . . . . . . . . . 9
Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Pathname Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Bourne Shell Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
POSIX Parameter Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Shell-Specific Expansions, bash2, and ksh93 . . . . . . . . . . . . . . . . . . 15
Shell Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Sourcing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
v
673985214.005.png
vi
CONTENTS
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Functions Are Fast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Command Substitution Is Slow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using the Functions in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
standard-funcs: A Collection of Useful Commands . . . . . . . . . . . . . . . . . . . 19
1.1 get_key—Get a Single Keystroke from the User . . . . . . . . . . . . 19
1.2 getline—Prompt User to Enter a Line . . . . . . . . . . . . . . . . . . . . . . 20
1.3 press_any_key—Prompt for a Single Keypress . . . . . . . . . . . . . 21
1.4 menu1—Print a Menu and Execute a Selected
Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5 arg—Prompt for Required Argument If None Supplied . . . . . . . 23
1.6 die—Print Error Message and Exit with Error Status . . . . . . . . . 24
1.7 show_date—Display Date in D[D] MMM YYYY Format . . . . . . . 25
1.8 date_vars—Set Date and Time Variables . . . . . . . . . . . . . . . . . . . 26
1.9 is_num—Is This a Positive Integer? . . . . . . . . . . . . . . . . . . . . . . . 28
1.10 abbrev_num—Abbreviate Large Numbers . . . . . . . . . . . . . . . . 29
1.11 commas—Add Thousands Separators to a Number . . . . . . . . 30
1.12 pr1—Print Arguments, One to a Line . . . . . . . . . . . . . . . . . . . . . 32
1.13 checkdirs—Check for Directories; Create If Necessary . . . . . 33
1.14 checkfiles—Check That a Directory Contains Certain
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.15 zpad—Pad a Number with Leading Zeroes . . . . . . . . . . . . . . . . 34
1.16 cleanup—Remove Temporary Files and Reset Terminal
on Exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Unix Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
cat: Concatenate Files to the Standard Output . . . . . . . . . . . . . . . . . . 36
sed: A Text Stream Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
awk: Pattern Scanning and Processing Language . . . . . . . . . . . . . . . 37
grep: Print Lines Matching a Regular Expression . . . . . . . . . . . . . . . . 38
date: Show or Set the System Date . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
tr: A Character Translation Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
wc: Count Characters, Words, and Lines in a File . . . . . . . . . . . . . . . 38
file: Determine the File Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
ls: Sort and Provide Details About Files . . . . . . . . . . . . . . . . . . . . . . . . 39
uniq: Remove Consecutive Duplicate Lines . . . . . . . . . . . . . . . . . . . . . 39
sudo: Execute Commands as the Superuser . . . . . . . . . . . . . . . . . . . 40
split: Divide a File into Equal-Sized Pieces . . . . . . . . . . . . . . . . . . . . . 40
which: Show the Full Path to a Command . . . . . . . . . . . . . . . . . . . . . . 40
gs, gv: Render, Convert, or View PostScript and PDF Files . . . . . . . . 40
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
673985214.001.png
Zgłoś jeśli naruszono regulamin