bash Cookbook (2007).pdf

(3237 KB) Pobierz
153244603 UNPDF
153244603.002.png
bash Cookbook
Carl Albing, JP Vossen, and Cameron Newham
Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo
153244603.003.png
bash Cookbook
by Carl Albing, JP Vossen, and Cameron Newham
Copyright © 2007 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles ( safari.oreilly.com ). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com .
Editor: Mike Loukides
Production Editor: Laurel R.T. Ruma
Copyeditor: Derek Di Matteo
Production Services: Tolman Creek Design
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrators: Robert Romano and Jessamyn Read
Printing History:
May 2007:
First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’ReillyMedia,Inc. bash Cookbook ,theimageofawoodturtle,andrelatedtradedressaretrademarks
of O’Reilly Media, Inc.
Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedas
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors
assume no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.
This book uses RepKover , a durable and flexible lay-flat binding.
ISBN 10: 0-596-52678-4
ISNB 13: 978-0-596-52678-8
[M]
153244603.004.png
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Beginning bash
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.1 Decoding the Prompt
4
1.2 Showing Where You Are
5
1.3 Finding and Running Commands
6
1.4 Getting Information About Files
8
1.5 Showing All Hidden (dot) Files in the Current Directory
10
1.6 Using Shell Quoting
12
1.7 Using or Replacing Built-ins and External Commands
13
1.8 Determining If You Are Running Interactively
15
1.9 Setting bash As Your Default Shell
16
1.10 Getting bash for Linux
17
1.11 Getting bash for xBSD
20
1.12 Getting bash for Mac OS X
21
1.13 Getting bash for Unix
22
1.14 Getting bash for Windows
23
1.15 Getting bash Without Getting bash
24
1.16 Learning More About bash Documentation
25
2. Standard Output
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
2.1 Writing Output to the Terminal/Window
29
2.2 Writing Output but Preserving Spacing
30
2.3 Writing Output with More Formatting Control
31
2.4 Writing Output Without the Newline
32
2.5 Saving Output from a Command
33
2.6 Saving Output to Other Files
34
iii
153244603.005.png
 
2.7 Saving Output from the ls Command
35
2.8 Sending Both Output and Error Messages to Different Files
37
2.9 Sending Both Output and Error Messages to the Same File
37
2.10 Appending Rather Than Clobbering Output
39
2.11 Using Just the Beginning or End of a File
39
2.12 Skipping a Header in a File
40
2.13 Throwing Output Away
41
2.14 Saving or Grouping Output from Several Commands
41
2.15 Connecting Two Programs by Using Output As Input
43
2.16 Saving a Copy of Output Even While Using It As Input
44
2.17 Connecting Two Programs by Using Output As Arguments
46
2.18 Using Multiple Redirects on One Line
47
2.19 Saving Output When Redirect Doesn’t Seem to Work
48
2.20 Swapping STDERR and STDOUT
50
2.21 Keeping Files Safe from Accidental Overwriting
52
2.22 Clobbering a File on Purpose
53
3. Standard Input
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
3.1 Getting Input from a File
55
3.2 Keeping Your Data with Your Script
56
3.3 Preventing Weird Behavior in a Here-Document
57
3.4 Indenting Here-Documents
59
3.5 Getting User Input
60
3.6 Getting Yes or No Input
61
3.7 Selecting from a List of Options
64
3.8 Prompting for a Password
65
4. Executing Commands
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
4.1 Running Any Executable
67
4.2 Telling If a Command Succeeded or Not
69
4.3 Running Several Commands in Sequence
71
4.4 Running Several Commands All at Once
72
4.5 Deciding Whether a Command Succeeds
74
4.6 Using Fewer if Statements
75
4.7 Running Long Jobs Unattended
76
4.8 Displaying Error Messages When Failures Occur
77
4.9 Running Commands from a Variable
78
4.10 Running All Scripts in a Directory
79
iv
|
Table of Contents
153244603.001.png
Zgłoś jeśli naruszono regulamin