没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Advanced Bash−Scripting Guide
An in−depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
3.7
23 October 2005
Revision History
Revision 3.5 04 June 2005 Revised by: mc
'BOXBERRY' release: Important Update.
Revision 3.6 28 Aug 2005 Revised by: mc
'POKEBERRY' release: Bugfix Update.
Revision 3.7 23 Oct 2005 Revised by: mc
'WHORTLEBERRY' release: Bugfix Update.
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an
intermediate/advanced level of instruction . . . all the while sneaking in little snippets of UNIX® wisdom and
lore. It serves as a textbook, a manual for self−study, and a reference and source of knowledge on shell
scripting techniques. The exercises and heavily−commented examples invite active reader participation, under
the premise that the only way to really learn scripting is to write scripts.
This book is suitable for classroom use as a general introduction to programming concepts.
The latest update of this document, as an archived, bzip2−ed "tarball" including both the SGML source and
rendered HTML, may be downloaded from the author's home site. A pdf version is also available. See the
change log for a revision history.
Dedication
For Anita, the source of all the magic
Table of Contents
Chapter 1. Why Shell Programming?...............................................................................................................1
Chapter 2. Starting Off With a Sha−Bang.......................................................................................................3
2.1. Invoking the script............................................................................................................................6
2.2. Preliminary Exercises.......................................................................................................................6
Part 2. Basics.......................................................................................................................................................7
Chapter 3. Special Characters...........................................................................................................................8
Chapter 4. Introduction to Variables and Parameters..................................................................................25
4.1. Variable Substitution......................................................................................................................25
4.2. Variable Assignment.......................................................................................................................27
4.3. Bash Variables Are Untyped..........................................................................................................28
4.4. Special Variable Types...................................................................................................................30
Chapter 5. Quoting...........................................................................................................................................34
5.1. Quoting Variables...........................................................................................................................34
5.2. Escaping..........................................................................................................................................35
Chapter 6. Exit and Exit Status.......................................................................................................................41
Chapter 7. Tests................................................................................................................................................43
7.1. Test Constructs...............................................................................................................................43
7.2. File test operators............................................................................................................................49
7.3. Other Comparison Operators..........................................................................................................52
7.4. Nested if/then Condition Tests.......................................................................................................57
7.5. Testing Your Knowledge of Tests..................................................................................................57
Chapter 8. Operations and Related Topics....................................................................................................59
8.1. Operators.........................................................................................................................................59
8.2. Numerical Constants.......................................................................................................................65
Part 3. Beyond the Basics.................................................................................................................................67
Chapter 9. Variables Revisited........................................................................................................................68
9.1. Internal Variables............................................................................................................................68
9.2. Manipulating Strings.......................................................................................................................84
9.2.1. Manipulating strings using awk............................................................................................89
9.2.2. Further Discussion.................................................................................................................90
9.3. Parameter Substitution....................................................................................................................90
9.4. Typing variables: declare or typeset...............................................................................................99
9.5. Indirect References to Variables...................................................................................................101
9.6. $RANDOM: generate random integer..........................................................................................104
9.7. The Double Parentheses Construct...............................................................................................113
Advanced Bash−Scripting Guide
i
Table of Contents
Chapter 10. Loops and Branches..................................................................................................................115
10.1. Loops..........................................................................................................................................115
10.2. Nested Loops..............................................................................................................................126
10.3. Loop Control...............................................................................................................................126
10.4. Testing and Branching................................................................................................................130
Chapter 11. Internal Commands and Builtins.............................................................................................137
11.1. Job Control Commands..............................................................................................................160
Chapter 12. External Filters, Programs and Commands...........................................................................165
12.1. Basic Commands........................................................................................................................165
12.2. Complex Commands...................................................................................................................169
12.3. Time / Date Commands..............................................................................................................179
12.4. Text Processing Commands........................................................................................................181
12.5. File and Archiving Commands...................................................................................................201
12.6. Communications Commands......................................................................................................217
12.7. Terminal Control Commands.....................................................................................................230
12.8. Math Commands.........................................................................................................................231
12.9. Miscellaneous Commands..........................................................................................................240
Chapter 13. System and Administrative Commands..................................................................................252
13.1. Analyzing a System Script..........................................................................................................277
Chapter 14. Command Substitution.............................................................................................................279
Chapter 15. Arithmetic Expansion................................................................................................................285
Chapter 16. I/O Redirection...........................................................................................................................286
16.1. Using exec...................................................................................................................................288
16.2. Redirecting Code Blocks............................................................................................................292
16.3. Applications................................................................................................................................296
Chapter 17. Here Documents.........................................................................................................................298
17.1. Here Strings................................................................................................................................307
Chapter 18. Recess Time................................................................................................................................308
Part 4. Advanced Topics.................................................................................................................................309
Chapter 19. Regular Expressions..................................................................................................................310
19.1. A Brief Introduction to Regular Expressions..............................................................................310
19.2. Globbing.....................................................................................................................................313
Chapter 20. Subshells.....................................................................................................................................315
Chapter 21. Restricted Shells.........................................................................................................................318
Advanced Bash−Scripting Guide
ii
Table of Contents
Chapter 22. Process Substitution...................................................................................................................320
Chapter 23. Functions....................................................................................................................................323
23.1. Complex Functions and Function Complexities.........................................................................325
23.2. Local Variables...........................................................................................................................335
23.2.1. Local variables help make recursion possible...................................................................336
23.3. Recursion Without Local Variables............................................................................................337
Chapter 24. Aliases.........................................................................................................................................340
Chapter 25. List Constructs...........................................................................................................................343
Chapter 26. Arrays.........................................................................................................................................346
Chapter 27. /dev and /proc.............................................................................................................................372
27.1. /dev..............................................................................................................................................372
27.2. /proc............................................................................................................................................373
Chapter 28. Of Zeros and Nulls.....................................................................................................................378
Chapter 29. Debugging...................................................................................................................................381
Chapter 30. Options........................................................................................................................................391
Chapter 31. Gotchas.......................................................................................................................................393
Chapter 32. Scripting With Style..................................................................................................................401
32.1. Unofficial Shell Scripting Stylesheet..........................................................................................401
Chapter 33. Miscellany...................................................................................................................................404
33.1. Interactive and non−interactive shells and scripts......................................................................404
33.2. Shell Wrappers............................................................................................................................405
33.3. Tests and Comparisons: Alternatives..........................................................................................409
33.4. Recursion....................................................................................................................................410
33.5. "Colorizing" Scripts....................................................................................................................412
33.6. Optimizations..............................................................................................................................425
33.7. Assorted Tips..............................................................................................................................426
33.8. Security Issues............................................................................................................................435
33.9. Portability Issues.........................................................................................................................435
33.10. Shell Scripting Under Windows...............................................................................................436
Chapter 34. Bash, versions 2 and 3...............................................................................................................437
34.1. Bash, version2.............................................................................................................................437
34.2. Bash, version 3............................................................................................................................441
Chapter 35. Endnotes.....................................................................................................................................444
35.1. Author's Note..............................................................................................................................444
35.2. About the Author........................................................................................................................444
Advanced Bash−Scripting Guide
iii
Table of Contents
Chapter 35. Endnotes
35.3. Where to Go For Help.................................................................................................................444
35.4. Tools Used to Produce This Book..............................................................................................445
35.4.1. Hardware...........................................................................................................................445
35.4.2. Software and Printware.....................................................................................................445
35.5. Credits.........................................................................................................................................445
Bibliography....................................................................................................................................................448
Appendix A. Contributed Scripts..................................................................................................................455
Appendix B. Reference Cards........................................................................................................................582
Appendix C. A Sed and Awk Micro−Primer................................................................................................587
C.1. Sed................................................................................................................................................587
C.2. Awk..............................................................................................................................................590
Appendix D. Exit Codes With Special Meanings.........................................................................................593
Appendix E. A Detailed Introduction to I/O and I/O Redirection.............................................................594
Appendix F. Standard Command−Line Options.........................................................................................596
Appendix G. Important Files.........................................................................................................................598
Appendix H. Important System Directories.................................................................................................599
Appendix I. Localization................................................................................................................................601
Appendix J. History Commands...................................................................................................................605
Appendix K. A Sample .bashrc File..............................................................................................................606
Appendix L. Converting DOS Batch Files to Shell Scripts.........................................................................617
Appendix M. Exercises...................................................................................................................................621
M.1. Analyzing Scripts........................................................................................................................621
M.2. Writing Scripts............................................................................................................................622
Appendix N. Revision History.......................................................................................................................629
Appendix O. Mirror Sites...............................................................................................................................631
Appendix P. To Do List..................................................................................................................................632
Appendix Q. Copyright..................................................................................................................................634
Notes....................................................................................................................................................635
Advanced Bash−Scripting Guide
iv
剩余645页未读,继续阅读
libra811
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0