Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Advanced Bash Scripting

Advanced Bash Scripting

Ratings: (0)|Views: 39 |Likes:
Published by api-3729323

More info:

Published by: api-3729323 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Advanced Bash-Scripting Guide
An in-depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
5.1
10 November 2007
Revision History
Revision 4.3
29 Apr 2007

Revised by: mc
'INKBERRY' release: Minor Update.
Revision 5.0

24 Jun 2007

Revised by: mc
'SERVICEBERRY' release: Major Update.
Revision 5.1

10 Nov 2007
Revised by: mc
'LINGONBERRY' release: Minor 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\u00ae 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 ( pdf mirror
site). 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..................................................................................27

4.1. Variable Substitution......................................................................................................................27 4.2. Variable Assignment.......................................................................................................................30 4.3. Bash Variables Are Untyped..........................................................................................................31 4.4. Special Variable Types...................................................................................................................32

Chapter 5. Quoting...........................................................................................................................................37
5.1. Quoting Variables...........................................................................................................................37
5.2. Escaping..........................................................................................................................................39
Chapter 6. Exit and Exit Status.......................................................................................................................44
Chapter 7. Tests................................................................................................................................................46

7.1. Test Constructs...............................................................................................................................46 7.2. File test operators............................................................................................................................52 7.3. Other Comparison Operators..........................................................................................................55 7.4. Nested if/then Condition Tests.......................................................................................................60 7.5. Testing Your Knowledge of Tests..................................................................................................60

Chapter 8. Operations and Related Topics....................................................................................................62
8.1. Operators.........................................................................................................................................62
8.2. Numerical Constants.......................................................................................................................68
Part 3. Beyond the Basics.................................................................................................................................70
Chapter 9. Variables Revisited........................................................................................................................71

9.1. Internal Variables............................................................................................................................71
9.2. Manipulating Strings.......................................................................................................................88
9.2.1. Manipulating strings using awk............................................................................................95
9.2.2. Further Discussion.................................................................................................................96
9.3. Parameter Substitution....................................................................................................................96
9.4. Typing variables: declare or typeset.............................................................................................105
9.5. Indirect References.......................................................................................................................107
9.6. $RANDOM: generate random integer..........................................................................................110
9.7. The Double Parentheses Construct...............................................................................................121

Advanced Bash-Scripting Guide
i
Table of Contents
Chapter 10. Loops and Branches..................................................................................................................123

10.1. Loops..........................................................................................................................................123 10.2. Nested Loops..............................................................................................................................135 10.3. Loop Control...............................................................................................................................136 10.4. Testing and Branching................................................................................................................139

Chapter 11. Command Substitution.............................................................................................................147 Chapter 12. Arithmetic Expansion................................................................................................................153 Chapter 13. Recess Time................................................................................................................................154 Part 4. Commands..........................................................................................................................................155 Chapter 14. Internal Commands and Builtins.............................................................................................163

14.1. Job Control Commands..............................................................................................................190
Chapter 15. External Filters, Programs and Commands...........................................................................195

15.1. Basic Commands........................................................................................................................195 15.2. Complex Commands...................................................................................................................200 15.3. Time / Date Commands..............................................................................................................210 15.4. Text Processing Commands........................................................................................................213 15.5. File and Archiving Commands...................................................................................................234 15.6. Communications Commands......................................................................................................251 15.7. Terminal Control Commands.....................................................................................................265 15.8. Math Commands.........................................................................................................................266 15.9. Miscellaneous Commands..........................................................................................................275

Chapter 16. System and Administrative Commands..................................................................................289
16.1. Analyzing a System Script..........................................................................................................318
Part 5. Advanced Topics.................................................................................................................................320
Chapter 17. Regular Expressions..................................................................................................................322
17.1. A Brief Introduction to Regular Expressions..............................................................................322
17.2. Globbing.....................................................................................................................................325
Chapter 18. Here Documents.........................................................................................................................327
18.1. Here Strings................................................................................................................................337
Chapter 19. I/O Redirection...........................................................................................................................340

19.1. Using exec...................................................................................................................................343 19.2. Redirecting Code Blocks............................................................................................................346 19.3. Applications................................................................................................................................351

Chapter 20. Subshells.....................................................................................................................................353
Advanced Bash-Scripting Guide
ii

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->