shell Script Invocation Error

Owenlzy 2012-11-27 11:23:47
shell Script Invocation Error
/Users/rjxyrjxy/Desktop/iPhone/TemporaryItems/Diceshaker.build/Debug-iphonesimulator/Update Localizations.build/Script-154232570E6F3BA800335D5C.sh: line 2: /BuildLocale: No such file or directory
怎么解决???
...全文
203 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix ChaPter 1 introduction to Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 What Shell Scripting Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 What Shell Scripting Isn’t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Why Shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 The Bourne Shell Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Why Portable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Why Not? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Beyond Portability: Cleanliness and Good Living . . . . . . . . . . . . . . . .10 What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Introducing the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Interactive and Noninteractive Usage . . . . . . . . . . . . . . . . . . . . . . . . . .11 Simple Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Introducing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Introducing Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 The printf Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 ChaPter 2 Patterns and regular expressions . . . . . . . . . . . . . . . . . . . . . . . . .21 Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Pattern-Matching Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Using Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Pathname Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Differences from Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Using Globs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 10436fmfinal 7 10/23/08 10:40:25 PM ■CONTENTS viii Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Basic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Backreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33 Extended Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Common Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Replacements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Using Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Replacing Patterns with Regular Expressions . . . . . . . . . . . . . . . . . . .39 Common Pitfalls of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . .40 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 ChaPter 3 Basic Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Introducing Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 What Is Truth? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Introducing Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Introducing Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Thinking About Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Introducing Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 Understanding File Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Redirection Using exec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Introducing Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 Redirection and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 ChaPter 4 Core Shell Features explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Words and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Command Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Shell Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Escaping Characters with a Backslash . . . . . . . . . . . . . . . . . . . . . . . .77 Escaping Characters with Single Quotes . . . . . . . . . . . . . . . . . . . . . . .79 Escaping Characters with Double Quotes . . . . . . . . . . . . . . . . . . . . . .79 Quoting Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 10436fmfinal 8 10/23/08 10:40:26 PM ■CONTENTS ix Substitution and Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Substitution and Field Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Understanding Parameter Substitution . . . . . . . . . . . . . . . . . . . . . . . .81 Tilde Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Globbing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 ChaPter 5 Shells Within Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Understanding Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Variables and the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Manipulating the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Temporary Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Exploring Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Subshells and External Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Implicit and Explicit Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Modifying the State of the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Shell Builtins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Shell Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 The eval Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 The dot ( .) Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Using Shells Within Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 When to Use an External Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 When to Use eval or dot ( .) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 When to Use Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 When to Use Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . .112 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 ChaPter 6 invocation and execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Shell Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 How UNIX Runs Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Shell Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Using Positional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 Manipulating Parameters for Fun and Profit . . . . . . . . . . . . . . . . . . .122 Shell Startup and Interactive Sessions . . . . . . . . . . . . . . . . . . . . . . . .128 10436fmfinal 9 10/23/08 10:40:26 PM ■CONTENTS x Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 More on Jobs and Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Understanding Runtime Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 ChaPter 7 Shell Language Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 More on Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Portability Issues: Welcome to the Club . . . . . . . . . . . . . . . . . . . . . . .147 Common Extensions and Omissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Other Kinds of Expansion and Substitution . . . . . . . . . . . . . . . . . . . .148 Syntax Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Common Omissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Common Shells and Their Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Almquist Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Bourne-Again Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 Debian Almquist Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 Korn Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Public Domain Korn Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Solaris /usr/xpg4/bin/sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 SVR2 Bourne Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 SVR4 Bourne Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 Traditional Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 Z Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166 Execution Preambles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Setting Options and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Picking a Better Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169 Self-Modifying Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Emulating Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 10436fmfinal 10 10/23/08 10:40:27 PM ■CONTENTS xi ChaPter 8 Utility Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Common Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Days of Yore: System V and BSD . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 GNU Arrives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 busybox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 Shell Builtins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 Avoiding Unnecessary Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 Relying on Extensions Considered Harmful . . . . . . . . . . . . . . . . . . . .181 Test with More Than One Shell on More Than One System . . . . . .182 Document Your Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Common Utility Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Public Enemy #1: echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Multiple Different Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Archive Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Block Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Other Common Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 What to Do When Something Is Unavailable . . . . . . . . . . . . . . . . . . . . . . . .200 Roll Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Add a Missing Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Use Something Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Demand a Real System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 A Few Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 ChaPter 9 Bringing it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Handling Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Temporary Files and Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208 Handling Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Startup Files and Environment Variables . . . . . . . . . . . . . . . . . . . . . .211 Documentation and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 Degrade Gracefully . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Specify, and Test For, Requirements . . . . . . . . . . . . . . . . . . . . . . . . .216 Scripts That Write Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Building a Script for a Specific Target . . . . . . . . . . . . . . . . . . . . . . . .217 Mixing with Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 10436fmfinal 11 10/23/08 10:40:27 PM ■CONTENTS xii ChaPter 10 Shell Script design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Do One Thing Well . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Separate Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Isolate Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Be Cooperative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 Filters, File Manipulation, and Program Manipulation . . . . . . . . . . .221 Command-Line Options and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . .224 Designing Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Options and Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 Set Reasonable Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 Define Your Functional Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Define Your Target Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Case Study: pids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 ChaPter 11 Mixing and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Mixing Quoting Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Embedding Shell Scripts in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Shell and make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Shell and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241 Embedding Code in Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Shell and sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Shell and awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 Utilities and Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 aPPeNdix a the Shell Command Language . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 . Shell Command Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 .1 Shell Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 .2 Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 2 .3 Token Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258 2 .4 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 2 .5 Parameters and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 2 .6 Word Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 2 .7 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 2 .8 Exit Status and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 2 .9 Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276 10436fmfinal 12 10/23/08 10:40:28 PM ■CONTENTS xiii 2 .10 Shell Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 2 .11 Signals and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 2 .12 Shell Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . .293 2 .13 Pattern Matching Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294 2 .14 Special Built- In Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 aPPeNdix B the sh Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 Stdin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Asynchronous Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Output Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Extended Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command History List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command Line Editing (vi- mode) . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 vi Line Editing Insert Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 vi Line Editing Command Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Consequences Of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Application Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Rationale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Future Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Change History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Issue 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Issue 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 10436fmfinal 13 10/23/08 10:40:28 PM ■CONTENTS xiv aPPeNdix C regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 9 . Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 9 .1 Regular Expression Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . .320 9 .2 Regular Expression General Requirements . . . . . . . . . . . . . . . .321 9 .3 Basic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322 9 .4 Extended Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . .327 9 .5 Regular Expression Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . .330 iNdex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ 一、注意事项 命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。 ~/ 表示主目录。 ./ 当前目录(一个点)。 ../ 上一级目录(两个点)。 ; 多个命令一起用。 > >> 输出重定向 。将一个命令的输出内容写入到一个文件里面。如果该文件存在, 就将该文件的内容覆盖; 如果不存在就先创建该文件, 然后再写入内容。 输出重定向,意思就是说,将原来屏幕输出变为文件输出,即将内容输到文件中。 < << 输入重定向。 本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * 匹配零个或者多个字符。 ? 匹配一个字符。 [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后接命令符 -a 所有(all)。 -e 所有(every),比a更详细。 -f 取消保护。 -i 添加提示。 -p 强制执行。 -r 目录管理。 分屏显示的中途操作 空格 继续打开下一屏; 回车 继续打开下一行; b 另外开上一屏; f 另外开下一屏; h 帮助; q或Ctrl+C 退出; /字符串 从上往下查找匹配的字符串; ?字符串 从下往上查找匹配的字符串; n 继续查找。 四、退出命令 exit 退出; DOS内部命令 用于退出当前的命令处理器(COMMAND.COM) 恢复前一个命令处理器。 Ctrl+d 跟exit一样效果,表中止本次操作。 logout 当csh时可用来退出,其他shell不可用。 clear 清屏,清除(之前的内容并未删除,只是没看到,拉回上面可以看回)。 五、目录管理命令 pwd 显示当前所在目录,打印当前目录的绝对路径。 cd 进入某目录,DOS内部命令 显示或改变当前目录。 cd回车/cd ~ 都是回到自己的主目录。 cd . 当前目录(空格再加一个点)。 cd .. 回到上一级目录(空格再加两个点)。 cd ../.. 向上两级。 cd /user/s0807 从绝对路径去到某目录。 cd ~/s0807 直接进入主目录下的某目录(“cd ~"相当于主目录的路径的简写)。 ls 显示当前目录的所有目录和文件。 用法 ls [-aAbcCdeEfFghHilLmnopqrRstux1@] [file...] ls /etc/ 显示某目录下的所有文件和目录,如etc目录下的。 ls -l (list)列表显示文件(默认按文件名排序), 显示文件的权限、硬链接数(即包含文件数,普通文件是1,目录1+)、用户、组名、大小、修改日期、文件名。 ls -t (time)按修改时间排序,显示目录和文件。 ls -lt 是“-l”和“-t”的组合,按时间顺序显示列表。 ls -F 显示文件类型,目录“/ ”结尾;可执行文件“*”结尾;文本文件(none),没有结尾。 ls -R 递归显示目录结构。即该目录下的文件和各个副目录下的文件都一一显示。 ls -a 显示所有文件,包括隐藏文件。 文件权限 r 读权限。对普通文件来说,是读取该文件的权限;对目录来说,是获得该目录下的文件信息。 w 写权限。对文件,是修改;对目录,是增删文件与子目录。 (注 删除没有写权限的文件可以用 rm -f ,这是为了操作方便,是人性化的设计)。 x 执行权限;对目录,是进入该目录 - 表示没有权限 形式 - rw- r-- r-- 其中 第一个是文件类型(-表普通文件,d表目录,l表软链接文件) 第2~4个是属主,生成文件时登录的人,权限最高,用u表示 第5~7个是属组,系统管理员分配的同组的一个或几个人,用g表示 第8~10个是其他人,除属组外的人,用o表示 所有人,包括属主、属组及其他人,用a表示 chmod 更改权限; 用法 chmod [-fR] <绝对模式> 文件 ... chmod [-fR] <符号模式列表> 文件 ... 其中 <符号模式列表> 是一个用逗号分隔的表 [ugoa]{+|-|=}[rwxXlstugo] chmod u+rw 给用户加权限。同理,u-rw也可以减权限。 chmod u=rw 给用户赋权限。与加权限不一样,赋权限有覆盖的效果。 主要形式有如下几种 chmod u+rw chmod u=rw chmod u+r, u+w chmod u+rw,g+w, o+r chmod 777( 用数字的方式设置权限是最常用的) 数字表示权限时,各数位分别表示属主、属组及其他人; 其中,1是执行权(Execute),2是写权限(Write),4是读权限(Read), 具体权限相当于三种权限的数相加,如7=1+2+4,即拥有读写和执行权。 另外,临时文件/目录的权限为rwt,可写却不可删,关机后自动删除;建临时目录:chmod 777 目录名,再chmod +t 目录名。 id 显示用户有效的uid(用户字)和gid(组名) 用法 id [-ap] [user] id 显示自己的。 id root 显示root的。 id -a root 显示用户所在组的所有组名(如root用户,是所有组的组员) df 查看文件系统,查看数据区 用法 df [-F FSType] [-abeghklntVvZ] [-o FSType 特定选项] [目录 | 块设备 | 资源] df -k 以kbytes显示文件大小的查看文件系统方式 六、显示文件内容 more 分屏显示文件的内容。 用法 more [-cdflrsuw] [-行] [+行号] [+/模式] [文件名 ...]。 显示7个信息:用户名 密码 用户id(uid) 组id(gid) 描述信息(一般为空) 用户主目录 login shell(登录shell) cat 显示文件内容,不分屏(一般用在小文件,大文件显示不下);合并文件,仅在屏幕上合并,并不改变原文件。 用法 cat [ -usvtebn ] [-|文件] ... tail 实时监控文件,一般用在日志文件,可以只看其中的几行。 用法 tail [+/-[n][lbc][f]] [文件] tail [+/-[n][l][r|f]] [文件] 七、文件/目录的增删 echo 显示一行内容。 touch 如果文件/目录不存在,则创建新文件/目录;如果文件存在,那么就是更新该文件的最后访问时间, 用法 touch [-acm] [-r ref_file] 文件... touch [-acm] [MMDDhhmm[yy]] 文件... touch [-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file... mkdir 创建目录(必须有创建目录的权限) 用法 mkdir [-m 模式] [-p] dirname ... mkdir dir1/dir2 在dir1下建dir2 mkdir dir13 dir4 dir5 连建多个 mkdir ~/games 用户主目录下建(默认在当前目录下创建) mkdir -p dir6/dir7/dir8 强制创建dir8;若没有前面的目录,会自动创建dir6和dir7。 不用-p时,若没有dir6/dir7,则创建失败。 cp 复制文件/目录 cp 源文件 目标文件 复制文件;若已有文件则覆盖 cp -r 源目录 目标目录 复制目录;若已有目录则把源目录复制到目标目录下, 没有目标目录时,相当于完全复制源目录,只是文件名不同。 cp beans apple dir2 把beans、apple文件复制到dir2目录下 cp -i beans apple 增加是否覆盖的提示 mv 移动或重命名文件/目录 用法 mv [-f] [-i] f1 f2 mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 mv 源文件名 目标文件名 若目标文件名还没有,则是源文件重命名为目标文件;若目标文件已存在,则源文件覆盖目标文件。 mv 源文件名 目标目录 移动文件 mv 源目录 目标目录 若目标目录不存在,则源目录重命名;若目标目录已存在,则源目录移动到目标目录下。 rm 删除文件/目录 用法 rm [-fiRr] 文件 ... rm 文件名 删除文件。 rm -r 目录名 删除目录。 rm –f 文件 只要是该文件或者目录的拥有者,无论是否有权限删除,都可以用这个命令参数强行删除。 rm -rf * 删除所有文件及目录 rmdir 删除空目录。只可以删除空目录。 ln 创建硬链接或软链接,硬链接=同一文件的多个名字;软链接=快捷方式 用法 ln [-f] [-n] [-s] f1 [f2] ln [-f] [-n] [-s] f1 ... fn d1 ln [-f] [-n] -s d1 d2 ln file1 file1.ln 创建硬链接。感觉是同一文件,删除一个,对另一个没有影响;须两个都删除才算删除。 ln -s file1 file1.sln 创建软链接。可跨系统操作,冲破操作权限;也是快捷方式。 八、时间显示 date 显示时间,精确到秒 用法 date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] cal 显示日历 cal 9 2008 显示2008年9月的日历; cal 显示当月的 用法 cal [ [月] 年 ] 九、帮助 man 帮助( format and display the on-line manual pages) 用法 man [-] [-adFlrt] [-M 路径] [-T 宏软件包] [-s 段] 名称 ... man [-] [-adFlrt] [-M path] [-T macro-package] [-s section] name... man [-M 路径] -k 关键字 ... man [-M 路径] -f 文件 ... awk 按一定格式输出(pattern scanning and processing language) 用法 awk [-Fc] [-f 源代码 | 'cmds'] [文件] 十、vi 底行模式 /? 命令模式 i a o 输入模式 vi 的使用方法 1、光标 h 左 j 下 k 上 l 右 set nu 显示行号(set nonu) 21 光标停在指定行 21G 第N行 (G到文件尾,1G到文件头) 如果要将光标移动到文件第一行,那么就按 1G H 屏幕头 M 屏幕中间 L 屏幕底 ^ 或 shift+6 行首 $ 或 shift+4 行尾 Ctrl+f 下翻 Ctrl+b 上翻 2、输入 (输入模式) o 光标往下换一行 O (大写字母o)在光标所在行上插入一空行 i 在光标所在位置的前面插入字母 a 在光标所在位置的后面插入一个新字母 退出插入状态。 3、修改替换 r 替换一个字符 dd 删除行,剪切行 (5dd删除5行) 5,10d 删除 5 至 10 行(包括第 5行和第 10 行) x 删除一个字符 dw 删除词,剪切词。 ( 3dw删除 3 单词) cw 替换一个单词。 (cw 和 dw 的区别 cw 删除某一个单词后直接进入编辑模式,而dw删除词后仍处于命令模式) cc 替换一行 C 替换从光标到行尾 yy 复制行 (用法同下的 Y ,见下行) Y 将光标移动到要复制行位置,按yy。当你想粘贴的时候,请将光标移动到你想复制的位置的前一个位置,然后按 p yw 复制词 p 当前行下粘贴 1,2co3 复制行1,2在行3之后 4,5m6 移动行4,5在行6之后 u 当你的前一个命令操作是一个误操作的时候,那么可以按一下 u键,即可复原。只能撤销一次 r file2 在光标所在处插入另一个文件 ~ 将字母变成大写 J 可以将当前行与下一行连接起来 /字符串 从上往下找匹配的字符串 ?字符串 从下往上找匹配的字符串 n 继续查找 1,$s/旧串/新串/g 替换全文(或者 %s/旧串/新串/g) (1表示从第一行开始) 没有g则只替换一次,加g替换所有 3、存盘和退出 w 存盘 w newfile 存成新文件 wq 存盘再退出VI(或者ZZ或 X) q! 强行退出不存盘 查看用户 users 显示在线用户(仅显示用户名)。 who 显示在线用户,但比users更详细,包括用户名、终端号、登录时间、IP地址。 who am i 仅显示自己,(但包括用户名、端口、登录时间、IP地址;信息量=who)。 whoami 也仅显示自己,但只有用户名(仅显示自己的有效的用户名)。 w 显示比who更多内容,还包括闲置时间、占CPU、平均占用CPU、执行命令。 用法 w [ -hlsuw ] [ 用户 ] su 改变用户,需再输入密码。 用法 su [-] [ username [ arg ... ] ] su - 相当于退出再重新登录。 查找 find 查找文件 用法 find [-H | -L] 路径列表 谓词列表 find / -name perl 从根目录开始查找名为perl的文件。 find . -mtime 10 -print 从当前目录查找距离现在10天时修改的文件,显示在屏幕上。 (注 “10”表示第10天的时候;如果是“+10”表示10天以外的范围;“-10”表示10天以内的范围。) grep 文件中查找字符;有过滤功能,只列出想要的内容 用法 grep -hblcnsviw 模式 文件 . . . 如 grep abc /etc/passwd 在passwd文件下找abc字符 wc 统计 -l 统计行数; -w统计单词数; -c 统计字符数 如 grep wang /etc/passwd|wc -l 统计passwd文件含“wang”的行数 du 查看目录情况 如 du -sk * 不加-s会显示子目录,-k按千字节排序 用法 du [-a] [-d] [-h|-k] [-r] [-o|-s] [-H|-L] [文件...] 进程管理 ps 显示进程。 用法 ps [ -aAdeflcjLPyZ ] [ -o 格式 ] [ -t 项列表 ] [ -u 用户列表 ] [ -U 用户列表 ] [ -G 组列表 ] [ -p 进程列表 ] [ -g 程序组列表 ] [ -s 标识符列表 ] [ -z 区域列表 ] ps 显示自己的进程。 ps -e 显示每个进程,包括空闲进程。 ps -f 显示详情。 ps -ef 组合-e和-f,所有进程的详情。 ps -U uidlist(用户列表) 具体查看某人的进程。 kill pkill sleep jobs 用法 jobs [-l ] fg %n bg %n stop %n 挂起(仅csh能用) Ctrl+C Ctrl+Z 网络链接 ping usage ping host [timeout] usage ping -s [-l | U] [adLnRrv] [-A addr_family] [-c traffic_class] [-g gateway [-g gateway ...]] [-F flow_label] [-I interval] [-i interface] [-P tos] [-p port] [-t ttl] host [data_size] [npackets] ifconfig -a /sbin/ifconfig 查看本机的IP地址 netstat -rn rlogin ftp 帮助文件 [sd0807@localhost ~]$ help GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star (*) next to a name means that the command is disabled. JOB_SPEC [&] (( expression )) . filename [arguments] [ arg... ] [[ expression ]] alias [-p] [name[=value] ... ] bg [job_spec ...] bind [-lpvsPVS] [-m keymap] [-f fi break [n] builtin [shell-builtin [arg ...]] caller [EXPR] case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir] command [-pVv] command [arg ...] compgen [-abcdefgjksuv] [-o option complete [-abcdefgjksuv] [-pr] [-o continue [n] declare [-afFirtx] [-p] [name[=val dirs [-clpv] [+N] [-N] disown [-h] [-ar] [jobspec ...] echo [-neE] [arg ...] enable [-pnds] [-a] [-f filename] eval [arg ...] exec [-cl] [-a name] file [redirec exit [n] export [-nf] [name[=value] ...] or false fc [-e ename] [-nlr] [first] [last fg [job_spec] for NAME [in WORDS ... ;] do COMMA for (( exp1; exp2; exp3 )); do COM function NAME { COMMANDS ; } or NA getopts optstring name [arg] hash [-lr] [-p pathname] [-dt] [na help [-s] [pattern ...] history [-c] [-d offset] [n] or hi if COMMANDS; then COMMANDS; [ elif jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si let arg [arg ...] local name[=value] ... logout popd [+N | -N] [-n] printf [-v var] format [arguments] pushd [dir | +N | -N] [-n] pwd [-LP] read [-ers] [-u fd] [-t timeout] [ readonly [-af] [name[=value] ...] return [n] select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o option] [arg ...] shift [n] shopt [-pqsu] [-o long-option] opt source filename [arguments] suspend [-f] test [expr] time [-p] PIPELINE times trap [-lp] [arg signal_spec ...] true type [-afptP] name [name ...] typeset [-afFirtx] [-p] name[=valu ulimit [-SHacdfilmnpqstuvx] [limit umask [-p] [-S] [mode] unalias [-a] name [name ...] unset [-f] [-v] [name ...] until COMMANDS; do COMMANDS; done variables - Some variable names an wait [n] while COMMANDS; do COMMANDS; done { COMMANDS ; } 输入 man help BASH_BUILTINS(1) BASH_BUILTINS(1) NAME bash, :, ., [, alias, bg, bind, break, builtin, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit, umask, una- lias, unset, wait - bash built-in commands, see bash(1) BASH BUILTIN COMMANDS Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options. For example, the :, true, false, and test builtins do not accept options. : [arguments] No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned. . filename [arguments] source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file- name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read. alias [-p] [name[=value] ...] Alias with no arguments or with the -p option prints the list of aliases in the form alias name=value on standard output. When arguments are supplied, an alias is defined for each name whose value is given. A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded. For each name in the argument list for which no value is supplied, the name and value of the alias is printed. Alias returns true unless a name is given for which no alias has been defined. bg [jobspec ...] Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell’s notion of the current job is used. bg jobspec returns 0 unless run when job control is disabled or, when run with job con- trol enabled, any specified jobspec was not found or was started without job control. bind [-m keymap] [-lpsvPSV] bind [-m keymap] [-q function] [-u function] [-r keyseq] bind [-m keymap] -f filename bind [-m keymap] -x keyseq:shell-command bind [-m keymap] keyseq:function-name bind readline-command Display current readline key and function bindings, bind a key sequence to a readline function or macro, or set a readline variable. Each non-option argument is a command as it would appear in .inputrc, but each binding or command must be passed as a sepa- rate argument; e.g., ’"\C-x\C-r": re-read-init-file’. Options, if supplied, have the following meanings: -m keymap Use keymap as the keymap to be affected by the subsequent bindings. Accept- able keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command, and vi-insert. vi is equivalent to vi-command; emacs is equivalent to emacs-standard. -l List the names of all readline functions. -p Display readline function names and bindings in such a way that they can be re-read. -P List current readline function names and bindings. -v Display readline variable names and values in such a way that they can be re- read. -V List current readline variable names and values. -s Display readline key sequences bound to macros and the strings they output in such a way that they can be re-read. -S Display readline key sequences bound to macros and the strings they output. -f filename Read key bindings from filename. -q function Query about which keys invoke the named function. -u function Unbind all keys bound to the named function. -r keyseq Remove any current binding for keyseq. -x keyseq:shell-command Cause shell-command to be executed whenever keyseq is entered. The return value is 0 unless an unrecognized option is given or an error occurred. break [n] Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless the shell is not executing a loop when break is executed. builtin shell-builtin [arguments] Execute the specified shell builtin, passing it arguments, and return its exit sta- tus. This is useful when defining a function whose name is the same as a shell builtin, retaining the functionality of the builtin within the function. The cd builtin is commonly redefined this way. The return status is false if shell-builtin is not a shell builtin command. cd [-L|-P] [dir] Change the current directory to dir. The variable HOME is the default dir. The variable CDPATH defines the search path for the directory containing dir. Alterna- tive directory names in CDPATH are separated by a colon (:). A null directory name in CDPATH is the same as the current directory, i.e., ‘‘.’’. If dir begins with a slash (/), then CDPATH is not used. The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command); the -L option forces symbolic links to be followed. An argument of - is equivalent to $OLDPWD. If a non-empty directory name from CDPATH is used, or if - is the first argument, and the directory change is successful, the absolute path- name of the new working directory is written to the standard output. The return value is true if the directory was successfully changed; false otherwise. caller [expr] Returns the context of any active subroutine call (a shell function or a script exe- cuted with the . or source builtins. Without expr, caller displays the line number and source filename of the current subroutine call. If a non-negative integer is supplied as expr, caller displays the line number, subroutine name, and source file corresponding to that position in the current execution call stack. This extra information may be used, for example, to print a stack trace. The current frame is frame 0. The return value is 0 unless the shell is not executing a subroutine call or expr does not correspond to a valid position in the call stack. command [-pVv] command [arg ...] Run command with args suppressing the normal shell function lookup. Only builtin com- mands or commands found in the PATH are executed. If the -p option is given, the search for command is performed using a default value for PATH that is guaranteed to find all of the standard utilities. If either the -V or -v option is supplied, a description of command is printed. The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option produces a more verbose description. If the -V or -v option is supplied, the exit status is 0 if command was found, and 1 if not. If neither option is supplied and an error occurred or command cannot be found, the exit status is 127. Otherwise, the exit status of the command builtin is the exit status of command. compgen [option] [word] Generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output. When using the -F or -C options, the vari- ous shell variables set by the programmable completion facilities, while available, will not have useful values. The matches will be generated in the same way as if the programmable completion code had generated them directly from a completion specification with the same flags. If word is specified, only those completions matching word will be displayed. The return value is true unless an invalid option is supplied, or no matches were generated. complete [-abcdefgjksuv] [-o comp-option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] name [name ...] complete -pr [name ...] Specify how arguments to each name should be completed. If the -p option is sup- plied, or if no options are supplied, existing completion specifications are printed in a way that allows them to be reused as input. The -r option removes a completion specification for each name, or, if no names are supplied, all completion specifica- tions. The process of applying these completion specifications when word completion is attempted is described above under Programmable Completion. Other options, if specified, have the following meanings. The arguments to the -G, -W, and -X options (and, if necessary, the -P and -S options) should be quoted to protect them from expansion before the complete builtin is invoked. -o comp-option The comp-option controls several aspects of the compspec’s behavior beyond the simple generation of completions. comp-option may be one of: bashdefault Perform the rest of the default bash completions if the compspec gen- erates no matches. default Use readline’s default filename completion if the compspec generates no matches. dirnames Perform directory name completion if the compspec generates no matches. filenames Tell readline that the compspec generates filenames, so it can per- form any filename-specific processing (like adding a slash to direc- tory names or suppressing trailing spaces). Intended to be used with shell functions. nospace Tell readline not to append a space (the default) to words completed at the end of the line. plusdirs After any matches defined by the compspec are generated, directory name completion is attempted and any matches are added to the results of the other actions. -A action The action may be one of the following to generate a list of possible comple- tions: alias Alias names. May also be specified as -a. arrayvar Array variable names. binding Readline key binding names. builtin Names of shell builtin commands. May also be specified as -b. command Command names. May also be specified as -c. directory Directory names. May also be specified as -d. disabled Names of disabled shell builtins. enabled Names of enabled shell builtins. export Names of exported shell variables. May also be specified as -e. file File names. May also be specified as -f. function Names of shell functions. group Group names. May also be specified as -g. helptopic Help topics as accepted by the help builtin. hostname Hostnames, as taken from the file specified by the HOSTFILE shell variable. job Job names, if job control is active. May also be specified as -j. keyword Shell reserved words. May also be specified as -k. running Names of running jobs, if job control is active. service Service names. May also be specified as -s. setopt Valid arguments for the -o option to the set builtin. shopt Shell option names as accepted by the shopt builtin. signal Signal names. stopped Names of stopped jobs, if job control is active. user User names. May also be specified as -u. variable Names of all shell variables. May also be specified as -v. -G globpat The filename expansion pattern globpat is expanded to generate the possible completions. -W wordlist The wordlist is split using the characters in the IFS special variable as delimiters, and each resultant word is expanded. The possible completions are the members of the resultant list which match the word being completed. -C command command is executed in a subshell environment, and its output is used as the possible completions. -F function The shell function function is executed in the current shell environment. When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable. -X filterpat filterpat is a pattern as used for filename expansion. It is applied to the list of possible completions generated by the preceding options and argu- ments, and each completion matching filterpat is removed from the list. A leading ! in filterpat negates the pattern; in this case, any completion not matching filterpat is removed. -P prefix prefix is added at the beginning of each possible completion after all other options have been applied. -S suffix suffix is appended to each possible completion after all other options have been applied. The return value is true unless an invalid option is supplied, an option other than -p or -r is supplied without a name argument, an attempt is made to remove a comple- tion specification for a name for which no specification exists, or an error occurs adding a completion specification. continue [n] Resume the next iteration of the enclosing for, while, until, or select loop. If n is specified, resume at the nth enclosing loop. n must be ≥ 1. If n is greater than the number of enclosing loops, the last enclosing loop (the ‘‘top-level’’ loop) is resumed. The return value is 0 unless the shell is not executing a loop when con- tinue is executed. declare [-afFirtx] [-p] [name[=value] ...] typeset [-afFirtx] [-p] [name[=value] ...] Declare variables and/or give them attributes. If no names are given then display the values of variables. The -p option will display the attributes and values of each name. When -p is used, additional options are ignored. The -F option inhibits the display of function definitions; only the function name and attributes are printed. If the extdebug shell option is enabled using shopt, the source file name and line number where the function is defined are displayed as well. The -F option implies -f. The following options can be used to restrict output to variables with the specified attribute or to give variables attributes: -a Each name is an array variable (see Arrays above). -f Use function names only. -i The variable is treated as an integer; arithmetic evaluation (see ARITHMETIC EVALUATION ) is performed when the variable is assigned a value. -r Make names readonly. These names cannot then be assigned values by subsequent assignment statements or unset. -t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special mean- ing for variables. -x Mark names for export to subsequent commands via the environment. Using ‘+’ instead of ‘-’ turns off the attribute instead, with the exception that +a may not be used to destroy an array variable. When used in a function, makes each name local, as with the local command. If a variable name is followed by =value, the value of the variable is set to value. The return value is 0 unless an invalid option is encountered, an attempt is made to define a function using ‘‘-f foo=bar’’, an attempt is made to assign a value to a readonly variable, an attempt is made to assign a value to an array variable without using the compound assignment syntax (see Arrays above), one of the names is not a valid shell variable name, an attempt is made to turn off readonly status for a readonly variable, an attempt is made to turn off array status for an array variable, or an attempt is made to display a non-exis- tent function with -f. dirs [-clpv] [+n] [-n] Without options, displays the list of currently remembered directories. The default display is on a single line with directory names separated by spaces. Directories are added to the list with the pushd command; the popd command removes entries from the list. +n Displays the nth entry counting from the left of the list shown by dirs when invoked without options, starting with zero. -n Displays the nth entry counting from the right of the list shown by dirs when invoked without options, starting with zero. -c Clears the directory stack by deleting all of the entries. -l Produces a longer listing; the default listing format uses a tilde to denote the home directory. -p Print the directory stack with one entry per line. -v Print the directory stack with one entry per line, prefixing each entry with its index in the stack. The return value is 0 unless an invalid option is supplied or n indexes beyond the end of the directory stack. disown [-ar] [-h] [jobspec ...] Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job. echo [-neE] [arg ...] Output the args, separated by spaces, followed by a newline. The return status is always 0. If -n is specified, the trailing newline is suppressed. If the -e option is given, interpretation of the following backslash-escaped characters is enabled. The -E option disables the interpretation of these escape characters, even on systems where they are interpreted by default. The xpg_echo shell option may be used to dynamically determine whether or not echo expands these escape characters by default. echo does not interpret -- to mean the end of options. echo interprets the following escape sequences: \a alert (bell) \b backspace \c suppress trailing newline \e an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \0nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits) \nnn the eight-bit character whose value is the octal value nnn (one to three octal digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) enable [-adnps] [-f filename] [name ...] Enable and disable builtin shell commands. Disabling a builtin allows a disk command which has the same name as a shell builtin to be executed without specifying a full pathname, even though the shell normally searches for builtins before disk commands. If -n is used, each name is disabled; otherwise, names are enabled. For example, to use the test binary found via the PATH instead of the shell builtin version, run ‘‘enable -n test’’. The -f option means to load the new builtin command name from shared object filename, on systems that support dynamic loading. The -d option will delete a builtin previously loaded with -f. If no name arguments are given, or if the -p option is supplied, a list of shell builtins is printed. With no other option arguments, the list consists of all enabled shell builtins. If -n is supplied, only disabled builtins are printed. If -a is supplied, the list printed includes all builtins, with an indication of whether or not each is enabled. If -s is supplied, the output is restricted to the POSIX special builtins. The return value is 0 unless a name is not a shell builtin or there is an error loading a new builtin from a shared object. eval [arg ...] The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0. exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth arg passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environ- ment. If -a is supplied, the shell passes name as the zeroth argument to the exe- cuted command. If command cannot be executed for some reason, a non-interactive shell exits, unless the shell option execfail is enabled, in which case it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1. exit [n] Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell termi- nates. export [-fn] [name[=word]] ... export -p The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed. The -n option causes the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to word. export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is sup- plied with a name that is not a function. fc [-e ename] [-nlr] [first] [last] fc -s [pat=rep] [cmd] Fix Command. In the first form, a range of commands from first to last is selected from the history list. First and last may be specified as a string (to locate the last command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to the current command for listing (so that ‘‘fc -l -10’’ prints the last 10 commands) and to first otherwise. If first is not speci- fied it is set to the previous command for editing and -16 for listing. The -n option suppresses the command numbers when listing. The -r option reverses the order of the commands. If the -l option is given, the commands are listed on standard output. Otherwise, the editor given by ename is invoked on a file contain- ing those commands. If ename is not given, the value of the FCEDIT variable is used, and the value of EDITOR if FCEDIT is not set. If neither variable is set, is used. When editing is complete, the edited commands are echoed and executed. In the second form, command is re-executed after each instance of pat is replaced by rep. A useful alias to use with this is ‘‘r="fc -s"’’, so that typing ‘‘r cc’’ runs the last command beginning with ‘‘cc’’ and typing ‘‘r’’ re-executes the last command. If the first form is used, the return value is 0 unless an invalid option is encoun- tered or first or last specify history lines out of range. If the -e option is sup- plied, the return value is the value of the last command executed or failure if an error occurs with the temporary file of commands. If the second form is used, the return status is that of the command re-executed, unless cmd does not specify a valid history line, in which case fc returns failure. fg [jobspec] Resume jobspec in the foreground, and make it the current job. If jobspec is not present, the shell’s notion of the current job is used. The return value is that of the command placed into the foreground, or failure if run when job control is dis- abled or, when run with job control enabled, if jobspec does not specify a valid job or jobspec specifies a job that was started without job control. getopts optstring name [args] getopts is used by shell procedures to parse positional parameters. optstring con- tains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. The colon and question mark characters may not be used as option char- acters. Each time it is invoked, getopts places the next option in the shell vari- able name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND auto- matically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used. When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to ?. getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead. getopts can report errors in two ways. If the first character of optstring is a colon, silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered. If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon. If an invalid option is seen, getopts places ? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in OPTARG and no diagnostic message is printed. If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a colon (:) is placed in name and OPTARG is set to the option charac- ter found. getopts returns true if an option, specified or unspecified, is found. It returns false if the end of options is encountered or an error occurs. hash [-lr] [-p filename] [-dt] [name] For each name, the full file name of the command is determined by searching the directories in $PATH and remembered. If the -p option is supplied, no path search is performed, and filename is used as the full file name of the command. The -r option causes the shell to forget all remembered locations. The -d option causes the shell to forget the remembered location of each name. If the -t option is supplied, the full pathname to which each name corresponds is printed. If multiple name arguments are supplied with -t, the name is printed before the hashed full pathname. The -l option causes output to be displayed in a format that may be reused as input. If no arguments are given, or if only -l is supplied, information about remembered commands is printed. The return status is true unless a name is not found or an invalid option is supplied. help [-s] [pattern] Display helpful information about builtin commands. If pattern is specified, help gives detailed help on all commands matching pattern; otherwise help for all the builtins and shell control structures is printed. The -s option restricts the infor- mation displayed to a short usage synopsis. The return status is 0 unless no command matches pattern. history [n] history -c history -d offset history -anrw [filename] history -p arg [arg ...] history -s arg [arg ...] With no options, display the command history list with line numbers. Lines listed with a * have been modified. An argument of n lists only the last n lines. If the shell variable HISTTIMEFORMAT is set and not null, it is used as a format string for strftime(3) to display the time stamp associated with each displayed history entry. No intervening blank is printed between the formatted time stamp and the history line. If filename is supplied, it is used as the name of the history file; if not, the value of HISTFILE is used. Options, if supplied, have the following meanings: -c Clear the history list by deleting all the entries. -d offset Delete the history entry at position offset. -a Append the ‘‘new’’ history lines (history lines entered since the beginning of the current bash session) to the history file. -n Read the history lines not already read from the history file into the current history list. These are lines appended to the history file since the begin- ning of the current bash session. -r Read the contents of the history file and use them as the current history. -w Write the current history to the history file, overwriting the history file’s contents. -p Perform history substitution on the following args and display the result on the standard output. Does not store the results in the history list. Each arg must be quoted to disable normal history expansion. -s Store the args in the history list as a single entry. The last command in the history list is removed before the args are added. If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file. The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied as an argument to -p fails. jobs [-lnprs] [ jobspec ... ] jobs -x command [ args ... ] The first form lists the active jobs. The options have the following meanings: -l List process IDs in addition to the normal information. -p List only the process ID of the job’s process group leader. -n Display information only about jobs that have changed status since the user was last notified of their status. -r Restrict output to running jobs. -s Restrict output to stopped jobs. If jobspec is given, output is restricted to information about that job. The return status is 0 unless an invalid option is encountered or an invalid jobspec is sup- plied. If the -x option is supplied, jobs replaces any jobspec found in command or args with the corresponding process group ID, and executes command passing it args, returning its exit status. kill [-s sigspec | -n signum | -sigspec] [pid | jobspec] ... kill -l [sigspec | exit_status] Send the signal named by sigspec or signum to the processes named by pid or jobspec. sigspec is either a case-insensitive signal name such as SIGKILL (with or without the SIG prefix) or a signal number; signum is a signal number. If sigspec is not present, then SIGTERM is assumed. An argument of -l lists the signal names. If any arguments are supplied when -l is given, the names of the signals corresponding to the arguments are listed, and the return status is 0. The exit_status argument to -l is a number specifying either a signal number or the exit status of a process termi- nated by a signal. kill returns true if at least one signal was successfully sent, or false if an error occurs or an invalid option is encountered. let arg [arg ...] Each arg is an arithmetic expression to be evaluated (see ARITHMETIC EVALUATION). If the last arg evaluates to 0, let returns 1; 0 is returned otherwise. local [option] [name[=value] ...] For each argument, a local variable named name is created, and assigned value. The option can be any of the options accepted by declare. When local is used within a function, it causes the variable name to have a visible scope restricted to that function and its children. With no operands, local writes a list of local variables to the standard output. It is an error to use local when not within a function. The return status is 0 unless local is used outside a function, an invalid name is sup- plied, or name is a readonly variable. logout Exit a login shell. popd [-n] [+n] [-n] Removes entries from the directory stack. With no arguments, removes the top direc- tory from the stack, and performs a cd to the new top directory. Arguments, if sup- plied, have the following meanings: +n Removes the nth entry counting from the left of the list shown by dirs, start- ing with zero. For example: ‘‘popd +0’’ removes the first directory, ‘‘popd +1’’ the second. -n Removes the nth entry counting from the right of the list shown by dirs, starting with zero. For example: ‘‘popd -0’’ removes the last directory, ‘‘popd -1’’ the next to last. -n Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated. If the popd command is successful, a dirs is performed as well, and the return status is 0. popd returns false if an invalid option is encountered, the directory stack is empty, a non-existent directory stack entry is specified, or the directory change fails. printf [-v var] format [arguments] Write the formatted arguments to the standard output under the control of the format. The format is a character string which contains three types of objects: plain charac- ters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standard printf(1) formats, %b causes printf to expand backslash escape sequences in the cor- responding argument (except that \c terminates output, backslashes in \', \", and \? are not removed, and octal escapes beginning with \0 may contain up to four digits), and %q causes printf to output the corresponding argument in a format that can be reused as shell input. The -v option causes the output to be assigned to the variable var rather than being printed to the standard output. The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied. The return value is zero on success, non-zero on failure. pushd [-n] [dir] pushd [-n] [+n] [-n] Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories and returns 0, unless the directory stack is empty. Arguments, if supplied, have the following meanings: +n Rotates the stack so that the nth directory (counting from the left of the list shown by dirs, starting with zero) is at the top. -n Rotates the stack so that the nth directory (counting from the right of the list shown by dirs, starting with zero) is at the top. -n Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated. dir Adds dir to the directory stack at the top, making it the new current working directory. If the pushd command is successful, a dirs is performed as well. If the first form is used, pushd returns 0 unless the cd to dir fails. With the second form, pushd returns 0 unless the directory stack is empty, a non-existent directory stack element is specified, or the directory change to the specified new current directory fails. pwd [-LP] Print the absolute pathname of the current working directory. The pathname printed contains no symbolic links if the -P option is supplied or the -o physical option to the set builtin command is enabled. If the -L option is used, the pathname printed may contain symbolic links. The return status is 0 unless an error occurs while reading the name of the current directory or an invalid option is supplied. read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...] One line is read from the standard input, or from the file descriptor fd supplied as an argument to the -u option, and the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values. The characters in IFS are used to split the line into words. The backslash character (\) may be used to remove any special meaning for the next character read and for line continuation. Options, if supplied, have the following meanings: -a aname The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored. -d delim The first character of delim is used to terminate the input line, rather than newline. -e If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line. -n nchars read returns after reading nchars characters rather than waiting for a com- plete line of input. -p prompt Display prompt on standard error, without a trailing newline, before attempt- ing to read any input. The prompt is displayed only if input is coming from a terminal. -r Backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation. -s Silent mode. If input is coming from a terminal, characters are not echoed. -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe. -u fd Read input from file descriptor fd. If no names are supplied, the line read is assigned to the variable REPLY. The return code is zero, unless end-of-file is encountered, read times out, or an invalid file descriptor is supplied as the argument to -u. readonly [-apf] [name[=word] ...] The given names are marked readonly; the values of these names may not be changed by subsequent assignment. If the -f option is supplied, the functions corresponding to the names are so marked. The -a option restricts the variables to arrays. If no name arguments are given, or if the -p option is supplied, a list of all readonly names is printed. The -p option causes output to be displayed in a format that may be reused as input. If a variable name is followed by =word, the value of the vari- able is set to word. The return status is 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function. return [n] Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script. set [--abefhkmnptuvxBCHP] [-o option] [arg ...] Without options, the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the currently-set variables. Read-only variables cannot be reset. In posix mode, only shell variables are listed. The output is sorted according to the current locale. When options are specified, they set or unset shell attributes. Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n. Options, if specified, have the following meanings: -a Automatically mark variables and functions which are modified or created for export to the environment of subsequent commands. -b Report the status of terminated background jobs immediately, rather than before the next primary prompt. This is effective only when job control is enabled. -e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or ││ list, or if the command’s return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits. -f Disable pathname expansion. -h Remember the location of commands as they are looked up for execution. This is enabled by default. -k All arguments in the form of assignment statements are placed in the environ- ment for a command, not just those that precede the command name. -m Monitor mode. Job control is enabled. This option is on by default for interactive shells on systems that support it (see JOB CONTROL above). Back- ground processes run in a separate process group and a line containing their exit status is printed upon their completion. -n Read commands but do not execute them. This may be used to check a shell script for syntax errors. This is ignored by interactive shells. -o option-name The option-name can be one of the following: allexport Same as -a. braceexpand Same as -B. emacs Use an emacs-style command line editing interface. This is enabled by default when the shell is interactive, unless the shell is started with the --noediting option. errtrace Same as -E. functrace Same as -T. errexit Same as -e. hashall Same as -h. histexpand Same as -H. history Enable command history, as described above under HISTORY. This option is on by default in interactive shells. ignoreeof The effect is as if the shell command ‘‘IGNOREEOF=10’’ had been exe- cuted (see Shell Variables above). keyword Same as -k. monitor Same as -m. noclobber Same as -C. noexec Same as -n. noglob Same as -f. nolog Currently ignored. notify Same as -b. nounset Same as -u. onecmd Same as -t. physical Same as -P. pipefail If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all command
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in Numeric Literals PEP 525: Asynchronous Generators PEP 530: Asynchronous Comprehensions PEP 487: Simpler customization of class creation PEP 487: Descriptor Protocol Enhancements PEP 519: Adding a file system path protocol PEP 495: Local Time Disambiguation PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument Order New dict implementation PEP 523: Adding a frame evaluation API to CPython PYTHONMALLOC environment variable DTrace and SystemTap probing support Other Language Changes New Modules secrets Improved Modules array ast asyncio binascii cmath collections concurrent.futures contextlib datetime decimal distutils email encodings enum faulthandler fileinput hashlib http.client idlelib and IDLE importlib inspect json logging math multiprocessing os pathlib pdb pickle pickletools pydoc random re readline rlcompleter shlex site sqlite3 socket socketserver ssl statistics struct subprocess sys telnetlib time timeit tkinter traceback tracemalloc typing unicodedata unittest.mock urllib.request urllib.robotparser venv warnings winreg winsound xmlrpc.client zipfile zlib Optimizations Build and C API Changes Other Improvements Deprecated New Keywords Deprecated Python behavior Deprecated Python modules, functions and methods asynchat asyncore dbm distutils grp importlib os re ssl tkinter venv Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API Changes in the C API CPython bytecode changes Notable changes in Python 3.6.2 New make regen-all build target Removal of make touch build target Notable changes in Python 3.6.5 What’s New In Python 3.5 Summary – Release highlights New Features PEP 492 - Coroutines with async and await syntax PEP 465 - A dedicated infix operator for matrix multiplication PEP 448 - Additional Unpacking Generalizations PEP 461 - percent formatting support for bytes and bytearray PEP 484 - Type Hints PEP 471 - os.scandir() function – a better and faster directory iterator PEP 475: Retry system calls failing with EINTR PEP 479: Change StopIteration handling inside generators PEP 485: A function for testing approximate equality PEP 486: Make the Python Launcher aware of virtual environments PEP 488: Elimination of PYO files PEP 489: Multi-phase extension module initialization Other Language Changes New Modules typing zipapp Improved Modules argparse asyncio bz2 cgi cmath code collections collections.abc compileall concurrent.futures configparser contextlib csv curses dbm difflib distutils doctest email enum faulthandler functools glob gzip heapq http http.client idlelib and IDLE imaplib imghdr importlib inspect io ipaddress json linecache locale logging lzma math multiprocessing operator os pathlib pickle poplib re readline selectors shutil signal smtpd smtplib sndhdr socket ssl Memory BIO Support Application-Layer Protocol Negotiation Support Other Changes sqlite3 subprocess sys sysconfig tarfile threading time timeit tkinter traceback types unicodedata unittest unittest.mock urllib wsgiref xmlrpc xml.sax zipfile Other module-level changes Optimizations Build and C API Changes Deprecated New Keywords Deprecated Python Behavior Unsupported Operating Systems Deprecated Python modules, functions and methods Removed API and Feature Removals Porting to Python 3.5 Changes in Python behavior Changes in the Python API Changes in the C API What’s New In Python 3.4 Summary – Release Highlights New Features PEP 453: Explicit Bootstrapping of PIP in Python Installations Bootstrapping pip By Default Documentation Changes PEP 446: Newly Created File Descriptors Are Non-Inheritable Improvements to Codec Handling PEP 451: A ModuleSpec Type for the Import System Other Language Changes New Modules asyncio ensurepip enum pathlib selectors statistics tracemalloc Improved Modules abc aifc argparse audioop base64 collections colorsys contextlib dbm dis doctest email filecmp functools gc glob hashlib hmac html http idlelib and IDLE importlib inspect ipaddress logging marshal mmap multiprocessing operator os pdb pickle plistlib poplib pprint pty pydoc re resource select shelve shutil smtpd smtplib socket sqlite3 ssl stat struct subprocess sunau sys tarfile textwrap threading traceback types urllib unittest venv wave weakref xml.etree zipfile CPython Implementation Changes PEP 445: Customization of CPython Memory Allocators PEP 442: Safe Object Finalization PEP 456: Secure and Interchangeable Hash Algorithm PEP 436: Argument Clinic Other Build and C API Changes Other Improvements Significant Optimizations Deprecated Deprecations in the Python API Deprecated Features Removed Operating Systems No Longer Supported API and Feature Removals Code Cleanups Porting to Python 3.4 Changes in ‘python’ Command Behavior Changes in the Python API Changes in the C API Changed in 3.4.3 PEP 476: Enabling certificate verification by default for stdlib http clients What’s New In Python 3.3 Summary – Release highlights PEP 405: Virtual Environments PEP 420: Implicit Namespace Packages PEP 3118: New memoryview implementation and buffer protocol documentation Features API changes PEP 393: Flexible String Representation Functionality Performance and resource usage PEP 397: Python Launcher for Windows PEP 3151: Reworking the OS and IO exception hierarchy PEP 380: Syntax for Delegating to a Subgenerator PEP 409: Suppressing exception context PEP 414: Explicit Unicode literals PEP 3155: Qualified name for classes and functions PEP 412: Key-Sharing Dictionary PEP 362: Function Signature Object PEP 421: Adding sys.implementation SimpleNamespace Using importlib as the Implementation of Import New APIs Visible Changes Other Language Changes A Finer-Grained Import Lock Builtin functions and types New Modules faulthandler ipaddress lzma Improved Modules abc array base64 binascii bz2 codecs collections contextlib crypt curses datetime decimal Features API changes email Policy Framework Provisional Policy with New Header API Other API Changes ftplib functools gc hmac http html imaplib inspect io itertools logging math mmap multiprocessing nntplib os pdb pickle pydoc re sched select shlex shutil signal smtpd smtplib socket socketserver sqlite3 ssl stat struct subprocess sys tarfile tempfile textwrap threading time types unittest urllib webbrowser xml.etree.ElementTree zlib Optimizations Build and C API Changes Deprecated Unsupported Operating Systems Deprecated Python modules, functions and methods Deprecated functions and types of the C API Deprecated features Porting to Python 3.3 Porting Python code Porting C code Building C extensions Command Line Switch Changes What’s New In Python 3.2 PEP 384: Defining a Stable ABI PEP 389: Argparse Command Line Parsing Module PEP 391: Dictionary Based Configuration for Logging PEP 3148: The concurrent.futures module PEP 3147: PYC Repository Directories PEP 3149: ABI Version Tagged .so Files PEP 3333: Python Web Server Gateway Interface v1.0.1 Other Language Changes New, Improved, and Deprecated Modules email elementtree functools itertools collections threading datetime and time math abc io reprlib logging csv contextlib decimal and fractions ftp popen select gzip and zipfile tarfile hashlib ast os shutil sqlite3 html socket ssl nntp certificates imaplib http.client unittest random poplib asyncore tempfile inspect pydoc dis dbm ctypes site sysconfig pdb configparser urllib.parse mailbox turtledemo Multi-threading Optimizations Unicode Codecs Documentation IDLE Code Repository Build and C API Changes Porting to Python 3.2 What’s New In Python 3.1 PEP 372: Ordered Dictionaries PEP 378: Format Specifier for Thousands Separator Other Language Changes New, Improved, and Deprecated Modules Optimizations IDLE Build and C API Changes Porting to Python 3.1 What’s New In Python 3.0 Common Stumbling Blocks Print Is A Function Views And Iterators Instead Of Lists Ordering Comparisons Integers Text Vs. Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already Present In Python 2.6 Library Changes PEP 3101: A New Approach To String Formatting Changes To Exceptions Miscellaneous Other Changes Operators And Special Methods Builtins Build and C API Changes Performance Porting To Python 3.0 What’s New in Python 2.7 The Future for Python 2.x Changes to the Handling of Deprecation Warnings Python 3.1 Features PEP 372: Adding an Ordered Dictionary to collections PEP 378: Format Specifier for Thousands Separator PEP 389: The argparse Module for Parsing Command Lines PEP 391: Dictionary-Based Configuration For Logging PEP 3106: Dictionary Views PEP 3137: The memoryview Object Other Language Changes Interpreter Changes Optimizations New and Improved Modules New module: importlib New module: sysconfig ttk: Themed Widgets for Tk Updated module: unittest Updated module: ElementTree 1.3 Build and C API Changes Capsules Port-Specific Changes: Windows Port-Specific Changes: Mac OS X Port-Specific Changes: FreeBSD Other Changes and Fixes Porting to Python 2.7 New Features Added to Python 2.7 Maintenance Releases PEP 434: IDLE Enhancement Exception for All Branches PEP 466: Network Security Enhancements for Python 2.7 Acknowledgements What’s New in Python 2.6 Python 3.0 Changes to the Development Process New Issue Tracker: Roundup New Documentation Format: reStructuredText Using Sphinx PEP 343: The ‘with’ statement Writing Context Managers The contextlib module PEP 366: Explicit Relative Imports From a Main Module PEP 370: Per-user site-packages Directory PEP 371: The multiprocessing Package PEP 3101: Advanced String Formatting PEP 3105: print As a Function PEP 3110: Exception-Handling Changes PEP 3112: Byte Literals PEP 3116: New I/O Library PEP 3118: Revised Buffer Protocol PEP 3119: Abstract Base Classes PEP 3127: Integer Literal Support and Syntax PEP 3129: Class Decorators PEP 3141: A Type Hierarchy for Numbers The fractions Module Other Language Changes Optimizations Interpreter Changes New and Improved Modules The ast module The future_builtins module The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific Changes: Windows Port-Specific Changes: Mac OS X Port-Specific Changes: IRIX Porting to Python 2.6 Acknowledgements What’s New in Python 2.5 PEP 308: Conditional Expressions PEP 309: Partial Function Application PEP 314: Metadata for Python Software Packages v1.1 PEP 328: Absolute and Relative Imports PEP 338: Executing Modules as Scripts PEP 341: Unified try/except/finally PEP 342: New Generator Features PEP 343: The ‘with’ statement Writing Context Managers The contextlib module PEP 352: Exceptions as New-Style Classes PEP 353: Using ssize_t as the index type PEP 357: The ‘__index__’ method Other Language Changes Interactive Interpreter Changes Optimizations New, Improved, and Removed Modules The ctypes package The ElementTree package The hashlib package The sqlite3 package The wsgiref package Build and C API Changes Port-Specific Changes Porting to Python 2.5 Acknowledgements What’s New in Python 2.4 PEP 218: Built-In Set Objects PEP 237: Unifying Long Integers and Integers PEP 289: Generator Expressions PEP 292: Simpler String Substitutions PEP 318: Decorators for Functions and Methods PEP 322: Reverse Iteration PEP 324: New subprocess Module PEP 327: Decimal Data Type Why is Decimal needed? The Decimal type The Context type PEP 328: Multi-line Imports PEP 331: Locale-Independent Float/String Conversions Other Language Changes Optimizations New, Improved, and Deprecated Modules cookielib doctest Build and C API Changes Port-Specific Changes Porting to Python 2.4 Acknowledgements What’s New in Python 2.3 PEP 218: A Standard Set Datatype PEP 255: Simple Generators PEP 263: Source Code Encodings PEP 273: Importing Modules from ZIP Archives PEP 277: Unicode file name support for Windows NT PEP 278: Universal Newline Support PEP 279: enumerate() PEP 282: The logging Package PEP 285: A Boolean Type PEP 293: Codec Error Handling Callbacks PEP 301: Package Index and Metadata for Distutils PEP 302: New Import Hooks PEP 305: Comma-separated Files PEP 307: Pickle Enhancements Extended Slices Other Language Changes String Changes Optimizations New, Improved, and Deprecated Modules Date/Time Type The optparse Module Pymalloc: A Specialized Object Allocator Build and C API Changes Port-Specific Changes Other Changes and Fixes Porting to Python 2.3 Acknowledgements What’s New in Python 2.2 Introduction PEPs 252 and 253: Type and Class Changes Old and New Classes Descriptors Multiple Inheritance: The Diamond Rule Attribute Access Related Links PEP 234: Iterators PEP 255: Simple Generators PEP 237: Unifying Long Integers and Integers PEP 238: Changing the Division Operator Unicode Changes PEP 227: Nested Scopes New and Improved Modules Interpreter Changes and Fixes Other Changes and Fixes Acknowledgements What’s New in Python 2.1 Introduction PEP 227: Nested Scopes PEP 236: __future__ Directives PEP 207: Rich Comparisons PEP 230: Warning Framework PEP 229: New Build System PEP 205: Weak References PEP 232: Function Attributes PEP 235: Importing Modules on Case-Insensitive Platforms PEP 217: Interactive Display Hook PEP 208: New Coercion Model PEP 241: Metadata in Python Packages New and Improved Modules Other Changes and Fixes Acknowledgements What’s New in Python 2.0 Introduction What About Python 1.6? New Development Process Unicode List Comprehensions Augmented Assignment String Methods Garbage Collection of Cycles Other Core Changes Minor Language Changes Changes to Built-in Functions Porting to 2.0 Extending/Embedding Changes Distutils: Making Modules Easy to Install XML Modules SAX2 Support DOM Support Relationship to PyXML Module changes New modules IDLE Improvements Deleted and Deprecated Modules Acknowledgements Changelog Python 3.6.5 final? Tests Build Python 3.6.5 release candidate 1? Security Core and Builtins Library Documentation Tests Build Windows macOS IDLE Tools/Demos C API Python 3.6.4 final? Python 3.6.4 release candidate 1? Core and Builtins Library Documentation Tests Build Windows macOS IDLE Tools/Demos C API Python 3.6.3 final? Library Build Python 3.6.3 release candidate 1? Security Core and Builtins Library Documentation Tests Build Windows IDLE Tools/Demos Python 3.6.2 final? Python 3.6.2 release candidate 2? Security Python 3.6.2 release candidate 1? Core and Builtins Library Security Library IDLE C API Build Documentation Tools/Demos Tests Windows Python 3.6.1 final? Core and Builtins Build Python 3.6.1 release candidate 1? Core and Builtins Library IDLE Windows C API Documentation Tests Build Python 3.6.0 final? Python 3.6.0 release candidate 2? Core and Builtins Tools/Demos Windows Build Python 3.6.0 release candidate 1? Core and Builtins Library C API Documentation Tools/Demos Python 3.6.0 beta 4? Core and Builtins Library Documentation Tests Build Python 3.6.0 beta 3? Core and Builtins Library Windows Build Tests Python 3.6.0 beta 2? Core and Builtins Library Windows C API Build Tests Python 3.6.0 beta 1? Core and Builtins Library IDLE C API Tests Build Tools/Demos Windows Python 3.6.0 alpha 4? Core and Builtins Library IDLE Tests Windows Build Python 3.6.0 alpha 3? Core and Builtins Library Security Library Security Library IDLE C API Build Tools/Demos Documentation Tests Python 3.6.0 alpha 2? Core and Builtins Library Security Library Security Library IDLE Documentation Tests Windows Build Windows C API Tools/Demos Python 3.6.0 alpha 1? Core and Builtins Library Security Library Security Library Security Library IDLE Documentation Tests Build Windows Tools/Demos C API Python 3.5.3 final? Python 3.5.3 release candidate 1? Core and Builtins Library Security Library Security Library IDLE C API Documentation Tests Tools/Demos Windows Build Python 3.5.2 final? Core and Builtins Tests IDLE Python 3.5.2 release candidate 1? Core and Builtins Security Library Security Library Security Library Security Library Security Library IDLE Documentation Tests Build Windows Tools/Demos Windows Python 3.5.1 final? Core and Builtins Windows Python 3.5.1 release candidate 1? Core and Builtins Library IDLE Documentation Tests Build Windows Tools/Demos Python 3.5.0 final? Build Python 3.5.0 release candidate 4? Library Build Python 3.5.0 release candidate 3? Core and Builtins Library Python 3.5.0 release candidate 2? Core and Builtins Library Python 3.5.0 release candidate 1? Core and Builtins Library IDLE Documentation Tests Python 3.5.0 beta 4? Core and Builtins Library Build Python 3.5.0 beta 3? Core and Builtins Library Tests Documentation Build Python 3.5.0 beta 2? Core and Builtins Library Python 3.5.0 beta 1? Core and Builtins Library IDLE Tests Documentation Tools/Demos Python 3.5.0 alpha 4? Core and Builtins Library Build Tests Tools/Demos C API Python 3.5.0 alpha 3? Core and Builtins Library Build Tests Tools/Demos Python 3.5.0 alpha 2? Core and Builtins Library Build C API Windows Python 3.5.0 alpha 1? Core and Builtins Library IDLE Build C API Documentation Tests Tools/Demos Windows The Python Tutorial 1. Whetting Your Appetite 2. Using the Python Interpreter 2.1. Invoking the Interpreter 2.1.1. Argument Passing 2.1.2. Interactive Mode 2.2. The Interpreter and Its Environment 2.2.1. Source Code Encoding 3. An Informal Introduction to Python 3.1. Using Python as a Calculator 3.1.1. Numbers 3.1.2. Strings 3.1.3. Lists 3.2. First Steps Towards Programming 4. More Control Flow Tools 4.1. if Statements 4.2. for Statements 4.3. The range() Function 4.4. break and continue Statements, and else Clauses on Loops 4.5. pass Statements 4.6. Defining Functions 4.7. More on Defining Functions 4.7.1. Default Argument Values 4.7.2. Keyword Arguments 4.7.3. Arbitrary Argument Lists 4.7.4. Unpacking Argument Lists 4.7.5. Lambda Expressions 4.7.6. Documentation Strings 4.7.7. Function Annotations 4.8. Intermezzo: Coding Style 5. Data Structures 5.1. More on Lists 5.1.1. Using Lists as Stacks 5.1.2. Using Lists as Queues 5.1.3. List Comprehensions 5.1.4. Nested List Comprehensions 5.2. The del statement 5.3. Tuples and Sequences 5.4. Sets 5.5. Dictionaries 5.6. Looping Techniques 5.7. More on Conditions 5.8. Comparing Sequences and Other Types 6. Modules 6.1. More on Modules 6.1.1. Executing modules as scripts 6.1.2. The Module Search Path 6.1.3. “Compiled” Python files 6.2. Standard Modules 6.3. The dir() Function 6.4. Packages 6.4.1. Importing * From a Package 6.4.2. Intra-package References 6.4.3. Packages in Multiple Directories 7. Input and Output 7.1. Fancier Output Formatting 7.1.1. Old string formatting 7.2. Reading and Writing Files 7.2.1. Methods of File Objects 7.2.2. Saving structured data with json 8. Errors and Exceptions 8.1. Syntax Errors 8.2. Exceptions 8.3. Handling Exceptions 8.4. Raising Exceptions 8.5. User-defined Exceptions 8.6. Defining Clean-up Actions 8.7. Predefined Clean-up Actions 9. Classes 9.1. A Word About Names and Objects 9.2. Python Scopes and Namespaces 9.2.1. Scopes and Namespaces Example 9.3. A First Look at Classes 9.3.1. Class Definition Syntax 9.3.2. Class Objects 9.3.3. Instance Objects 9.3.4. Method Objects 9.3.5. Class and Instance Variables 9.4. Random Remarks 9.5. Inheritance 9.5.1. Multiple Inheritance 9.6. Private Variables 9.7. Odds and Ends 9.8. Iterators 9.9. Generators 9.10. Generator Expressions 10. Brief Tour of the Standard Library 10.1. Operating System Interface 10.2. File Wildcards 10.3. Command Line Arguments 10.4. Error Output Redirection and Program Termination 10.5. String Pattern Matching 10.6. Mathematics 10.7. Internet Access 10.8. Dates and Times 10.9. Data Compression 10.10. Performance Measurement 10.11. Quality Control 10.12. Batteries Included 11. Brief Tour of the Standard Library — Part II 11.1. Output Formatting 11.2. Templating 11.3. Working with Binary Data Record Layouts 11.4. Multi-threading 11.5. Logging 11.6. Weak References 11.7. Tools for Working with Lists 11.8. Decimal Floating Point Arithmetic 12. Virtual Environments and Packages 12.1. Introduction 12.2. Creating Virtual Environments 12.3. Managing Packages with pip 13. What Now? 14. Interactive Input Editing and History Substitution 14.1. Tab Completion and History Editing 14.2. Alternatives to the Interactive Interpreter 15. Floating Point Arithmetic: Issues and Limitations 15.1. Representation Error 16. Appendix 16.1. Interactive Mode 16.1.1. Error Handling 16.1.2. Executable Python Scripts 16.1.3. The Interactive Startup File 16.1.4. The Customization Modules Python Setup and Usage 1. Command line and environment 1.1. Command line 1.1.1. Interface options 1.1.2. Generic options 1.1.3. Miscellaneous options 1.1.4. Options you shouldn’t use 1.2. Environment variables 1.2.1. Debug-mode variables 2. Using Python on Unix platforms 2.1. Getting and installing the latest version of Python 2.1.1. On Linux 2.1.2. On FreeBSD and OpenBSD 2.1.3. On OpenSolaris 2.2. Building Python 2.3. Python-related paths and files 2.4. Miscellaneous 2.5. Editors and IDEs 3. Using Python on Windows 3.1. Installing Python 3.1.1. Supported Versions 3.1.2. Installation Steps 3.1.3. Removing the MAX_PATH Limitation 3.1.4. Installing Without UI 3.1.5. Installing Without Downloading 3.1.6. Modifying an install 3.1.7. Other Platforms 3.2. Alternative bundles 3.3. Configuring Python 3.3.1. Excursus: Setting environment variables 3.3.2. Finding the Python executable 3.4. Python Launcher for Windows 3.4.1. Getting started 3.4.1.1. From the command-line 3.4.1.2. Virtual environments 3.4.1.3. From a script 3.4.1.4. From file associations 3.4.2. Shebang Lines 3.4.3. Arguments in shebang lines 3.4.4. Customization 3.4.4.1. Customization via INI files 3.4.4.2. Customizing default Python versions 3.4.5. Diagnostics 3.5. Finding modules 3.6. Additional modules 3.6.1. PyWin32 3.6.2. cx_Freeze 3.6.3. WConio 3.7. Compiling Python on Windows 3.8. Embedded Distribution 3.8.1. Python Application 3.8.2. Embedding Python 3.9. Other resources 4. Using Python on a Macintosh 4.1. Getting and Installing MacPython 4.1.1. How to run a Python script 4.1.2. Running scripts with a GUI 4.1.3. Configuration 4.2. The IDE 4.3. Installing Additional Python Packages 4.4. GUI Programming on the Mac 4.5. Distributing Python Applications on the Mac 4.6. Other Resources The Python Language Reference 1. Introduction 1.1. Alternate Implementations 1.2. Notation 2. Lexical analysis 2.1. Line structure 2.1.1. Logical lines 2.1.2. Physical lines 2.1.3. Comments 2.1.4. Encoding declarations 2.1.5. Explicit line joining 2.1.6. Implicit line joining 2.1.7. Blank lines 2.1.8. Indentation 2.1.9. Whitespace between tokens 2.2. Other tokens 2.3. Identifiers and keywords 2.3.1. Keywords 2.3.2. Reserved classes of identifiers 2.4. Literals 2.4.1. String and Bytes literals 2.4.2. String literal concatenation 2.4.3. Formatted string literals 2.4.4. Numeric literals 2.4.5. Integer literals 2.4.6. Floating point literals 2.4.7. Imaginary literals 2.5. Operators 2.6. Delimiters 3. Data model 3.1. Objects, values and types 3.2. The standard type hierarchy 3.3. Special method names 3.3.1. Basic customization 3.3.2. Customizing attribute access 3.3.2.1. Customizing module attribute access 3.3.2.2. Implementing Descriptors 3.3.2.3. Invoking Descriptors 3.3.2.4. __slots__ 3.3.2.4.1. Notes on using __slots__ 3.3.3. Customizing class creation 3.3.3.1. Metaclasses 3.3.3.2. Determining the appropriate metaclass 3.3.3.3. Preparing the class namespace 3.3.3.4. Executing the class body 3.3.3.5. Creating the class object 3.3.3.6. Metaclass example 3.3.4. Customizing instance and subclass checks 3.3.5. Emulating callable objects 3.3.6. Emulating container types 3.3.7. Emulating numeric types 3.3.8. With Statement Context Managers 3.3.9. Special method lookup 3.4. Coroutines 3.4.1. Awaitable Objects 3.4.2. Coroutine Objects 3.4.3. Asynchronous Iterators 3.4.4. Asynchronous Context Managers 4. Execution model 4.1. Structure of a program 4.2. Naming and binding 4.2.1. Binding of names 4.2.2. Resolution of names 4.2.3. Builtins and restricted execution 4.2.4. Interaction with dynamic features 4.3. Exceptions 5. The import system 5.1. importlib 5.2. Packages 5.2.1. Regular packages 5.2.2. Namespace packages 5.3. Searching 5.3.1. The module cache 5.3.2. Finders and loaders 5.3.3. Import hooks 5.3.4. The meta path 5.4. Loading 5.4.1. Loaders 5.4.2. Submodules 5.4.3. Module spec 5.4.4. Import-related module attributes 5.4.5. module.__path__ 5.4.6. Module reprs 5.5. The Path Based Finder 5.5.1. Path entry finders 5.5.2. Path entry finder protocol 5.6. Replacing the standard import system 5.7. Special considerations for __main__ 5.7.1. __main__.__spec__ 5.8. Open issues 5.9. References 6. Expressions 6.1. Arithmetic conversions 6.2. Atoms 6.2.1. Identifiers (Names) 6.2.2. Literals 6.2.3. Parenthesized forms 6.2.4. Displays for lists, sets and dictionaries 6.2.5. List displays 6.2.6. Set displays 6.2.7. Dictionary displays 6.2.8. Generator expressions 6.2.9. Yield expressions 6.2.9.1. Generator-iterator methods 6.2.9.2. Examples 6.2.9.3. Asynchronous generator functions 6.2.9.4. Asynchronous generator-iterator methods 6.3. Primaries 6.3.1. Attribute references 6.3.2. Subscriptions 6.3.3. Slicings 6.3.4. Calls 6.4. Await expression 6.5. The power operator 6.6. Unary arithmetic and bitwise operations 6.7. Binary arithmetic operations 6.8. Shifting operations 6.9. Binary bitwise operations 6.10. Comparisons 6.10.1. Value comparisons 6.10.2. Membership test operations 6.10.3. Identity comparisons 6.11. Boolean operations 6.12. Conditional expressions 6.13. Lambdas 6.14. Expression lists 6.15. Evaluation order 6.16. Operator precedence 7. Simple statements 7.1. Expression statements 7.2. Assignment statements 7.2.1. Augmented assignment statements 7.2.2. Annotated assignment statements 7.3. The assert statement 7.4. The pass statement 7.5. The del statement 7.6. The return statement 7.7. The yield statement 7.8. The raise statement 7.9. The break statement 7.10. The continue statement 7.11. The import statement 7.11.1. Future statements 7.12. The global statement 7.13. The nonlocal statement 8. Compound statements 8.1. The if statement 8.2. The while statement 8.3. The for statement 8.4. The try statement 8.5. The with statement 8.6. Function definitions 8.7. Class definitions 8.8. Coroutines 8.8.1. Coroutine function definition 8.8.2. The async for statement 8.8.3. The async with statement 9. Top-level components 9.1. Complete Python programs 9.2. File input 9.3. Interactive input 9.4. Expression input 10. Full Grammar specification The Python Standard Library 1. Introduction 2. Built-in Functions 3. Built-in Constants 3.1. Constants added by the site module 4. Built-in Types 4.1. Truth Value Testing 4.2. Boolean Operations — and, or, not 4.3. Comparisons 4.4. Numeric Types — int, float, complex 4.4.1. Bitwise Operations on Integer Types 4.4.2. Additional Methods on Integer Types 4.4.3. Additional Methods on Float 4.4.4. Hashing of numeric types 4.5. Iterator Types 4.5.1. Generator Types 4.6. Sequence Types — list, tuple, range 4.6.1. Common Sequence Operations 4.6.2. Immutable Sequence Types 4.6.3. Mutable Sequence Types 4.6.4. Lists 4.6.5. Tuples 4.6.6. Ranges 4.7. Text Sequence Type — str 4.7.1. String Methods 4.7.2. printf-style String Formatting 4.8. Binary Sequence Types — bytes, bytearray, memoryview 4.8.1. Bytes Objects 4.8.2. Bytearray Objects 4.8.3. Bytes and Bytearray Operations 4.8.4. printf-style Bytes Formatting 4.8.5. Memory Views 4.9. Set Types — set, frozenset 4.10. Mapping Types — dict 4.10.1. Dictionary view objects 4.11. Context Manager Types 4.12. Other Built-in Types 4.12.1. Modules 4.12.2. Classes and Class Instances 4.12.3. Functions 4.12.4. Methods 4.12.5. Code Objects 4.12.6. Type Objects 4.12.7. The Null Object 4.12.8. The Ellipsis Object 4.12.9. The NotImplemented Object 4.12.10. Boolean Values 4.12.11. Internal Objects 4.13. Special Attributes 5. Built-in Exceptions 5.1. Base classes 5.2. Concrete exceptions 5.2.1. OS exceptions 5.3. Warnings 5.4. Exception hierarchy 6. Text Processing Services 6.1. string — Common string operations 6.1.1. String constants 6.1.2. Custom String Formatting 6.1.3. Format String Syntax 6.1.3.1. Format Specification Mini-Language 6.1.3.2. Format examples 6.1.4. Template strings 6.1.5. Helper functions 6.2. re — Regular expression operations 6.2.1. Regular Expression Syntax 6.2.2. Module Contents 6.2.3. Regular Expression Objects 6.2.4. Match Objects 6.2.5. Regular Expression Examples 6.2.5.1. Checking for a Pair 6.2.5.2. Simulating scanf() 6.2.5.3. search() vs. match() 6.2.5.4. Making a Phonebook 6.2.5.5. Text Munging 6.2.5.6. Finding all Adverbs 6.2.5.7. Finding all Adverbs and their Positions 6.2.5.8. Raw String Notation 6.2.5.9. Writing a Tokenizer 6.3. difflib — Helpers for computing deltas 6.3.1. SequenceMatcher Objects 6.3.2. SequenceMatcher Examples 6.3.3. Differ Objects 6.3.4. Differ Example 6.3.5. A command-line interface to difflib 6.4. textwrap — Text wrapping and filling 6.5. unicodedata — Unicode Database 6.6. stringprep — Internet String Preparation 6.7. readline — GNU readline interface 6.7.1. Init file 6.7.2. Line buffer 6.7.3. History file 6.7.4. History list 6.7.5. Startup hooks 6.7.6. Completion 6.7.7. Example 6.8. rlcompleter — Completion function for GNU readline 6.8.1. Completer Objects 7. Binary Data Services 7.1. struct — Interpret bytes as packed binary data 7.1.1. Functions and Exceptions 7.1.2. Format Strings 7.1.2.1. Byte Order, Size, and Alignment 7.1.2.2. Format Characters 7.1.2.3. Examples 7.1.3. Classes 7.2. codecs — Codec registry and base classes 7.2.1. Codec Base Classes 7.2.1.1. Error Handlers 7.2.1.2. Stateless Encoding and Decoding 7.2.1.3. Incremental Encoding and Decoding 7.2.1.3.1. IncrementalEncoder Objects 7.2.1.3.2. IncrementalDecoder Objects 7.2.1.4. Stream Encoding and Decoding 7.2.1.4.1. StreamWriter Objects 7.2.1.4.2. StreamReader Objects 7.2.1.4.3. StreamReaderWriter Objects 7.2.1.4.4. StreamRecoder Objects 7.2.2. Encodings and Unicode 7.2.3. Standard Encodings 7.2.4. Python Specific Encodings 7.2.4.1. Text Encodings 7.2.4.2. Binary Transforms 7.2.4.3. Text Transforms 7.2.5. encodings.idna — Internationalized Domain Names in Applications 7.2.6. encodings.mbcs — Windows ANSI codepage 7.2.7. encodings.utf_8_sig — UTF-8 codec with BOM signature 8. Data Types 8.1. datetime — Basic date and time types 8.1.1. Available Types 8.1.2. timedelta Objects 8.1.3. date Objects 8.1.4. datetime Objects 8.1.5. time Objects 8.1.6. tzinfo Objects 8.1.7. timezone Objects 8.1.8. strftime() and strptime() Behavior 8.2. calendar — General calendar-related functions 8.3. collections — Container datatypes 8.3.1. ChainMap objects 8.3.1.1. ChainMap Examples and Recipes 8.3.2. Counter objects 8.3.3. deque objects 8.3.3.1. deque Recipes 8.3.4. defaultdict objects 8.3.4.1. defaultdict Examples 8.3.5. namedtuple() Factory Function for Tuples with Named Fields 8.3.6. OrderedDict objects 8.3.6.1. OrderedDict Examples and Recipes 8.3.7. UserDict objects 8.3.8. UserList objects 8.3.9. UserString objects 8.4. collections.abc — Abstract Base Classes for Containers 8.4.1. Collections Abstract Base Classes 8.5. heapq — Heap queue algorithm 8.5.1. Basic Examples 8.5.2. Priority Queue Implementation Notes 8.5.3. Theory 8.6. bisect — Array bisection algorithm 8.6.1. Searching Sorted Lists 8.6.2. Other Examples 8.7. array — Efficient arrays of numeric values 8.8. weakref — Weak references 8.8.1. Weak Reference Objects 8.8.2. Example 8.8.3. Finalizer Objects 8.8.4. Comparing finalizers with __del__() methods 8.9. types — Dynamic type creation and names for built-in types 8.9.1. Dynamic Type Creation 8.9.2. Standard Interpreter Types 8.9.3. Additional Utility Classes and Functions 8.9.4. Coroutine Utility Functions 8.10. copy — Shallow and deep copy operations 8.11. pprint — Data pretty printer 8.11.1. PrettyPrinter Objects 8.11.2. Example 8.12. reprlib — Alternate repr() implementation 8.12.1. Repr Objects 8.12.2. Subclassing Repr Objects 8.13. enum — Support for enumerations 8.13.1. Module Contents 8.13.2. Creating an Enum 8.13.3. Programmatic access to enumeration members and their attributes 8.13.4. Duplicating enum members and values 8.13.5. Ensuring unique enumeration values 8.13.6. Using automatic values 8.13.7. Iteration 8.13.8. Comparisons 8.13.9. Allowed members and attributes of enumerations 8.13.10. Restricted subclassing of enumerations 8.13.11. Pickling 8.13.12. Functional API 8.13.13. Derived Enumerations 8.13.13.1. IntEnum 8.13.13.2. IntFlag 8.13.13.3. Flag 8.13.13.4. Others 8.13.14. Interesting examples 8.13.14.1. Omitting values 8.13.14.1.1. Using auto 8.13.14.1.2. Using object 8.13.14.1.3. Using a descriptive string 8.13.14.1.4. Using a custom __new__() 8.13.14.2. OrderedEnum 8.13.14.3. DuplicateFreeEnum 8.13.14.4. Planet 8.13.15. How are Enums different? 8.13.15.1. Enum Classes 8.13.15.2. Enum Members (aka instances) 8.13.15.3. Finer Points 8.13.15.3.1. Supported __dunder__ names 8.13.15.3.2. Supported _sunder_ names 8.13.15.3.3. Enum member type 8.13.15.3.4. Boolean value of Enum classes and members 8.13.15.3.5. Enum classes with methods 8.13.15.3.6. Combining members of Flag 9. Numeric and Mathematical Modules 9.1. numbers — Numeric abstract base classes 9.1.1. The numeric tower 9.1.2. Notes for type implementors 9.1.2.1. Adding More Numeric ABCs 9.1.2.2. Implementing the arithmetic operations 9.2. math — Mathematical functions 9.2.1. Number-theoretic and representation functions 9.2.2. Power and logarithmic functions 9.2.3. Trigonometric functions 9.2.4. Angular conversion 9.2.5. Hyperbolic functions 9.2.6. Special functions 9.2.7. Constants 9.3. cmath — Mathematical functions for complex numbers 9.3.1. Conversions to and from polar coordinates 9.3.2. Power and logarithmic functions 9.3.3. Trigonometric functions 9.3.4. Hyperbolic functions 9.3.5. Classification functions 9.3.6. Constants 9.4. decimal — Decimal fixed point and floating point arithmetic 9.4.1. Quick-start Tutorial 9.4.2. Decimal objects 9.4.2.1. Logical operands 9.4.3. Context objects 9.4.4. Constants 9.4.5. Rounding modes 9.4.6. Signals 9.4.7. Floating Point Notes 9.4.7.1. Mitigating round-off error with increased precision 9.4.7.2. Special values 9.4.8. Working with threads 9.4.9. Recipes 9.4.10. Decimal FAQ 9.5. fractions — Rational numbers 9.6. random — Generate pseudo-random numbers 9.6.1. Bookkeeping functions 9.6.2. Functions for integers 9.6.3. Functions for sequences 9.6.4. Real-valued distributions 9.6.5. Alternative Generator 9.6.6. Notes on Reproducibility 9.6.7. Examples and Recipes 9.7. statistics — Mathematical statistics functions 9.7.1. Averages and measures of central location 9.7.2. Measures of spread 9.7.3. Function details 9.7.4. Exceptions 10. Functional Programming Modules 10.1. itertools — Functions creating iterators for efficient looping 10.1.1. Itertool functions 10.1.2. Itertools Recipes 10.2. functools — Higher-order functions and operations on callable objects 10.2.1. partial Objects 10.3. operator — Standard operators as functions 10.3.1. Mapping Operators to Functions 10.3.2. Inplace Operators 11. File and Directory Access 11.1. pathlib — Object-oriented filesystem paths 11.1.1. Basic use 11.1.2. Pure paths 11.1.2.1. General properties 11.1.2.2. Operators 11.1.2.3. Accessing individual parts 11.1.2.4. Methods and properties 11.1.3. Concrete paths 11.1.3.1. Methods 11.2. os.path — Common pathname manipulations 11.3. fileinput — Iterate over lines from multiple input streams 11.4. stat — Interpreting stat() results 11.5. filecmp — File and Directory Comparisons 11.5.1. The dircmp class 11.6. tempfile — Generate temporary files and directories 11.6.1. Examples 11.6.2. Deprecated functions and variables 11.7. glob — Unix style pathname pattern expansion 11.8. fnmatch — Unix filename pattern matching 11.9. linecache — Random access to text lines 11.10. shutil — High-level file operations 11.10.1. Directory and files operations 11.10.1.1. copytree example 11.10.1.2. rmtree example 11.10.2. Archiving operations 11.10.2.1. Archiving example 11.10.3. Querying the size of the output terminal 11.11. macpath — Mac OS 9 path manipulation functions 12. Data Persistence 12.1. pickle — Python object serialization 12.1.1. Relationship to other Python modules 12.1.1.1. Comparison with marshal 12.1.1.2. Comparison with json 12.1.2. Data stream format 12.1.3. Module Interface 12.1.4. What can be pickled and unpickled? 12.1.5. Pickling Class Instances 12.1.5.1. Persistence of External Objects 12.1.5.2. Dispatch Tables 12.1.5.3. Handling Stateful Objects 12.1.6. Restricting Globals 12.1.7. Performance 12.1.8. Examples 12.2. copyreg — Register pickle support functions 12.2.1. Example 12.3. shelve — Python object persistence 12.3.1. Restrictions 12.3.2. Example 12.4. marshal — Internal Python object serialization 12.5. dbm — Interfaces to Unix “databases” 12.5.1. dbm.gnu — GNU’s reinterpretation of dbm 12.5.2. dbm.ndbm — Interface based on ndbm 12.5.3. dbm.dumb — Portable DBM implementation 12.6. sqlite3 — DB-API 2.0 interface for SQLite databases 12.6.1. Module functions and constants 12.6.2. Connection Objects 12.6.3. Cursor Objects 12.6.4. Row Objects 12.6.5. Exceptions 12.6.6. SQLite and Python types 12.6.6.1. Introduction 12.6.6.2. Using adapters to store additional Python types in SQLite databases 12.6.6.2.1. Letting your object adapt itself 12.6.6.2.2. Registering an adapter callable 12.6.6.3. Converting SQLite values to custom Python types 12.6.6.4. Default adapters and converters 12.6.7. Controlling Transactions 12.6.8. Using sqlite3 efficiently 12.6.8.1. Using shortcut methods 12.6.8.2. Accessing columns by name instead of by index 12.6.8.3. Using the connection as a context manager 12.6.9. Common issues 12.6.9.1. Multithreading 13. Data Compression and Archiving 13.1. zlib — Compression compatible with gzip 13.2. gzip — Support for gzip files 13.2.1. Examples of usage 13.3. bz2 — Support for bzip2 compression 13.3.1. (De)compression of files 13.3.2. Incremental (de)compression 13.3.3. One-shot (de)compression 13.4. lzma — Compression using the LZMA algorithm 13.4.1. Reading and writing compressed files 13.4.2. Compressing and decompressing data in memory 13.4.3. Miscellaneous 13.4.4. Specifying custom filter chains 13.4.5. Examples 13.5. zipfile — Work with ZIP archives 13.5.1. ZipFile Objects 13.5.2. PyZipFile Objects 13.5.3. ZipInfo Objects 13.5.4. Command-Line Interface 13.5.4.1. Command-line options 13.6. tarfile — Read and write tar archive files 13.6.1. TarFile Objects 13.6.2. TarInfo Objects 13.6.3. Command-Line Interface 13.6.3.1. Command-line options 13.6.4. Examples 13.6.5. Supported tar formats 13.6.6. Unicode issues 14. File Formats 14.1. csv — CSV File Reading and Writing 14.1.1. Module Contents 14.1.2. Dialects and Formatting Parameters 14.1.3. Reader Objects 14.1.4. Writer Objects 14.1.5. Examples 14.2. configparser — Configuration file parser 14.2.1. Quick Start 14.2.2. Supported Datatypes 14.2.3. Fallback Values 14.2.4. Supported INI File Structure 14.2.5. Interpolation of values 14.2.6. Mapping Protocol Access 14.2.7. Customizing Parser Behaviour 14.2.8. Legacy API Examples 14.2.9. ConfigParser Objects 14.2.10. RawConfigParser Objects 14.2.11. Exceptions 14.3. netrc — netrc file processing 14.3.1. netrc Objects 14.4. xdrlib — Encode and decode XDR data 14.4.1. Packer Objects 14.4.2. Unpacker Objects 14.4.3. Exceptions 14.5. plistlib — Generate and parse Mac OS X .plist files 14.5.1. Examples 15. Cryptographic Services 15.1. hashlib — Secure hashes and message digests 15.1.1. Hash algorithms 15.1.2. SHAKE variable length digests 15.1.3. Key derivation 15.1.4. BLAKE2 15.1.4.1. Creating hash objects 15.1.4.2. Constants 15.1.4.3. Examples 15.1.4.3.1. Simple hashing 15.1.4.3.2. Using different digest sizes 15.1.4.3.3. Keyed hashing 15.1.4.3.4. Randomized hashing 15.1.4.3.5. Personalization 15.1.4.3.6. Tree mode 15.1.4.4. Credits 15.2. hmac — Keyed-Hashing for Message Authentication 15.3. secrets — Generate secure random numbers for managing secrets 15.3.1. Random numbers 15.3.2. Generating tokens 15.3.2.1. How many bytes should tokens use? 15.3.3. Other functions 15.3.4. Recipes and best practices 16. Generic Operating System Services 16.1. os — Miscellaneous operating system interfaces 16.1.1. File Names, Command Line Arguments, and Environment Variables 16.1.2. Process Parameters 16.1.3. File Object Creation 16.1.4. File Descriptor Operations 16.1.4.1. Querying the size of a terminal 16.1.4.2. Inheritance of File Descriptors 16.1.5. Files and Directories 16.1.5.1. Linux extended attributes 16.1.6. Process Management 16.1.7. Interface to the scheduler 16.1.8. Miscellaneous System Information 16.1.9. Random numbers 16.2. io — Core tools for working with streams 16.2.1. Overview 16.2.1.1. Text I/O 16.2.1.2. Binary I/O 16.2.1.3. Raw I/O 16.2.2. High-level Module Interface 16.2.2.1. In-memory streams 16.2.3. Class hierarchy 16.2.3.1. I/O Base Classes 16.2.3.2. Raw File I/O 16.2.3.3. Buffered Streams 16.2.3.4. Text I/O 16.2.4. Performance 16.2.4.1. Binary I/O 16.2.4.2. Text I/O 16.2.4.3. Multi-threading 16.2.4.4. Reentrancy 16.3. time — Time access and conversions 16.3.1. Functions 16.3.2. Clock ID Constants 16.3.3. Timezone Constants 16.4. argparse — Parser for command-line options, arguments and sub-commands 16.4.1. Example 16.4.1.1. Creating a parser 16.4.1.2. Adding arguments 16.4.1.3. Parsing arguments 16.4.2. ArgumentParser objects 16.4.2.1. prog 16.4.2.2. usage 16.4.2.3. description 16.4.2.4. epilog 16.4.2.5. parents 16.4.2.6. formatter_class 16.4.2.7. prefix_chars 16.4.2.8. fromfile_prefix_chars 16.4.2.9. argument_default 16.4.2.10. allow_abbrev 16.4.2.11. conflict_handler 16.4.2.12. add_help 16.4.3. The add_argument() method 16.4.3.1. name or flags 16.4.3.2. action 16.4.3.3. nargs 16.4.3.4. const 16.4.3.5. default 16.4.3.6. type 16.4.3.7. choices 16.4.3.8. required 16.4.3.9. help 16.4.3.10. metavar 16.4.3.11. dest 16.4.3.12. Action classes 16.4.4. The parse_args() method 16.4.4.1. Option value syntax 16.4.4.2. Invalid arguments 16.4.4.3. Arguments containing - 16.4.4.4. Argument abbreviations (prefix matching) 16.4.4.5. Beyond sys.argv 16.4.4.6. The Namespace object 16.4.5. Other utilities 16.4.5.1. Sub-commands 16.4.5.2. FileType objects 16.4.5.3. Argument groups 16.4.5.4. Mutual exclusion 16.4.5.5. Parser defaults 16.4.5.6. Printing help 16.4.5.7. Partial parsing 16.4.5.8. Customizing file parsing 16.4.5.9. Exiting methods 16.4.6. Upgrading optparse code 16.5. getopt — C-style parser for command line options 16.6. logging — Logging facility for Python 16.6.1. Logger Objects 16.6.2. Logging Levels 16.6.3. Handler Objects 16.6.4. Formatter Objects 16.6.5. Filter Objects 16.6.6. LogRecord Objects 16.6.7. LogRecord attributes 16.6.8. LoggerAdapter Objects 16.6.9. Thread Safety 16.6.10. Module-Level Functions 16.6.11. Module-Level Attributes 16.6.12. Integration with the warnings module 16.7. logging.config — Logging configuration 16.7.1. Configuration functions 16.7.2. Configuration dictionary schema 16.7.2.1. Dictionary Schema Details 16.7.2.2. Incremental Configuration 16.7.2.3. Object connections 16.7.2.4. User-defined objects 16.7.2.5. Access to external objects 16.7.2.6. Access to internal objects 16.7.2.7. Import resolution and custom importers 16.7.3. Configuration file format 16.8. logging.handlers — Logging handlers 16.8.1. StreamHandler 16.8.2. FileHandler 16.8.3. NullHandler 16.8.4. WatchedFileHandler 16.8.5. BaseRotatingHandler 16.8.6. RotatingFileHandler 16.8.7. TimedRotatingFileHandler 16.8.8. SocketHandler 16.8.9. DatagramHandler 16.8.10. SysLogHandler 16.8.11. NTEventLogHandler 16.8.12. SMTPHandler 16.8.13. MemoryHandler 16.8.14. HTTPHandler 16.8.15. QueueHandler 16.8.16. QueueListener 16.9. getpass — Portable password input 16.10. curses — Terminal handling for character-cell displays 16.10.1. Functions 16.10.2. Window Objects 16.10.3. Constants 16.11. curses.textpad — Text input widget for curses programs 16.11.1. Textbox objects 16.12. curses.ascii — Utilities for ASCII characters 16.13. curses.panel — A panel stack extension for curses 16.13.1. Functions 16.13.2. Panel Objects 16.14. platform — Access to underlying platform’s identifying data 16.14.1. Cross Platform 16.14.2. Java Platform 16.14.3. Windows Platform 16.14.3.1. Win95/98 specific 16.14.4. Mac OS Platform 16.14.5. Unix Platforms 16.15. errno — Standard errno system symbols 16.16. ctypes — A foreign function library for Python 16.16.1. ctypes tutorial 16.16.1.1. Loading dynamic link libraries 16.16.1.2. Accessing functions from loaded dlls 16.16.1.3. Calling functions 16.16.1.4. Fundamental data types 16.16.1.5. Calling functions, continued 16.16.1.6. Calling functions with your own custom data types 16.16.1.7. Specifying the required argument types (function prototypes) 16.16.1.8. Return types 16.16.1.9. Passing pointers (or: passing parameters by reference) 16.16.1.10. Structures and unions 16.16.1.11. Structure/union alignment and byte order 16.16.1.12. Bit fields in structures and unions 16.16.1.13. Arrays 16.16.1.14. Pointers 16.16.1.15. Type conversions 16.16.1.16. Incomplete Types 16.16.1.17. Callback functions 16.16.1.18. Accessing values exported from dlls 16.16.1.19. Surprises 16.16.1.20. Variable-sized data types 16.16.2. ctypes reference 16.16.2.1. Finding shared libraries 16.16.2.2. Loading shared libraries 16.16.2.3. Foreign functions 16.16.2.4. Function prototypes 16.16.2.5. Utility functions 16.16.2.6. Data types 16.16.2.7. Fundamental data types 16.16.2.8. Structured data types 16.16.2.9. Arrays and pointers 17. Concurrent Execution 17.1. threading — Thread-based parallelism 17.1.1. Thread-Local Data 17.1.2. Thread Objects 17.1.3. Lock Objects 17.1.4. RLock Objects 17.1.5. Condition Objects 17.1.6. Semaphore Objects 17.1.6.1. Semaphore Example 17.1.7. Event Objects 17.1.8. Timer Objects 17.1.9. Barrier Objects 17.1.10. Using locks, conditions, and semaphores in the with statement 17.2. multiprocessing — Process-based parallelism 17.2.1. Introduction 17.2.1.1. The Process class 17.2.1.2. Contexts and start methods 17.2.1.3. Exchanging objects between processes 17.2.1.4. Synchronization between processes 17.2.1.5. Sharing state between processes 17.2.1.6. Using a pool of workers 17.2.2. Reference 17.2.2.1. Process and exceptions 17.2.2.2. Pipes and Queues 17.2.2.3. Miscellaneous 17.2.2.4. Connection Objects 17.2.2.5. Synchronization primitives 17.2.2.6. Shared ctypes Objects 17.2.2.6.1. The multiprocessing.sharedctypes module 17.2.2.7. Managers 17.2.2.7.1. Customized managers 17.2.2.7.2. Using a remote manager 17.2.2.8. Proxy Objects 17.2.2.8.1. Cleanup 17.2.2.9. Process Pools 17.2.2.10. Listeners and Clients 17.2.2.10.1. Address Formats 17.2.2.11. Authentication keys 17.2.2.12. Logging 17.2.2.13. The multiprocessing.dummy module 17.2.3. Programming guidelines 17.2.3.1. All start methods 17.2.3.2. The spawn and forkserver start methods 17.2.4. Examples 17.3. The concurrent package 17.4. concurrent.futures — Launching parallel tasks 17.4.1. Executor Objects 17.4.2. ThreadPoolExecutor 17.4.2.1. ThreadPoolExecutor Example 17.4.3. ProcessPoolExecutor 17.4.3.1. ProcessPoolExecutor Example 17.4.4. Future Objects 17.4.5. Module Functions 17.4.6. Exception classes 17.5. subprocess — Subprocess management 17.5.1. Using the subprocess Module 17.5.1.1. Frequently Used Arguments 17.5.1.2. Popen Constructor 17.5.1.3. Exceptions 17.5.2. Security Considerations 17.5.3. Popen Objects 17.5.4. Windows Popen Helpers 17.5.4.1. Constants 17.5.5. Older high-level API 17.5.6. Replacing Older Functions with the subprocess Module 17.5.6.1. Replacing /bin/sh shell backquote 17.5.6.2. Replacing shell pipeline 17.5.6.3. Replacing os.system() 17.5.6.4. Replacing the os.spawn family 17.5.6.5. Replacing os.popen(), os.popen2(), os.popen3() 17.5.6.6. Replacing functions from the popen2 module 17.5.7. Legacy Shell Invocation Functions 17.5.8. Notes 17.5.8.1. Converting an argument sequence to a string on Windows 17.6. sched — Event scheduler 17.6.1. Scheduler Objects 17.7. queue — A synchronized queue class 17.7.1. Queue Objects 17.8. dummy_threading — Drop-in replacement for the threading module 17.9. _thread — Low-level threading API 17.10. _dummy_thread — Drop-in replacement for the _thread module 18. Interprocess Communication and Networking 18.1. socket — Low-level networking interface 18.1.1. Socket families 18.1.2. Module contents 18.1.2.1. Exceptions 18.1.2.2. Constants 18.1.2.3. Functions 18.1.2.3.1. Creating sockets 18.1.2.3.2. Other functions 18.1.3. Socket Objects 18.1.4. Notes on socket timeouts 18.1.4.1. Timeouts and the connect method 18.1.4.2. Timeouts and the accept method 18.1.5. Example 18.2. ssl — TLS/SSL wrapper for socket objects 18.2.1. Functions, Constants, and Exceptions 18.2.1.1. Socket creation 18.2.1.2. Context creation 18.2.1.3. Random generation 18.2.1.4. Certificate handling 18.2.1.5. Constants 18.2.2. SSL Sockets 18.2.3. SSL Contexts 18.2.4. Certificates 18.2.4.1. Certificate chains 18.2.4.2. CA certificates 18.2.4.3. Combined key and certificate 18.2.4.4. Self-signed certificates 18.2.5. Examples 18.2.5.1. Testing for SSL support 18.2.5.2. Client-side operation 18.2.5.3. Server-side operation 18.2.6. Notes on non-blocking sockets 18.2.7. Memory BIO Support 18.2.8. SSL session 18.2.9. Security considerations 18.2.9.1. Best defaults 18.2.9.2. Manual settings 18.2.9.2.1. Verifying certificates 18.2.9.2.2. Protocol versions 18.2.9.2.3. Cipher selection 18.2.9.3. Multi-processing 18.2.10. LibreSSL support 18.3. select — Waiting for I/O completion 18.3.1. /dev/poll Polling Objects 18.3.2. Edge and Level Trigger Polling (epoll) Objects 18.3.3. Polling Objects 18.3.4. Kqueue Objects 18.3.5. Kevent Objects 18.4. selectors — High-level I/O multiplexing 18.4.1. Introduction 18.4.2. Classes 18.4.3. Examples 18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks 18.5.1. Base Event Loop 18.5.1.1. Run an event loop 18.5.1.2. Calls 18.5.1.3. Delayed calls 18.5.1.4. Futures 18.5.1.5. Tasks 18.5.1.6. Creating connections 18.5.1.7. Creating listening connections 18.5.1.8. Watch file descriptors 18.5.1.9. Low-level socket operations 18.5.1.10. Resolve host name 18.5.1.11. Connect pipes 18.5.1.12. UNIX signals 18.5.1.13. Executor 18.5.1.14. Error Handling API 18.5.1.15. Debug mode 18.5.1.16. Server 18.5.1.17. Handle 18.5.1.18. Event loop examples 18.5.1.18.1. Hello World with call_soon() 18.5.1.18.2. Display the current date with call_later() 18.5.1.18.3. Watch a file descriptor for read events 18.5.1.18.4. Set signal handlers for SIGINT and SIGTERM 18.5.2. Event loops 18.5.2.1. Event loop functions 18.5.2.2. Available event loops 18.5.2.3. Platform support 18.5.2.3.1. Windows 18.5.2.3.2. Mac OS X 18.5.2.4. Event loop policies and the default policy 18.5.2.5. Event loop policy interface 18.5.2.6. Access to the global loop policy 18.5.2.7. Customizing the event loop policy 18.5.3. Tasks and coroutines 18.5.3.1. Coroutines 18.5.3.1.1. Example: Hello World coroutine 18.5.3.1.2. Example: Coroutine displaying the current date 18.5.3.1.3. Example: Chain coroutines 18.5.3.2. InvalidStateError 18.5.3.3. TimeoutError 18.5.3.4. Future 18.5.3.4.1. Example: Future with run_until_complete() 18.5.3.4.2. Example: Future with run_forever() 18.5.3.5. Task 18.5.3.5.1. Example: Parallel execution of tasks 18.5.3.6. Task functions 18.5.4. Transports and protocols (callback based API) 18.5.4.1. Transports 18.5.4.1.1. BaseTransport 18.5.4.1.2. ReadTransport 18.5.4.1.3. WriteTransport 18.5.4.1.4. DatagramTransport 18.5.4.1.5. BaseSubprocessTransport 18.5.4.2. Protocols 18.5.4.2.1. Protocol classes 18.5.4.2.2. Connection callbacks 18.5.4.2.3. Streaming protocols 18.5.4.2.4. Datagram protocols 18.5.4.2.5. Flow control callbacks 18.5.4.2.6. Coroutines and protocols 18.5.4.3. Protocol examples 18.5.4.3.1. TCP echo client protocol 18.5.4.3.2. TCP echo server protocol 18.5.4.3.3. UDP echo client protocol 18.5.4.3.4. UDP echo server protocol 18.5.4.3.5. Register an open socket to wait for data using a protocol 18.5.5. Streams (coroutine based API) 18.5.5.1. Stream functions 18.5.5.2. StreamReader 18.5.5.3. StreamWriter 18.5.5.4. StreamReaderProtocol 18.5.5.5. IncompleteReadError 18.5.5.6. LimitOverrunError 18.5.5.7. Stream examples 18.5.5.7.1. TCP echo client using streams 18.5.5.7.2. TCP echo server using streams 18.5.5.7.3. Get HTTP headers 18.5.5.7.4. Register an open socket to wait for data using streams 18.5.6. Subprocess 18.5.6.1. Windows event loop 18.5.6.2. Create a subprocess: high-level API using Process 18.5.6.3. Create a subprocess: low-level API using subprocess.Popen 18.5.6.4. Constants 18.5.6.5. Process 18.5.6.6. Subprocess and threads 18.5.6.7. Subprocess examples 18.5.6.7.1. Subprocess using transport and protocol 18.5.6.7.2. Subprocess using streams 18.5.7. Synchronization primitives 18.5.7.1. Locks 18.5.7.1.1. Lock 18.5.7.1.2. Event 18.5.7.1.3. Condition 18.5.7.2. Semaphores 18.5.7.2.1. Semaphore 18.5.7.2.2. BoundedSemaphore 18.5.8. Queues 18.5.8.1. Queue 18.5.8.2. PriorityQueue 18.5.8.3. LifoQueue 18.5.8.3.1. Exceptions 18.5.9. Develop with asyncio 18.5.9.1. Debug mode of asyncio 18.5.9.2. Cancellation 18.5.9.3. Concurrency and multithreading 18.5.9.4. Handle blocking functions correctly 18.5.9.5. Logging 18.5.9.6. Detect coroutine objects never scheduled 18.5.9.7. Detect exceptions never consumed 18.5.9.8. Chain coroutines correctly 18.5.9.9. Pending task destroyed 18.5.9.10. Close transports and event loops 18.6. asyncore — Asynchronous socket handler 18.6.1. asyncore Example basic HTTP client 18.6.2. asyncore Example basic echo server 18.7. asynchat — Asynchronous socket command/response handler 18.7.1. asynchat Example 18.8. signal — Set handlers for asynchronous events 18.8.1. General rules 18.8.1.1. Execution of Python signal handlers 18.8.1.2. Signals and threads 18.8.2. Module contents 18.8.3. Example 18.9. mmap — Memory-mapped file support 19. Internet Data Handling 19.1. email — An email and MIME handling package 19.1.1. email.message: Representing an email message 19.1.2. email.parser: Parsing email messages 19.1.2.1. FeedParser API 19.1.2.2. Parser API 19.1.2.3. Additional notes 19.1.3. email.generator: Generating MIME documents 19.1.4. email.policy: Policy Objects 19.1.5. email.errors: Exception and Defect classes 19.1.6. email.headerregistry: Custom Header Objects 19.1.7. email.contentmanager: Managing MIME Content 19.1.7.1. Content Manager Instances 19.1.8. email: Examples 19.1.9. email.message.Message: Representing an email message using the compat32 API 19.1.10. email.mime: Creating email and MIME objects from scratch 19.1.11. email.header: Internationalized headers 19.1.12. email.charset: Representing character sets 19.1.13. email.encoders: Encoders 19.1.14. email.utils: Miscellaneous utilities 19.1.15. email.iterators: Iterators 19.2. json — JSON encoder and decoder 19.2.1. Basic Usage 19.2.2. Encoders and Decoders 19.2.3. Exceptions 19.2.4. Standard Compliance and Interoperability 19.2.4.1. Character Encodings 19.2.4.2. Infinite and NaN Number Values 19.2.4.3. Repeated Names Within an Object 19.2.4.4. Top-level Non-Object, Non-Array Values 19.2.4.5. Implementation Limitations 19.2.5. Command Line Interface 19.2.5.1. Command line options 19.3. mailcap — Mailcap file handling 19.4. mailbox — Manipulate mailboxes in various formats 19.4.1. Mailbox objects 19.4.1.1. Maildir 19.4.1.2. mbox 19.4.1.3. MH 19.4.1.4. Babyl 19.4.1.5. MMDF 19.4.2. Message objects 19.4.2.1. MaildirMessage 19.4.2.2. mboxMessage 19.4.2.3. MHMessage 19.4.2.4. BabylMessage 19.4.2.5. MMDFMessage 19.4.3. Exceptions 19.4.4. Examples 19.5. mimetypes — Map filenames to MIME types 19.5.1. MimeTypes Objects 19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings 19.7. binhex — Encode and decode binhex4 files 19.7.1. Notes 19.8. binascii — Convert between binary and ASCII 19.9. quopri — Encode and decode MIME quoted-printable data 19.10. uu — Encode and decode uuencode files 20. Structured Markup Processing Tools 20.1. html — HyperText Markup Language support 20.2. html.parser — Simple HTML and XHTML parser 20.2.1. Example HTML Parser Application 20.2.2. HTMLParser Methods 20.2.3. Examples 20.3. html.entities — Definitions of HTML general entities 20.4. XML Processing Modules 20.4.1. XML vulnerabilities 20.4.2. The defusedxml and defusedexpat Packages 20.5. xml.etree.ElementTree — The ElementTree XML API 20.5.1. Tutorial 20.5.1.1. XML tree and elements 20.5.1.2. Parsing XML 20.5.1.3. Pull API for non-blocking parsing 20.5.1.4. Finding interesting elements 20.5.1.5. Modifying an XML File 20.5.1.6. Building XML documents 20.5.1.7. Parsing XML with Namespaces 20.5.1.8. Additional resources 20.5.2. XPath support 20.5.2.1. Example 20.5.2.2. Supported XPath syntax 20.5.3. Reference 20.5.3.1. Functions 20.5.3.2. Element Objects 20.5.3.3. ElementTree Objects 20.5.3.4. QName Objects 20.5.3.5. TreeBuilder Objects 20.5.3.6. XMLParser Objects 20.5.3.7. XMLPullParser Objects 20.5.3.8. Exceptions 20.6. xml.dom — The Document Object Model API 20.6.1. Module Contents 20.6.2. Objects in the DOM 20.6.2.1. DOMImplementation Objects 20.6.2.2. Node Objects 20.6.2.3. NodeList Objects 20.6.2.4. DocumentType Objects 20.6.2.5. Document Objects 20.6.2.6. Element Objects 20.6.2.7. Attr Objects 20.6.2.8. NamedNodeMap Objects 20.6.2.9. Comment Objects 20.6.2.10. Text and CDATASection Objects 20.6.2.11. ProcessingInstruction Objects 20.6.2.12. Exceptions 20.6.3. Conformance 20.6.3.1. Type Mapping 20.6.3.2. Accessor Methods 20.7. xml.dom.minidom — Minimal DOM implementation 20.7.1. DOM Objects 20.7.2. DOM Example 20.7.3. minidom and the DOM standard 20.8. xml.dom.pulldom — Support for building partial DOM trees 20.8.1. DOMEventStream Objects 20.9. xml.sax — Support for SAX2 parsers 20.9.1. SAXException Objects 20.10. xml.sax.handler — Base classes for SAX handlers 20.10.1. ContentHandler Objects 20.10.2. DTDHandler Objects 20.10.3. EntityResolver Objects 20.10.4. ErrorHandler Objects 20.11. xml.sax.saxutils — SAX Utilities 20.12. xml.sax.xmlreader — Interface for XML parsers 20.12.1. XMLReader Objects 20.12.2. IncrementalParser Objects 20.12.3. Locator Objects 20.12.4. InputSource Objects 20.12.5. The Attributes Interface 20.12.6. The AttributesNS Interface 20.13. xml.parsers.expat — Fast XML parsing using Expat 20.13.1. XMLParser Objects 20.13.2. ExpatError Exceptions 20.13.3. Example 20.13.4. Content Model Descriptions 20.13.5. Expat error constants 21. Internet Protocols and Support 21.1. webbrowser — Convenient Web-browser controller 21.1.1. Browser Controller Objects 21.2. cgi — Common Gateway Interface support 21.2.1. Introduction 21.2.2. Using the cgi module 21.2.3. Higher Level Interface 21.2.4. Functions 21.2.5. Caring about security 21.2.6. Installing your CGI script on a Unix system 21.2.7. Testing your CGI script 21.2.8. Debugging CGI scripts 21.2.9. Common problems and solutions 21.3. cgitb — Traceback manager for CGI scripts 21.4. wsgiref — WSGI Utilities and Reference Implementation 21.4.1. wsgiref.util – WSGI environment utilities 21.4.2. wsgiref.headers – WSGI response header tools 21.4.3. wsgiref.simple_server – a simple WSGI HTTP server 21.4.4. wsgiref.validate — WSGI conformance checker 21.4.5. wsgiref.handlers – server/gateway base classes 21.4.6. Examples 21.5. urllib — URL handling modules 21.6. urllib.request — Extensible library for opening URLs 21.6.1. Request Objects 21.6.2. OpenerDirector Objects 21.6.3. BaseHandler Objects 21.6.4. HTTPRedirectHandler Objects 21.6.5. HTTPCookieProcessor Objects 21.6.6. ProxyHandler Objects 21.6.7. HTTPPasswordMgr Objects 21.6.8. HTTPPasswordMgrWithPriorAuth Objects 21.6.9. AbstractBasicAuthHandler Objects 21.6.10. HTTPBasicAuthHandler Objects 21.6.11. ProxyBasicAuthHandler Objects 21.6.12. AbstractDigestAuthHandler Objects 21.6.13. HTTPDigestAuthHandler Objects 21.6.14. ProxyDigestAuthHandler Objects 21.6.15. HTTPHandler Objects 21.6.16. HTTPSHandler Objects 21.6.17. FileHandler Objects 21.6.18. DataHandler Objects 21.6.19. FTPHandler Objects 21.6.20. CacheFTPHandler Objects 21.6.21. UnknownHandler Objects 21.6.22. HTTPErrorProcessor Objects 21.6.23. Examples 21.6.24. Legacy interface 21.6.25. urllib.request Restrictions 21.7. urllib.response — Response classes used by urllib 21.8. urllib.parse — Parse URLs into components 21.8.1. URL Parsing 21.8.2. Parsing ASCII Encoded Bytes 21.8.3. Structured Parse Results 21.8.4. URL Quoting 21.9. urllib.error — Exception classes raised by urllib.request 21.10. urllib.robotparser — Parser for robots.txt 21.11. http — HTTP modules 21.11.1. HTTP status codes 21.12. http.client — HTTP protocol client 21.12.1. HTTPConnection Objects 21.12.2. HTTPResponse Objects 21.12.3. Examples 21.12.4. HTTPMessage Objects 21.13. ftplib — FTP protocol client 21.13.1. FTP Objects 21.13.2. FTP_TLS Objects 21.14. poplib — POP3 protocol client 21.14.1. POP3 Objects 21.14.2. POP3 Example 21.15. imaplib — IMAP4 protocol client 21.15.1. IMAP4 Objects 21.15.2. IMAP4 Example 21.16. nntplib — NNTP protocol client 21.16.1. NNTP Objects 21.16.1.1. Attributes 21.16.1.2. Methods 21.16.2. Utility functions 21.17. smtplib — SMTP protocol client 21.17.1. SMTP Objects 21.17.2. SMTP Example 21.18. smtpd — SMTP Server 21.18.1. SMTPServer Objects 21.18.2. DebuggingServer Objects 21.18.3. PureProxy Objects 21.18.4. MailmanProxy Objects 21.18.5. SMTPChannel Objects 21.19. telnetlib — Telnet client 21.19.1. Telnet Objects 21.19.2. Telnet Example 21.20. uuid — UUID objects according to RFC 4122 21.20.1. Example 21.21. socketserver — A framework for network servers 21.21.1. Server Creation Notes 21.21.2. Server Objects 21.21.3. Request Handler Objects 21.21.4. Examples 21.21.4.1. socketserver.TCPServer Example 21.21.4.2. socketserver.UDPServer Example 21.21.4.3. Asynchronous Mixins 21.22. http.server — HTTP servers 21.23. http.cookies — HTTP state management 21.23.1. Cookie Objects 21.23.2. Morsel Objects 21.23.3. Example 21.24. http.cookiejar — Cookie handling for HTTP clients 21.24.1. CookieJar and FileCookieJar Objects 21.24.2. FileCookieJar subclasses and co-operation with web browsers 21.24.3. CookiePolicy Objects 21.24.4. DefaultCookiePolicy Objects 21.24.5. Cookie Objec
Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi Chapter1 Apache and the Internet..............................................1 Apache: The Anatomy of a Web Server.........................................................1 The Apache Source .............................................................................................1 The Apache License............................................................................................1 Support for Apache.............................................................................................2 How Apache Works..............................................................................................3 The Hypertext Transfer Protocol..................................................................7 HTTP Requests and Responses..........................................................................7 HTTP Headers...................................................................................................12 Networking and TCP/IP......................................................................................13 Definitions.........................................................................................................13 Packets and Encapsulation...............................................................................14 ACKs, NAKs, and Other Messages....................................................................15 The TCP/IP Network Model.............................................................................16 Non-IP Protocols...............................................................................................19 IP Addresses and Network Classes...................................................................19 Special IP Addresses..........................................................................................20 Netmasks and Routing......................................................................................21 Web Services: Well-Known Ports......................................................................23 Internet Daemon: The Networking Super Server...........................................24 The Future: IPv6................................................................................................25 Networking Tools...............................................................................................26 Server Hardware...................................................................................................29 Supported Platforms.........................................................................................29 Basic Server Requirements...............................................................................30 Memory..............................................................................................................31 Network Interface..............................................................................................32 Internet Connection.........................................................................................32 Hard Disk and Controller.................................................................................33 Operating System Checklist.............................................................................33 Redundancy and Backup..................................................................................34 Specific Hardware Solutions............................................................................35 Get Someone Else to Do It.............................................................................36 Summary....................................................................................................................36 v 3006_Ch00_CMP2 12/14/03 8:56 AM Page v Chapter 2 Getting Started with Apache.................................37 Installing Apache..............................................................................................38 Getting Apache..................................................................................................38 Installing Apache from Binary Distribution....................................................39 Installing Apache from Source.........................................................................41 Installing Apache from Prebuilt Packages.......................................................41 Installing Apache by Hand...............................................................................45 Upgrading Apache.............................................................................................47 Other Issues.......................................................................................................49 Basic Configuration..........................................................................................50 Decisions............................................................................................................50 Introducing the Master Configuration File.....................................................55 Other Basic Configuration Directives..............................................................56 Starting, Stopping, and Restarting the Server.................................57 Starting Apache on Unix...................................................................................58 Starting Apache on Windows...........................................................................59 Invocation Options...........................................................................................60 Restarting the Server.........................................................................................73 Stopping the Server...........................................................................................75 Starting the Server Automatically....................................................................76 Testing the Server............................................................................................81 Testing with a Browser......................................................................................82 Testing from the Command Line or a Terminal Program..............................82 Testing the Server Configuration Without Starting It.....................................85 Getting the Server Status from the Command Line.......................................86 Using Graphical Configuration Tools.......................................................86 Comanche..........................................................................................................87 TkApache...........................................................................................................91 LinuxConf..........................................................................................................91 Webmin..............................................................................................................91 ApacheConf.......................................................................................................97 Other Configuration Tools................................................................................99 Summary..................................................................................................................100 Chapter 3 Building Apache the Way You Want It...........101 Why Build Apache Yourself?.........................................................................101 Verifying the Apache Source Archive.............................................................103 Building Apache from Source......................................................................105 Configuring and Building Apache.................................................................106 Determining Which Modules to Include.......................................................111 Building Apache As a Dynamic Server..........................................................116 Contents vi 3006_Ch00_CMP2 12/14/03 8:56 AM Page vi Changing the Module Order (Apache 1.3)....................................................118 Checking the Generated Configuration........................................................120 Building Apache from Source As an RPM (Apache 2)..................................122 Advanced Configuration.................................................................................124 Configuring Apache’s Layout..........................................................................124 Choosing a Layout Scheme............................................................................124 Choosing a Multiprocessing Module (Apache 2)..........................................132 Rules (Apache 1.3)...........................................................................................135 Building Apache with suExec support...........................................................137 Configuring Apache’s Supporting Files and Scripts.....................................139 Configuring Apache 2 for Cross-Platform Builds.........................................140 Configuring Apache for Production or Debug Builds..................................142 Configuring Apache for Binary Distribution.................................................143 Configuring Apache’s Library and Include Paths..........................................143 Configuring the Build Environment.........................................................144 Building Modules with configure and apxs..........................................146 Adding Third-Party Modules with configure................................................146 Building Modules with apxs...........................................................................148 Installing Modules with apxs..........................................................................150 Generating Module Templates with apxs......................................................151 Overriding apxs Defaults and Using apxs in makefiles................................152 Summary..................................................................................................................153 Chapter 4 Configuring Apache the Way You Want It...155 Where Apache Looks for Its Configuration..........................................155 Configuration File Syntax...............................................................................156 Configuration for Virtual Hosts......................................................................156 Including Multiple Configuration Files.........................................................157 Per-Directory Configuration..........................................................................159 Conditional Configuration.............................................................................160 How Apache Structures Its Configuration............................................163 Apache’s Container Directives........................................................................164 Directive Types and Locations.......................................................................168 Where Directives Can Go................................................................................171 Container Scope and Nesting.........................................................................172 How Apache Combines Containers and Their Contents.............................174 Legality of Directives in Containers...............................................................175 Options and Overrides....................................................................................176 Enabling and Disabling Features with Options............................................176 Overriding Directives with Per-Directory Configuration.............................179 Contents vii 3006_Ch00_CMP2 12/14/03 8:56 AM Page vii Restricting Access with allow and deny..............................................182 Controlling Access by Name...........................................................................183 Controlling Access by IP Address...................................................................184 Controlling Subnet Access by Network and Netmask..................................185 Controlling Access by HTTP Header.............................................................186 Combining Host-Based Access with User Authentication...........................187 Overriding Host-Based Access.......................................................................188 Directory Listings..........................................................................................188 Enabling and Disabling Directory Indices....................................................189 How mod_autoindex Generates the HTML Page.........................................190 Controlling Which Files Are Seen with IndexIgnore.....................................196 Controlling the Sort Order..............................................................................197 Assigning Icons................................................................................................199 Assigning Descriptions...................................................................................202 Apache’s Environment......................................................................................203 Setting, Unsetting, and Passing Variables from the Shell.............................204 Setting Variables Conditionally......................................................................205 Special Browser Variables...............................................................................207 Detecting Robots with BrowserMatch...........................................................209 Passing Variables to CGI.................................................................................209 Conditional Access Control............................................................................210 Caveats with SetEnvIf vs. SetEnv....................................................................210 Setting Variables with mod_rewrite...............................................................211 Controlling Request and Response Headers..........................................211 Setting Custom Response Headers................................................................213 Setting Custom Request Headers...................................................................215 Inserting Dynamic Values into Headers........................................................216 Setting Custom Headers Conditionally.........................................................217 Retrieving Response Headers from Metadata Files......................................217 Setting Expiry Times.......................................................................................219 Sending Content As-Is....................................................................................222 Controlling the Server Identification Header.................................223 Sending a Content Digest.............................................................................224 Handling the Neighbors.................................................................................225 Controlling Robots with robots.txt................................................................226 Controlling Robots in HTML..........................................................................227 Controlling Robots with Access Control........................................................227 Attracting Robots.............................................................................................228 Making Sure Robots Index the Right Information........................................228 Known Robots, Bad Robots, and Further Reading.......................................229 Summary..................................................................................................................229 Contents viii 3006_Ch00_CMP2 12/14/03 8:56 AM Page viii Chapter 5 Deciding What the Client Needs........................231 Content Handling and Negotiation...........................................................231 File Types.........................................................................................................232 File Encoding...................................................................................................236 File Languages.................................................................................................243 File Character Sets...........................................................................................245 Handling URLs with Extra Path Information................................................247 Content Negotiation.......................................................................................248 Content Negotiation with MultiViews...........................................................250 File Permutations and Valid URLs with MultiViews.....................................256 Magic MIME Types..........................................................................................260 Error and Response Handling......................................................................264 How Apache Handles Errors...........................................................................265 Error and Response Codes.............................................................................265 The ErrorDocument Directive.......................................................................266 Limitations of ErrorDocument......................................................................270 Aliases and Redirection...............................................................................271 Aliases and Script Aliases................................................................................271 Redirections.....................................................................................................273 Rewriting URLs with mod_rewrite.................................................................277 Server-Side Image Maps.................................................................................300 Matching Misspelled URLS............................................................................305 Summary..................................................................................................................306 Chapter 6 Delivering Dynamic Content..................................307 Server-Side Includes......................................................................................308 Enabling SSI.....................................................................................................309 Format of SSI Commands...............................................................................311 The SSI Command Set....................................................................................312 SSI Variables.....................................................................................................312 Passing Trailing Path Information to SSIs (and Other Dynamic Documents).................................................................315 Setting the Date and Error Format.................................................................316 Templating with SSIs.......................................................................................317 Caching Server-Parsed Documents...............................................................319 Identifying Server-Parsed Documents by Execute Permission...................320 CGI: The Common Gateway Interface.........................................................321 CGI and the Environment..............................................................................321 Configuring Apache to Recognize CGI Scripts.............................................323 Setting Up a CGI Directory with ExecCGI: A Simple Way............................327 Triggering CGI Scripts on Events...................................................................330 Contents ix 3006_Ch00_CMP2 12/14/03 8:56 AM Page ix ISINDEX-Style CGI Scripts and Command Line Arguments................332 Writing and Debugging CGI Scripts.........................................................333 A Minimal CGI Script......................................................................................333 Interactive Scripts: A Simple Form................................................................337 Adding Headers...............................................................................................338 Debugging CGI Scripts....................................................................................339 Setting the CGI Daemon Socket.....................................................................345 Limiting CGI Resource Usage.........................................................................346 Actions, Handlers, and Filters................................................................347 Handlers...........................................................................................................348 Filters................................................................................................................354 Dynamic Content and Security....................................................................363 CGI Security Issues..........................................................................................363 Security Advice on the Web............................................................................364 Security Issues with Apache CGI Configuration...........................................364 An Example of an Insecure CGI Script..........................................................365 Known Insecure CGI Scripts...........................................................................370 CGI Wrappers...................................................................................................370 Security Checklist............................................................................................380 Inventing a Better CGI Script with FastCGI......................................381 Summary..................................................................................................................403 Chapter 7 Hosting More Than One Web Site........................405 Implementing User Directories with UserDir......................................406 Enabling and Disabling Specific Users..........................................................407 Redirecting Users to Other Servers................................................................408 Alternative Ways to Implement User Directories.........................................409 Separate Servers..............................................................................................410 Restricting Apache’s Field of View..................................................................411 Specifying Different Configurations and Server Roots................................412 Starting Separate Servers from the Same Configuration.............................412 Sharing External Configuration Files.............................................................413 IP-Based Virtual Hosting.............................................................................414 Multiple IPs, Separate Networks, and Virtual Interfaces..............................415 Configuring What Apache Listens To.............................................................416 Defining IP-Based Virtual Hosts.....................................................................418 Virtual Hosts and the Server-Level Configuration........................................421 Specifying Virtual Host User Privileges..........................................................422 Excluded Directives.........................................................................................426 Default Virtual Hosts....................................................................................427 Contents x 3006_Ch00_CMP2 12/14/03 8:56 AM Page x Name-Based Virtual Hosting.........................................................................428 Defining Named Virtual Hosts.......................................................................428 Server Names and Aliases...............................................................................430 Defining a Default Host for Name-Based Virtual Hosting...........................430 Mixing IP-Based and Name-Based Hosting..................................................431 Issues Affecting Virtual Hosting...........................................................434 Log Files and File Handles..............................................................................434 Virtual Hosts and Server Security..................................................................436 Secure HTTP and Virtual Hosts......................................................................437 Handling HTTP/1.0 Clients with Name-Based Virtual Hosts......................439 Dynamic Virtual Hosting...............................................................................441 Mass Hosting with Virtual-Host Aliases........................................................441 Mapping Hostnames Dynamically with mod_rewrite.................................448 Generating On the Fly and Included Configuration Files with mod_perl..449 Summary..................................................................................................................455 Chapter 8 Improving Apache’s Performance........................457 Apache’s Performance Directives..............................................................458 Configuring MPMs: Processes and Threads..................................................459 Network and IP-Related Performance Directives.........................................470 HTTP-Related Performance Directives.........................................................472 HTTP Limit Directives....................................................................................475 Configuring Apache for Better Performance........................................477 Directives That Affect Performance...............................................................477 Additional Directives for Tuning Performance.............................................482 Benchmarking Apache’s Performance.........................................................490 Benchmarking Apache with ab......................................................................490 Benchmarking Apache with gprof.................................................................495 External Benchmarking Tools........................................................................496 Benchmarking Strategy and Pitfalls...............................................................496 A Performance Checklist...............................................................................497 Proxying................................................................................................................498 Installing and Enabling Proxy Services..........................................................498 Normal Proxy Operation.................................................................................499 Configuring Apache As a Proxy......................................................................500 URL Matching with Directory Containers....................................................502 Blocking Sites via the Proxy............................................................................504 Localizing Remote URLs and Hiding Servers from View.............................504 Relaying Requests to Remote Proxies............................................................508 Proxy Chains and the Via Header...................................................................509 Proxies and Intranets......................................................................................512 Handling Errors...............................................................................................512 Contents xi 3006_Ch00_CMP2 12/14/03 8:56 AM Page xi Timing Out Proxy Requests............................................................................514 Tunneling Other Protocols.............................................................................514 Tuning Proxy Operations................................................................................515 Squid: A High-Performance Proxy Alternative.............................................516 Caching..................................................................................................................516 Enabling Caching............................................................................................516 File-Based Caching.........................................................................................517 In-Memory Caching (Apache 2 Only)............................................................520 Coordinating Memory-Based and Disk-Based Caches................................522 General Cache Configuration.........................................................................522 Maintaining Good Relations with External Caches......................................527 Fault Tolerance and Clustering................................................................529 Backup Server via Redirected Secondary DNS.............................................530 Load Sharing with Round-Robin DNS...........................................................531 Backup Server via Floating IP Address..........................................................531 Hardware Load Balancing..............................................................................532 Clustering with Apache...................................................................................533 Other Clustering Solutions.............................................................................536 Summary..................................................................................................................537 Chapter 9 Monitoring Apache.........................................................539 Logs and Logging..............................................................................................539 Log Files and Security.....................................................................................540 The Error Log...................................................................................................540 Setting the Log Level.......................................................................................541 Logging Errors to the System Log..................................................................542 Transfer Logs...................................................................................................544 Driving Applications Through Logs...............................................................554 Log Rotation....................................................................................................556 Lies, Logs, and Statistics.........................................................................560 What You Can’t Find Out from Logs...............................................................560 Analog: A Log Analyzer...................................................................................561 Server Information..........................................................................................577 Server Status....................................................................................................578 Server Info........................................................................................................581 Securing Access to Server Information.........................................................582 User Tracking.....................................................................................................583 Alternatives to User Tracking.........................................................................584 Cookie Tracking with mod_usertrack............................................................584 URL Tracking with mod_session....................................................................589 Other Session Tracking Options.....................................................................594 Summary..................................................................................................................595 Contents xii 3006_Ch00_CMP2 12/14/03 8:56 AM Page xii Chapter 10Securing Apache..............................................................597 User Authentication........................................................................................597 Apache Authentication Modules...................................................................598 Authentication Configuration Requirements...............................................599 Using Authentication Directives in .htaccess...............................................601 Basic Authentication.......................................................................................601 Digest Authentication.....................................................................................603 Anonymous Authentication...........................................................................606 Setting Up User Information..........................................................................606 Specifying User Requirements.......................................................................614 LDAP Authentication......................................................................................617 Using Multiple Authentication Schemes.......................................................624 Combining User- and Host-Based Authentication......................................626 Securing Basic Authentication with SSL.......................................................627 SSL and Apache...................................................................................................627 Downloading OpenSSL and ModSSL............................................................628 Building and Installing the OpenSSL Library...............................................629 Building and Installing mod_ssl for Apache 2..............................................633 Building and Installing mod_ssl for Apache 1.3...........................................633 Basic SSL Configuration.................................................................................637 Installing a Private Key....................................................................................639 Creating a Certificate Signing Request and Temporary Certificate.............640 Getting a Signed Certificate............................................................................642 Advanced SSL Configuration.........................................................................644 Server-Level Configuration............................................................................644 Client Certification..........................................................................................657 Using Client Certification with User Authentication..................659 SSL and Logging..............................................................................................660 SSL Environment Variables and CGI.............................................................662 SSL and Virtual Hosts......................................................................................666 Advanced Features..........................................................................................668 Summary..................................................................................................................671 Chapter 11Improving Web Server Security..........................673 Apache Features.................................................................................................673 Unwanted Files................................................................................................674 Automatic Directory Indices..........................................................................674 Symbolic Links................................................................................................675 Server-Side Includes.......................................................................................676 ISINDEX-Style CGI Scripts.............................................................................677 Server Tokens...................................................................................................677 Contents xiii 3006_Ch00_CMP2 12/14/03 8:56 AM Page xiii File Permissions..............................................................................................678 Viewing Server Information with mod_info..........................................679 Restricting Server Privileges..................................................................679 Restricting Access by Hostname and IP Address...............................680 Other Server Security Measures................................................................682 Dedicated Server..............................................................................................682 File Integrity...................................................................................................683 md5sum...........................................................................................................684 Tripwire............................................................................................................685 Hardening the Server......................................................................................686 Minimizing Services........................................................................................686 Port Scanning with nmap ..............................................................................688 Probing with Nessus.......................................................................................689 Hardening Windows 2000 and XP..................................................................689 Disabling Network Services.........................................................................690 File Transfer Protocol (FTP)............................................................................690 telnet................................................................................................................690 rlogin, rsh, rexec, rcp.......................................................................................690 Network Filesystem (NFS)..............................................................................690 sendmail/Other Mail Transport Agents (MTAs)...........................................691 Restricting Services with TCP Wrappers........................................................691 Security Fixes, Alerts, and Online Resources.................................693 The WWW Security FAQ..................................................................................693 The BugTraQ Mailing List and Archive..........................................................693 Operating System Newsletters.......................................................................693 Package and Module Notification..................................................................694 Removing Important Data from the Server............................................694 Enabling Secure Logins with SSH..............................................................694 Building and Installing OpenSSH..................................................................695 Authentication Strategies...............................................................................698 Configuring SSH..............................................................................................699 Testing SSH......................................................................................................702 Expanding SSH to Authenticate Users..........................................................703 Secure Server Backups with Rsync and SSH.................................................704 Forwarding Client Connections to Server Applications...............................705 Firewalls and Multifacing Servers.........................................................706 Types of Firewall..............................................................................................706 Designing the Network Topology...................................................................707 Running Apache Under a Virtual chroot Root Directory................709 What chroot Is.................................................................................................709 What chroot Isn’t.............................................................................................710 Setting Up Apache for chroot Operation.......................................................711 Contents xiv 3006_Ch00_CMP2 12/14/03 8:56 AM Page xiv Server Security Checklist...........................................................................723 Avoid Root Services.........................................................................................723 Maintain Logs Properly..................................................................................723 Keep It Simple..................................................................................................724 Block Abusive Clients......................................................................................724 Have an Effective Backup and Restore Process............................................725 Plan for High Availability, Capacity, and Disaster Recovery........................725 Monitor the Server..........................................................................................725 Take Care with Information Flow...................................................................726 Choose an Effective robots.txt Policy............................................................726 Summary..................................................................................................................726 Chapter 12Extending Apache............................................................727 WebDAV....................................................................................................................727 Adding WebDAV to Apache.............................................................................728 The WebDAV Protocol.....................................................................................729 Configuring Apache for WebDAV...................................................................731 Restricting Options and Disabling Overrides...............................................734 WebDAV and Virtual Hosts.............................................................................735 Configuring the DAV Lock Time.....................................................................735 Limitations of File-Based Repositories..........................................................736 Protecting WebDAV Servers............................................................................737 More Advanced Configurations.....................................................................737 Cooperating with CGI and Other Content Handlers....................................740 ISAPI......................................................................................................................741 Supported ISAPI Support Functions.............................................................742 Configuring ISAPI Extensions........................................................................743 Setting the Maximum Initial Request Data Size...........................................744 Logging ISAPI Extensions...............................................................................745 Preloading and Caching ISAPI Extensions....................................................746 Handling Asynchronous ISAPI Extensions...................................................746 Perl.........................................................................................................................746 Building and Installing mod_perl..................................................................748 Migrating mod_perl from Apache 1.3 to Apache 2.......................................755 Configuring and Implementing Perl Handlers.............................................758 Configuring and Implementing Perl Filters..................................................771 Warnings, Taint Mode, and Debugging.........................................................772 Managing Perl Threads in mod_perl 2...........................................................774 Initializing Modules at Startup.......................................................................779 Restarting mod_perl and Auto-Reloading Modules.....................................780 Creating a mod_perl Status Page...................................................................782 Running CGI Scripts Under mod_perl..........................................................782 Contents xv 3006_Ch00_CMP2 12/14/03 8:56 AM Page xv CGI Caveats......................................................................................................785 Passing Variables to Perl Handlers.................................................................787 Using mod_perl with Server-Side Includes...................................................788 Embedding Perl in HTML...............................................................................789 Embedding Perl in Apache’s Configuration..................................................794 PHP...........................................................................................................................795 Installing PHP..................................................................................................796 Getting the PHP source...................................................................................796 Configuring Apache to Work with PHP..........................................................802 Configuring PHP.............................................................................................803 Testing PHP with Apache................................................................................807 Tomcat/Java.........................................................................................................807 So What Is Tomcat?..........................................................................................807 Installation.......................................................................................................808 Tomcat Configuration.....................................................................................813 mod_jk.............................................................................................................818 Mod_python....................................................................................................829 mod_ruby.........................................................................................................835 Summary..................................................................................................................839 Index....................................................................................................................843

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧