Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cooper M.Advanced bash-scripting guide.2002.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
916.67 Кб
Скачать

Advanced Bash−Scripting Guide

A complete guide to shell scripting

Mendel Cooper

Brindlesoft

 

 

thegrendel@theriver.com

 

 

06 January 2002

 

 

Revision History

 

 

Revision 0.1

14 June 2000

Revised by: mc

Initial release.

 

 

Revision 0.2

30 October 2000

Revised by: mc

Bugs fixed, plus much additional material and more example

scripts.

Revision 0.3

12 February 2001

Revised by: mc

Another major update.

 

 

Revision 0.4

08 July 2001

Revised by: mc

More bugfixes, much more material, more scripts − a complete revision and expansion of the book.

Revision 0.5

03 September 2001

Revised by: mc

Major update. Bugfixes, material added, chapters and sections reorganized.

Revision 1.0

14 October 2001

Revised by: mc

Bugfixes, reorganization, material added. Stable release.

 

Revision 1.1

06 January 2002

Revised by: mc

Bugfixes, material and scripts added.

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.

The latest update of this document, as an archived "tarball" including both the SGML source and rendered HTML, may be downloaded from the author's home site. See the change log for a revision history.

Dedication

For Anita, the source of all the magic

Advanced Bash−Scripting Guide

 

Table of Contents

 

Chapter 1. Why Shell Programming?...............................................................................................................

1

Chapter 2. Starting Off With a Sha−Bang .......................................................................................................

3

2.1. Invoking the script............................................................................................................................

5

2.2. Preliminary Exercises.......................................................................................................................

6

Part 2. Basics............................................................................................................................................

6

Chapter 3. Exit and Exit Status.........................................................................................................................

7

Chapter 4. Special Characters...........................................................................................................................

9

Chapter 5. Introduction to Variables and Parameters..................................................................................

23

5.1. Variable Substitution......................................................................................................................

23

5.2. Variable Assignment.......................................................................................................................

25

5.3. Bash Variables Are Untyped..........................................................................................................

26

5.4. Special Variable Types...................................................................................................................

27

Chapter 6. Quoting...........................................................................................................................................

31

Chapter 7. Tests................................................................................................................................................

37

7.1. Test Constructs...............................................................................................................................

37

7.2. File test operators............................................................................................................................

42

7.3. Comparison operators (binary).......................................................................................................

45

7.4. Nested if/then Condition Tests.......................................................................................................

50

7.5. Testing Your Knowledge of Tests..................................................................................................

51

Chapter 8. Operations and Related Topics....................................................................................................

52

8.1. Operators.........................................................................................................................................

52

8.2. Numerical Constants.......................................................................................................................

58

Part 3. Beyond the Basics......................................................................................................................

58

Chapter 9. Variables Revisited........................................................................................................................

60

9.1. Internal Variables............................................................................................................................

60

9.2. Manipulating Strings.......................................................................................................................

75

9.2.1. Manipulating strings using awk......................................................................................

79

9.2.2. Further Discussion..........................................................................................................

80

9.3. Parameter Substitution....................................................................................................................

80

9.4. Typing variables: declare or typeset..............................................................................................

88

9.5. Indirect References to Variables.....................................................................................................

90

9.6. $RANDOM: generate random integer............................................................................................

92

9.7. The Double Parentheses Construct.................................................................................................

96

Chapter 10. Loops and Branches....................................................................................................................

98

10.1. Loops............................................................................................................................................

98

10.2. Nested Loops..............................................................................................................................

108

10.3. Loop Control...............................................................................................................................

108

10.4. Testing and Branching................................................................................................................

111

i

Advanced Bash−Scripting Guide

 

Table of Contents

 

Chapter 11. Internal Commands and Builtins.............................................................................................

117

11.1. Job Control Commands..............................................................................................................

132

Chapter 12. External Filters, Programs and Commands...........................................................................

136

12.1. Basic Commands........................................................................................................................

136

12.2. Complex Commands...................................................................................................................

139

12.3. Time / Date Commands..............................................................................................................

145

12.4. Text Processing Commands........................................................................................................

147

12.5. File and Archiving Commands...................................................................................................

166

12.6. Communications Commands......................................................................................................

174

12.7. Terminal Control Commands.....................................................................................................

177

12.8. Math Commands.........................................................................................................................

178

12.9. Miscellaneous Commands..........................................................................................................

183

Chapter 13. System and Administrative Commands..................................................................................

190

Chapter 14. Command Substitution.............................................................................................................

213

Chapter 15. Arithmetic Expansion................................................................................................................

217

Chapter 16. I/O Redirection...........................................................................................................................

218

16.1. Using exec...................................................................................................................................

220

16.2. Redirecting Code Blocks............................................................................................................

221

16.3. Applications................................................................................................................................

225

Chapter 17. Here Documents.........................................................................................................................

227

Chapter 18. Recess Time................................................................................................................................

232

Part 4. Advanced Topics......................................................................................................................

232

Chapter 19. Regular Expressions..................................................................................................................

234

19.1. A Brief Introduction to Regular Expressions..............................................................................

234

19.2. Globbing.....................................................................................................................................

236

Chapter 20. Subshells.....................................................................................................................................

238

Chapter 21. Restricted Shells.........................................................................................................................

241

Chapter 22. Process Substitution...................................................................................................................

243

Chapter 23. Functions....................................................................................................................................

245

23.1. Complex Functions and Function Complexities.........................................................................

247

23.2. Local Variables...........................................................................................................................

254

23.2.1. Local variables make recursion possible....................................................................

255

Chapter 24. Aliases.........................................................................................................................................

257

Chapter 25. List Constructs...........................................................................................................................

260

ii

Advanced Bash−Scripting Guide

 

Table of Contents

 

Chapter 26. Arrays.........................................................................................................................................

263

Chapter 27. Files.............................................................................................................................................

274

Chapter 28. /dev and /proc.............................................................................................................................

275

28.1. /dev..............................................................................................................................................

275

28.2. /proc............................................................................................................................................

275

Chapter 29. Of Zeros and Nulls.....................................................................................................................

280

Chapter 30. Debugging...................................................................................................................................

283

Chapter 31. Options........................................................................................................................................

289

Chapter 32. Gotchas.......................................................................................................................................

292

Chapter 33. Scripting With Style..................................................................................................................

296

33.1. Unofficial Shell Scripting Stylesheet..........................................................................................

296

Chapter 34. Miscellany...................................................................................................................................

299

34.1. Interactive and non−interactive shells and scripts......................................................................

299

34.2. Shell Wrappers............................................................................................................................

300

34.3. Tests and Comparisons: Alternatives..........................................................................................

303

34.4. Optimizations..............................................................................................................................

304

34.5. Assorted Tips..............................................................................................................................

304

34.6. Oddities.......................................................................................................................................

307

34.7. Portability Issues.........................................................................................................................

308

34.8. Shell Scripting Under Windows.................................................................................................

308

Chapter 35. Bash, version 2...........................................................................................................................

309

Chapter 36. Endnotes.....................................................................................................................................

313

36.1. Author's Note..............................................................................................................................

313

36.2. About the Author........................................................................................................................

313

36.3. Tools Used to Produce This Book..............................................................................................

313

36.3.1. Hardware.....................................................................................................................

313

36.3.2. Software and Printware...............................................................................................

313

36.4. Credits.........................................................................................................................................

314

Bibliography........................................................................................................................................

315

Appendix A. Contributed Scripts.........................................................................................................

319

Appendix B. A Sed and Awk Micro−Primer.......................................................................................

337

B.1. Sed................................................................................................................................................

337

B.2. Awk..............................................................................................................................................

340

Appendix C. Exit Codes With Special Meanings................................................................................

341

Appendix D. A Detailed Introduction to I/O and I/O Redirection......................................................

342

Appendix E. Localization....................................................................................................................

343

Appendix F. History Commands.........................................................................................................

345

Appendix G. A Sample .bashrc File....................................................................................................

346

iii

Advanced Bash−Scripting Guide

 

Table of Contents

 

Appendix H. Converting DOS Batch Files to Shell Scripts................................................................

355

Appendix I. Exercises..........................................................................................................................

359

Appendix J. Copyright.........................................................................................................................

361

iv