没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Advanced Bash−Scripting Guide
An in−depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
3.1
14 November 2004
Revision History
Revision 2.8 11 July 2004 Revised by: mc
'ELDERBERRY' release: Minor update.
Revision 3.0 03 Oct 2004 Revised by: mc
'LOGANBERRY' release: Major update.
Revision 3.1 14 Nov 2004 Revised by: mc
'BAYBERRY' 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
Chapter 6. Exit and Exit Status.......................................................................................................................40
Chapter 7. Tests................................................................................................................................................42
7.1. Test Constructs...............................................................................................................................42
7.2. File test operators............................................................................................................................48
7.3. Other Comparison Operators..........................................................................................................51
7.4. Nested if/then Condition Tests.......................................................................................................56
7.5. Testing Your Knowledge of Tests..................................................................................................56
Chapter 8. Operations and Related Topics....................................................................................................58
8.1. Operators.........................................................................................................................................58
8.2. Numerical Constants.......................................................................................................................64
Part 3. Beyond the Basics.................................................................................................................................66
Chapter 9. Variables Revisited........................................................................................................................67
9.1. Internal Variables............................................................................................................................67
9.2. Manipulating Strings.......................................................................................................................84
9.2.1. Manipulating strings using awk............................................................................................88
9.2.2. Further Discussion.................................................................................................................89
9.3. Parameter Substitution....................................................................................................................89
9.4. Typing variables: declare or typeset...............................................................................................97
9.5. Indirect References to Variables.....................................................................................................99
9.6. $RANDOM: generate random integer..........................................................................................101
9.7. The Double Parentheses Construct...............................................................................................110
Chapter 10. Loops and Branches..................................................................................................................112
10.1. Loops..........................................................................................................................................112
10.2. Nested Loops..............................................................................................................................123
10.3. Loop Control...............................................................................................................................123
Advanced Bash−Scripting Guide
i
Table of Contents
Chapter 10. Loops and Branches
10.4. Testing and Branching................................................................................................................127
Chapter 11. Internal Commands and Builtins.............................................................................................134
11.1. Job Control Commands..............................................................................................................156
Chapter 12. External Filters, Programs and Commands...........................................................................161
12.1. Basic Commands........................................................................................................................161
12.2. Complex Commands...................................................................................................................165
12.3. Time / Date Commands..............................................................................................................174
12.4. Text Processing Commands........................................................................................................177
12.5. File and Archiving Commands...................................................................................................194
12.6. Communications Commands......................................................................................................209
12.7. Terminal Control Commands.....................................................................................................217
12.8. Math Commands.........................................................................................................................218
12.9. Miscellaneous Commands..........................................................................................................227
Chapter 13. System and Administrative Commands..................................................................................238
Chapter 14. Command Substitution.............................................................................................................262
Chapter 15. Arithmetic Expansion................................................................................................................267
Chapter 16. I/O Redirection...........................................................................................................................268
16.1. Using exec...................................................................................................................................270
16.2. Redirecting Code Blocks............................................................................................................274
16.3. Applications................................................................................................................................278
Chapter 17. Here Documents.........................................................................................................................280
17.1. Here Strings................................................................................................................................288
Chapter 18. Recess Time................................................................................................................................290
Part 4. Advanced Topics.................................................................................................................................291
Chapter 19. Regular Expressions..................................................................................................................292
19.1. A Brief Introduction to Regular Expressions..............................................................................292
19.2. Globbing.....................................................................................................................................295
Chapter 20. Subshells.....................................................................................................................................297
Chapter 21. Restricted Shells.........................................................................................................................300
Chapter 22. Process Substitution...................................................................................................................302
Chapter 23. Functions....................................................................................................................................305
23.1. Complex Functions and Function Complexities.........................................................................307
23.2. Local Variables...........................................................................................................................317
Advanced Bash−Scripting Guide
ii
Table of Contents
Chapter 23. Functions
23.2.1. Local variables help make recursion possible...................................................................318
23.3. Recursion Without Local Variables............................................................................................319
Chapter 24. Aliases.........................................................................................................................................321
Chapter 25. List Constructs...........................................................................................................................324
Chapter 26. Arrays.........................................................................................................................................327
Chapter 27. Files.............................................................................................................................................353
Chapter 28. /dev and /proc.............................................................................................................................354
28.1. /dev..............................................................................................................................................354
28.2. /proc............................................................................................................................................355
Chapter 29. Of Zeros and Nulls.....................................................................................................................360
Chapter 30. Debugging...................................................................................................................................364
Chapter 31. Options........................................................................................................................................372
Chapter 32. Gotchas.......................................................................................................................................374
Chapter 33. Scripting With Style..................................................................................................................382
33.1. Unofficial Shell Scripting Stylesheet..........................................................................................382
Chapter 34. Miscellany...................................................................................................................................385
34.1. Interactive and non−interactive shells and scripts......................................................................385
34.2. Shell Wrappers............................................................................................................................386
34.3. Tests and Comparisons: Alternatives..........................................................................................389
34.4. Recursion....................................................................................................................................390
34.5. "Colorizing" Scripts....................................................................................................................392
34.6. Optimizations..............................................................................................................................399
34.7. Assorted Tips..............................................................................................................................399
34.8. Security Issues............................................................................................................................409
34.9. Portability Issues.........................................................................................................................409
34.10. Shell Scripting Under Windows...............................................................................................410
Chapter 35. Bash, versions 2 and 3...............................................................................................................411
35.1. Bash, version2.............................................................................................................................411
35.2. Bash, version3.............................................................................................................................415
Chapter 36. Endnotes.....................................................................................................................................417
36.1. Author's Note..............................................................................................................................417
36.2. About the Author........................................................................................................................417
36.3. Where to Go For Help.................................................................................................................417
36.4. Tools Used to Produce This Book..............................................................................................418
Advanced Bash−Scripting Guide
iii
Table of Contents
Chapter 36. Endnotes
36.4.1. Hardware...........................................................................................................................418
36.4.2. Software and Printware.....................................................................................................418
36.5. Credits.........................................................................................................................................418
Bibliography....................................................................................................................................................420
Appendix A. Contributed Scripts..................................................................................................................426
Appendix B. Reference Cards........................................................................................................................524
Appendix C. A Sed and Awk Micro−Primer................................................................................................529
C.1. Sed................................................................................................................................................529
C.2. Awk..............................................................................................................................................532
Appendix D. Exit Codes With Special Meanings.........................................................................................535
Appendix E. A Detailed Introduction to I/O and I/O Redirection.............................................................536
Appendix F. Standard Command−Line Options.........................................................................................538
Appendix G. Important System Directories.................................................................................................540
Appendix H. Localization...............................................................................................................................542
Appendix I. History Commands....................................................................................................................546
Appendix J. A Sample .bashrc File...............................................................................................................547
Appendix K. Converting DOS Batch Files to Shell Scripts........................................................................558
Appendix L. Exercises....................................................................................................................................562
L.1. Analyzing Scripts.........................................................................................................................562
L.2. Writing Scripts.............................................................................................................................563
Appendix M. Revision History.......................................................................................................................570
Appendix N. Mirror Sites...............................................................................................................................571
Appendix O. To Do List..................................................................................................................................572
Appendix P. Copyright...................................................................................................................................574
Advanced Bash−Scripting Guide
iv
剩余584页未读,继续阅读
资源评论
- yyii1232012-03-28用上了,很好,可惜是英文版本,找起来很耗时啊,谢谢楼主
- AAA215713202013-04-22相当不错 初学者能看懂
wangzuocai
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功