PDF Wicked Cool Shell Scripts 101 Scripts For Linux OS X and UNIX Systems Dave Taylor Download
PDF Wicked Cool Shell Scripts 101 Scripts For Linux OS X and UNIX Systems Dave Taylor Download
com
https://textbookfull.com/product/wicked-cool-
shell-scripts-101-scripts-for-linux-os-x-and-unix-
systems-dave-taylor/
https://textbookfull.com/product/shell-programming-in-unix-linux-and-
os-x-stephen-g-kochan/
textbookfull.com
https://textbookfull.com/product/immersive-theater-and-activism-
scripts-and-strategies-for-directors-and-playwrights-nandita-dinesh/
textbookfull.com
https://textbookfull.com/product/mastering-linux-shell-scripting-a-
practical-guide-to-linux-command-line-bash-scripting-and-shell-
programming-ebrahim/
textbookfull.com
https://textbookfull.com/product/the-problem-of-the-rupee-its-origin-
and-its-solution-history-of-indian-currency-banking-b-r-ambedkar/
textbookfull.com
Nanomaterial and Polymer Membranes. Synthesis,
Characterization, and Applications 1st Edition Tawfik Abdo
Saleh
https://textbookfull.com/product/nanomaterial-and-polymer-membranes-
synthesis-characterization-and-applications-1st-edition-tawfik-abdo-
saleh/
textbookfull.com
https://textbookfull.com/product/machine-learning-and-data-mining-in-
aerospace-technology-aboul-ella-hassanien/
textbookfull.com
https://textbookfull.com/product/accelerating-the-transition-
to-a-100-renewable-energy-era-tanay-sidki-uyar/
textbookfull.com
https://textbookfull.com/product/knowing-people-the-val-kit-mystery-
series-book-7-1st-edition-rosalind-burgess/
textbookfull.com
https://textbookfull.com/product/social-theory-6th-edition-peter-j-
kivisto/
textbookfull.com
Evidence Use in Health Policy Making: An International
Public Policy Perspective Justin Parkhurst
https://textbookfull.com/product/evidence-use-in-health-policy-making-
an-international-public-policy-perspective-justin-parkhurst/
textbookfull.com
E
2 TI
D
N O
I
D N
W ICKED COOL
SHELL SCRIP T S
• 11 0
0 11 S
SCCR R II P
PTTS
S FF OO RR LL II N NU UX X ,, O
OSS X
X ,,
AA N
ND D UUN
N II X
X SS YY SS TT EE M
M SS
• Image processing and editing tools
• A ZIP code lookup tool that reports the city and state
“I can’t give this book enough praise and high enough recommendation.
Quite simply, this is the book that I wish I would’ve had when I was just learn-
ing Linux.”
—STEVE SUEHRING, LINUXWORLD EDITOR
“If you’re interested in the command line and shell scripting, this is an
excellent book which will extend your knowledge and provide more than a
hundred ready-to-run scripts.”
—MACBLOG.COM
“There are a few books in the [computer] field that remain useful for years
and years after their publishing date. Wicked Cool Shell Scripts is one of those
fortunate few.”
—THE BRAINSHED
by D av e T a y lo r a nd
Br a n d on P er r y
San Francisco
WICKED COOL SHELL SCRIPTS, 2ND EDITION. Copyright © 2017 by Dave Taylor and Brandon Perry.
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.
20 19 18 17 16 123456789
ISBN-10: 1-59327-602-8
ISBN-13: 978-1-59327-602-7
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900; info@nostarch.com
www.nostarch.com
Taylor, Dave.
Wicked cool shell scripts / Dave Taylor.
p. cm.
ISBN 1-59327-012-7
1. UNIX (Computer file) 2. UNIX Shells. I. Title.
QA76.76.O63T3895 2004
005.4'32--dc22
2003017496
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. 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 it.
About the Authors
Dave Taylor has been in the computer industry since 1980. He was a
contributor to BSD 4.4 UNIX, and his software is included in all major
UNIX distributions. He is an award-winning public speaker and has written
thousands of magazine and newspaper articles. He is the author of more
than 20 books, including Learning Unix for OS X (O’Reilly Media), Solaris 9
for Dummies (Wiley Publishing), and Sams Teach Yourself Unix in 24 Hours
(Sams Publishing). A popular columnist for Linux Journal magazine,
he also maintains a customer tech support and gadget reviews website,
askdavetaylor.com.
Brandon Perry started writing C# applications with the advent of the open
source .NET implementation Mono. In his free time, he enjoys writing
modules for the Metasploit framework, parsing binary files, and fuzzing
things.
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
CONTE NT S IN DE TA IL
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
INTRODUCTION xxv
What to Take Away . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi
This Book Is for You If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi
Organization of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi
Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
0
A SHELL SCRIPTS CRASH COURSE 1
What Is a Shell Script, Anyway? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Running Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Configuring Your Login Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Running Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Making Shell Scripts More Intuitive. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Why Shell Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Let’s Get Cracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1
THE MISSING CODE LIBRARY 9
What Is POSIX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
#1 Finding Programs in the PATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
#2 Validating Input: Alphanumeric Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
#3 Normalizing Date Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
#4 Presenting Large Numbers Attractively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
#5 Validating Integer Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
#6 Validating Floating-Point Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
#7 Validating Date Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
#8 Sidestepping Poor echo Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
#9 An Arbitrary-Precision Floating-Point Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . 34
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
#10 Locking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
#11 ANSI Color Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
#12 Building a Shell Script Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
#13 Debugging Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
x Contents in Detail
2
IMPROVING ON USER COMMANDS 51
#14 Formatting Long Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
#15 Backing Up Files as They’re Removed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
#16 Working with the Removed File Archive. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
#17 Logging File Removals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
#18 Displaying the Contents of Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
#19 Locating Files by Filename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
#20 Emulating Other Environments: MS-DOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
#21 Displaying Time in Different Time Zones. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Contents in Detail xi
3
CREATING UTILITIES 79
#22 A Reminder Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
#23 An Interactive Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
#24 Converting Temperatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
#25 Calculating Loan Payments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
#26 Keeping Track of Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
4
TWEAKING UNIX 97
#27 Displaying a File with Line Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
#28 Wrapping Only Long Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
#29 Displaying a File with Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5
SYSTEM ADMINISTRATION: MANAGING USERS 117
#35 Analyzing Disk Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
#36 Reporting Disk Hogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
#37 Improving the Readability of df Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6
SYSTEM ADMINISTRATION: SYSTEM MAINTENANCE 145
#45 Tracking Set User ID Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
#46 Setting the System Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
7
WEB AND INTERNET USERS 173
#53 Downloading Files via FTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
#54 Extracting URLs from a Web Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Contents in Detail xv
#55 Getting GitHub User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
#56 ZIP Code Lookup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
#57 Area Code Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
#58 Keeping Track of the Weather . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
#59 Digging Up Movie Info from IMDb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
#60 Calculating Currency Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
#61 Retrieving Bitcoin Address Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
#62 Tracking Changes on Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
9
WEB AND INTERNET ADMINISTRATION 217
#69 Identifying Broken Internal Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
10
INTERNET SERVER ADMINISTRATION 235
#73 Exploring the Apache access_log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
#74 Understanding Search Engine Traffic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
#75 Exploring the Apache error_log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
#76 Avoiding Disaster with a Remote Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
#77 Monitoring Network Status. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
11
OS X SCRIPTS 261
#79 Automating screencapture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
#80 Setting the Terminal Title Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
#81 Producing Summary Listings of iTunes Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 267
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
#82 Fixing the open Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
12
SHELL SCRIPT FUN AND GAMES 273
#83 Unscramble: A Word Game. . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 275
The Code . . . . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 275
How It Works . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 276
Running the Script . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 277
The Results . . . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 277
Hacking the Script. . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 277
#84 Hangman: Guess the Word Before It’s Too Late . . . . . . . . . . . . . . . . . . . . . . . . 277
The Code . . . . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 278
How It Works . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 279
Running the Script . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 280
The Results . . . . . . . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 280
Hacking the Script. . . . . . . . . ......... . . . . . . . . . . . . . . . . . . . . . . . . 281
13
WORKING WITH THE CLOUD 299
#89 Keeping Dropbox Running . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 300
The Code . . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 300
How It Works . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 300
Running the Script . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 301
The Results . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 301
Hacking the Script. . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 301
#90 Syncing Dropbox . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 301
The Code . . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 301
How It Works . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 303
Running the Script . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 303
The Results . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 303
Hacking the Script. . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 304
#91 Creating Slide Shows from Cloud Photo Streams. . . . . . . . . . . . . . . . . . . . . . . . 304
The Code . . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 305
How It Works . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 305
Running the Script . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 306
The Results . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 306
Hacking the Script. . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 306
#92 Syncing Files with Google Drive . ........... . . . . . . . . . . . . . . . . . . . . . . . 307
The Code . . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 307
How It Works . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 308
Running the Script . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 308
The Results . . . . . . . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 308
Hacking the Script. . . . . . . . ........... . . . . . . . . . . . . . . . . . . . . . . . 309
xx Contents in Detail
#93 The Computer Says . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
14
IMAGEMAGICK AND WORKING WITH GRAPHICS FILES 313
#94 A Smarter Image Size Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
#95 Watermarking Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
#96 Framing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
#97 Creating Image Thumbnails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
#98 Interpreting GPS Geolocation Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
15
DAYS AND DATES 329
#99 Finding the Day of a Specific Date in the Past. . . . . . . . . . . . . . . . . . . . . . . . . . 330
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
#100 Calculating Days Between Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
A
INSTALLING BASH ON WINDOWS 10 341
Turning On Developer Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Installing Bash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Microsoft’s Bash Shell vs. a Linux Distro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
B
BONUS SCRIPTS 345
#102 Bulk-Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
#103 Bulk-Running Commands on Multiprocessor Machines . . . . . . . . . . . . . . . . . . . 348
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
#104 Finding the Phase of the Moon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Running the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
The Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Hacking the Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
INDEX 355
Dave Taylor
Brandon Perry
Random documents with unrelated
content Scribd suggests to you:
«—Sapristi! fit Ninon, c'est ce pauvre chevalier qui en a emporté
les morceaux!»
Mlle de Quinsonas faillit s'écrier: «—Madame! ces morceaux sont
en place!» Mais elle ne dit rien et fut beaucoup plus étonnée encore
qu'avant d'interroger Ninon, car si les morceaux avaient été remis
aux mains du chevalier, qui avait disparu, comment pouvaient-ils
avoir été rétablis à leur place?
Mais passons sur cet épisode qui est venu nous distraire des
poursuites amoureuses qu'avait à subir la gouvernante, du matin au
soir. La pauvre fille les évitait de son mieux, et avec d'autant plus de
soin, peut-être, qu'elle commençait à en être troublée. Non que la
figure du marquis fût fort affriolante, mais en somme c'était un
gaillard, bâti solidement, vigoureux et sain; et quand Mlle de
Quinsonas voyait se mouvoir ces mains immenses qui convoitaient
voracement sa chair inquiète, elle sentait quelque chose de
comparable à une fourmilière qui lui grouillait avec des millions de
petites pattes autour des reins, puis partait en campagne,
dégringolait, enveloppait le pays alentour, monts et vallées, enfin lui
causait une telle fatigue des membres inférieurs, que parfois elle
s'arrêtait dans sa fuite, comme si quelqu'un lui eût jeté le lasso.
Mais elle avait résolu de ne sacrifier jamais l'équilibre de sa
situation à la rapidité d'un plaisir, et elle éprouvait une grande
tristesse des imprudences du marquis, parce qu'elle savait que
l'opinion a tôt fait de loger dans le même sac une femme qu'on
courtise et une femme qui a succombé. Et elle souhaitait trouver un
moyen de se soustraire au danger imminent d'un scandale qui
pouvait la rejeter du jour au lendemain dans la petite maison
humide due à la générosité de son oncle l'évêque et située dans une
méchante ruelle, derrière la cathédrale. Elle craignait aussi
beaucoup, d'autre part, que Jacquette n'allât parler de ce qu'elle
avait vu au bassin de l'Amour, et elle n'osait pas interdire à la petite
d'en parler, de peur qu'elle ne le racontât plus vite encore, et à tout
venant.
Voilà donc où en est notre infortunée gouvernante. Que va-t-elle
faire?
Lorsqu'on a grande envie de se laisser aller à quelque chose de
mauvais, ou qui vous doit causer de graves ennuis, on va demander
conseil à quelqu'un dont on connaît à peu près exactement l'avis par
avance, et qui vous engagera à vous abstenir de l'action
répréhensible ou dangereuse. On sort de chez cette personne en se
disant: «Cette personne a certainement raison.» On fait quatre pas
en admirant comme elle pense conformément aux principes selon
lesquels nous avons été élevés, puis au cinquième pas on se dit:
«Mais, tout de même, je serais curieux de savoir ce que ferait cette
personne si elle se trouvait exactement dans mon cas.» Ce qu'elle
ferait? Mais, elle viendrait vous demander conseil.
Mlle de Quinsonas se fût adressée à Mme de Matefelon, si la
vieille se fût trouvée là; cela va sans dire. Elle pouvait encore
recourir, tout aussi bien, à M. l'abbé Pucelle, son confesseur. Je
n'affirmerais pas qu'elle ne lui parla pas de ses embarras; mais si je
la mène à confesse, le moyen, s'il vous plaît, d'avoir l'air de
connaître la réponse du vénérable ecclésiastique, puisqu'aucun
prêtre n'a jamais trahi le secret de la confession? Que diriez-vous de
conduire la gouvernante chez le baron de Chemillé? Il y a quelque
temps que nous n'avons vu ce bonhomme, et je me suis engagé, il
me semble, à vous mener une fois chez lui. Pourquoi Mlle de
Quinsonas n'aurait-elle pas eu l'idée de consulter, dans la détresse,
un philosophe, malgré que la tournure d'esprit de celui-ci fût tenue
pour paradoxale?
Justement, Mlle de Quinsonas alla interroger le baron de
Chemillé, parce qu'elle se promit, en souriant, qu'elle ne suivrait pas
ses avis, qui étaient au rebours du sens commun. Elle prit Jacquette
par la main, et toutes deux s'engagèrent dans un sentier conduisant,
en raccourci, à Montsoreau, où le baron habitait. Elles sonnèrent à
sa petite maison. Le portail était ombragé par un tilleul, et les
fenêtres du rez-de-chaussée garnies de glycine. Une très jolie
soubrette les introduisit dans la bibliothèque de M. de Chemillé. Une
odeur de poussière et de tabac y était répandue, bien que les deux
fenêtres fussent ouvertes sur un jardinet fleuri des roses de l'arrière-
saison.
M. de Chemillé leva ses besicles et fit fête à ses visiteuses. Il
donna aussitôt des livres d'images à Jacquette, et ayant compris que
Mlle de Quinsonas avait quelque chose de confidentiel à lui dire, il lui
fit signe qu'il l'écoutait.
Mlle de Quinsonas ne se défendit point d'être un tantinet
intimidée; aussi, comme elle avait l'intention de débuter par l'aveu
de son intrigue avec le marquis, elle parla de tout autre chose et
raconta le phénomène de la statuette restaurée.
«—Ne vous émerveillez point, dit le baron, que ce marbre ait été
restauré, même par l'effet d'un miracle; car cette image—que je ne
cesse d'admirer, pour ma part,—est le symbole d'une force vive,
éternelle sans doute, et qui prévaudra contre tous les petits coups
de marteau de l'honorable Mme de Matefelon et les vôtres, ma belle
enfant. Je prise tant l'œuvre de M. François Gillet, que je me refuse
à y voir un marbre périssable! Non! Vraiment, c'est une divine
substance qui s'élève au milieu de ce bassin; et vous me viendriez
raconter demain que vous avez vu le Cupidon se mouvoir, venir à
vous et vous faire frémir, mademoiselle, par un contact, non froid,
mais chaud, que je n'en serais pas le moins du monde étonné.»
Mlle de Quinsonas rougissait, elle toussicotait, et la nef arrondie
de son séant tanguait et roulait dans la mer de duvet d'une grande
bergère où elle était assise. De la main, elle chassait la vision de ce
coquin d'Amour s'avançant vers elle, non froid, mais chaud.
«—Fi donc! dit-elle, Monsieur, vous admettez aisément la liberté
dans l'amour!…»
«—La liberté! dit le baron, non point, car il est le plus farouche et
le plus puissant despote; mais l'aisance dans les rapports amoureux,
c'est notre revanche, mademoiselle, contre les coups de force de ce
butor. Il nous terrasse: plions les reins avec élégance.»
«—Eh quoi! faut-il nous livrer sans vergogne au premier
satyre…»
«—Je m'indigne, dit M. de Chemillé, que l'on fasse tant d'affaires
d'une intrigue amoureuse. Un rendez-vous ne prend d'importance
que par les difficultés dont on s'ingénie à l'embarrasser. Que n'y met-
on plus de simplicité et de bonne grâce! il ne pèserait pas sur notre
vie le poids d'un grain de tabac sur la main.»
«—Ah! Monsieur, puisque vous y allez de ce ton, permettez-moi
de vous exposer un cas.»
Et la voilà qui glisse à propos sa petite histoire.
Le baron lui dit aussitôt que pour ce qui était du désir amoureux
du marquis, il le comprenait fort bien, du moment que Foulques
négligeait sa femme, ce qui était son seul tort. Mais, étant donné
qu'il était vraisemblable que la marquise s'égayait avec le jeune
page, le marquis ne pouvait mieux diriger son choix…
«—Ah! Monsieur, je devrais bondir, et je sais comment il se fait
que je vous écoute!»
«—Je me garde bien de vous indiquer, Mademoiselle, ce que vous
devez faire: je vous expose ce qui se fait: l'amour, quand il prend
seulement la forme d'un gamin, nous fouette comme de vils
esclaves, à plus forte raison quand il adopte les apparences d'un
maître.»
«—Mais, Monsieur, en admettant que nous fassions taire nos
préjugés ou nos répugnances, il reste un trouble public, un
scandale!»
«—Il est, dit le baron, un attribut de l'amour que les artistes
oublient de joindre à son petit bagage ordinaire et que je tiens pour
le plus joli et le plus précieux: c'est le silence.»
Et comme Mlle de Quinsonas se levait, il ajouta:
«—Et souvenez-vous, Mademoiselle, qu'il ne se fait presque rien
d'efficace en ce monde, qui ne soit le fruit d'une opinion téméraire.»
En rentrant au château, Mlle de Quinsonas et Jacquette virent
une personne noire qui se promenait de long en large sur le perron
avec la marquise. Et elles reconnurent le vénérable curé de
Montsoreau, l'abbé Pucelle.
M. l'abbé Pucelle était venu demander à Mme la marquise si elle
entendait faire préparer Jacquette à la première communion, car elle
courait sur ses dix ans.—Comme le temps passe!—Ninon répondit
que telle était en effet son intention, et M. le curé lui donna
quelques avis touchant la manière de vivre qu'il lui semblait décent
d'adopter pour Jacquette pendant les deux années qui la séparaient
du grand jour. Il conseilla de ne lui laisser voir le monde que le
moins possible et de l'entourer d'exemples édifiants. Ninon, qui était
très contrariée de se livrer au péché si près de sa fille, trouva que le
curé disait des choses justes et décida de cloîtrer Jacquette et sa
gouvernante dans les anciens appartements de feu M. Lemeunier de
Fontevrault, qui se trouvaient pour ainsi dire isolés. On les prépara
donc de façon que Jacquette et sa gouvernante y pussent demeurer
à l'abri du va-et-vient de la maison.
En un clin d'œil toutes les difficultés contre lesquelles essayait de
lutter Mlle de Quinsonas se trouvaient résolues, ou du moins
paraissaient bien l'être, et la bonne fille se demandait s'il n'était pas
préférable, en toute occasion, au lieu de se mettre martel en tête, de
s'abandonner aux soins excellents de la Providence.
XVIII