Bash-Beginners-Guide.pdf

(1182 KB) Pobierz
Bash Guide for Beginners
Bash Guide for Beginners
Machtelt Garrels
Xalasys.com
Version 1.6 Last updated 20050301 Edition
8011928.002.png
Bash Guide for Beginners
Table of Contents
Introduction. ........................................................................................................................................................1
1. Why this guide?. ..................................................................................................................................1
2. Who should read this book?. ................................................................................................................1
3. New versions and availability. .............................................................................................................2
4. Revision History. .................................................................................................................................2
5. Contributions. ......................................................................................................................................3
6. Feedback. .............................................................................................................................................3
7. Copyright information .........................................................................................................................3
8. What do you need?. ..............................................................................................................................4
9. Conventions used in this document. ....................................................................................................4
10. Organization of this documen. ..........................................................................................................4
Chapter 1. Bash and Bash scripts. .....................................................................................................................6
1.1. Common shell programs. ..................................................................................................................6
1.1.1. General shell functions. ...........................................................................................................6
1.1.2. Shell types. ..............................................................................................................................6
1.2. Advantages of the Bourne Again SHel. ...........................................................................................7
1.2.1. Bash is the GNU shell. ............................................................................................................7
1.2.2. Features only found in bash.. ...................................................................................................7
1.3. Executing commands. .....................................................................................................................12
1.3.1. General. .................................................................................................................................12
1.3.2. Shell built−in commands. ......................................................................................................12
1.3.3. Executing programs from a script. ........................................................................................13
1.4. Building blocks. ..............................................................................................................................13
1.4.1. Shell building blocks. ............................................................................................................13
1.5. Developing good scripts. ................................................................................................................15
1.5.1. Properties of good scripts. .....................................................................................................15
1.5.2. Structure. ...............................................................................................................................16
1.5.3. Terminology. .........................................................................................................................16
1.5.4. A word on order and logic. ....................................................................................................16
1.5.5. An example Bash script: mysystem.sh. .................................................................................17
1.5.6. Example init scrip. ................................................................................................................18
1.6. Summary. ........................................................................................................................................19
1.7. Exercises. ........................................................................................................................................19
Chapter 2. Writing and debugging scripts. ....................................................................................................21
2.1. Creating and running a script. .........................................................................................................21
2.1.1. Writing and naming. ..............................................................................................................21
2.1.2. script1.sh. ...............................................................................................................................21
2.1.3. Executing the script. ..............................................................................................................22
2.2. Script basics. ...................................................................................................................................24
2.2.1. Which shell will run the script?. ............................................................................................24
2.2.2. Adding comments.. ................................................................................................................24
2.3. Debugging Bash scripts. .................................................................................................................25
2.3.1. Debugging on the entire script. .............................................................................................25
2.3.2. Debugging on part(s) of the scrip. ........................................................................................26
2.4. Summary. ........................................................................................................................................28
2.5. Exercises. ........................................................................................................................................28
i
8011928.003.png
Bash Guide for Beginners
Table of Contents
Chapter 3. The Bash environment. .................................................................................................................29
3.1. Shell initialization files. ..................................................................................................................29
3.1.1. System−wide configuration files. ..........................................................................................29
3.1.2. Individual user configuration files. .......................................................................................31
3.1.3. Changing shell configuration files. .......................................................................................33
3.2. Variables. ........................................................................................................................................34
3.2.1. Types of variables. ................................................................................................................34
3.2.2. Creating variables. .................................................................................................................37
3.2.3. Exporting variables. ...............................................................................................................38
3.2.4. Reserved variables. ................................................................................................................39
3.2.5. Special parameters. ................................................................................................................41
3.2.6. Script recycling with variables. .............................................................................................43
3.3. Quoting characters. .........................................................................................................................44
3.3.1. Why?. ....................................................................................................................................44
3.3.2. Escape characters. .................................................................................................................45
3.3.3. Single quotes. ........................................................................................................................45
3.3.4. Double quotes. .......................................................................................................................45
3.3.5. ANSI−C quoting ...................................................................................................................46
3.3.6. Locales.. .................................................................................................................................46
3.4. Shell expansion. ..............................................................................................................................46
3.4.1. General. .................................................................................................................................46
3.4.2. Brace expansion. ...................................................................................................................46
3.4.3. Tilde expansion. ....................................................................................................................47
3.4.4. Shell parameter and variable expansion. ...............................................................................47
3.4.5. Command substitution. ..........................................................................................................48
3.4.6. Arithmetic expansion. ...........................................................................................................49
3.4.7. Process substitution. ..............................................................................................................50
3.4.8. Word splitting. .......................................................................................................................50
3.4.9. File name expansion. .............................................................................................................51
3.5. Aliases. ............................................................................................................................................51
3.5.1. What are aliases?. ..................................................................................................................51
3.5.2. Creating and removing aliases. .............................................................................................52
3.6. More Bash options. .........................................................................................................................53
3.6.1. Displaying options. ................................................................................................................53
3.6.2. Changing options. ..................................................................................................................54
3.7. Summary. ........................................................................................................................................55
3.8. Exercises. ........................................................................................................................................55
Chapter 4. Regular expressions. ......................................................................................................................56
4.1. Regular expressions. .......................................................................................................................56
4.1.1. What are regular expressions?. ..............................................................................................56
4.1.2. Regular expression metacharacters. ......................................................................................56
4.1.3. Basic versus extended regular expressions ...........................................................................57
4.2. Examples using grep. ......................................................................................................................57
4.2.1. What is grep?. ........................................................................................................................57
4.2.2. Grep and regular expressions. ...............................................................................................58
4.3. Pattern matching using Bash features. ............................................................................................60
4.3.1. Character ranges. ...................................................................................................................60
ii
8011928.004.png
Bash Guide for Beginners
Table of Contents
4.3.2. Character classes. ..................................................................................................................60
4.4. Summary. ........................................................................................................................................61
4.5. Exercises. ........................................................................................................................................61
Chapter 5. The GNU sed stream editor. .........................................................................................................62
5.1. Introduction. ....................................................................................................................................62
5.1.1. What is sed?.. .........................................................................................................................62
5.1.2. sed commands. ......................................................................................................................62
5.2. Interactive editing. ..........................................................................................................................63
5.2.1. Printing lines containing a pattern. ........................................................................................63
5.2.2. Deleting lines of input containing a pattern. .........................................................................64
5.2.3. Ranges of lines. .....................................................................................................................64
5.2.4. Find and replace with sed. .....................................................................................................65
5.3. Non−interactive editing. .................................................................................................................66
5.3.1. Reading sed commands from a file. ......................................................................................66
5.3.2. Writing output files ...............................................................................................................66
5.4. Summary. ........................................................................................................................................67
5.5. Exercises. ........................................................................................................................................68
Chapter 6. The GNU awk programming language. .......................................................................................69
6.1. Getting started with gawk. ..............................................................................................................69
6.1.1. What is gawk?. ......................................................................................................................69
6.1.2. Gawk commands. ..................................................................................................................69
6.2. The print program. ..........................................................................................................................70
6.2.1. Printing selected fields. .........................................................................................................70
6.2.2. Formatting fields. ..................................................................................................................71
6.2.3. The print command and regular expressions. ........................................................................72
6.2.4. Special patterns.. ....................................................................................................................72
6.2.5. Gawk scripts. .........................................................................................................................73
6.3. Gawk variables. ...............................................................................................................................73
6.3.1. The input field separato. .......................................................................................................73
6.3.2. The output separators. ...........................................................................................................74
6.3.3. The number of records. .........................................................................................................75
6.3.4. User defined variables. ..........................................................................................................75
6.3.5. More examples. .....................................................................................................................76
6.3.6. The printf program. ...............................................................................................................76
6.4. Summary. ........................................................................................................................................77
6.5. Exercises. ........................................................................................................................................77
Chapter 7. Conditional statements. .................................................................................................................79
7.1. Introduction to if. ............................................................................................................................79
7.1.1. General. .................................................................................................................................79
7.1.2. Simple applications of i.. ......................................................................................................82
7.2. More advanced if usage. .................................................................................................................84
7.2.1. if/then/else constructs. ...........................................................................................................84
7.2.2. if/then/elif/else constructs.. ....................................................................................................87
7.2.3. Nested if statements.. .............................................................................................................87
iii
8011928.005.png
Bash Guide for Beginners
Table of Contents
7.2.4. Boolean operations. ...............................................................................................................88
7.2.5. Using the exit statement and if. .............................................................................................89
7.3. Using case statements. ....................................................................................................................90
7.3.1. Simplified conditions. ...........................................................................................................90
7.3.2. Initscript example. .................................................................................................................91
7.4. Summary. ........................................................................................................................................92
7.5. Exercises. ........................................................................................................................................92
Chapter 8. Writing interactive scripts. ...........................................................................................................94
8.1. Displaying user messages. ..............................................................................................................94
8.1.1. Interactive or not?. .................................................................................................................94
8.1.2. Using the echo built−in command ........................................................................................94
8.2. Catching user input. ........................................................................................................................97
8.2.1. Using the read built−in command. ........................................................................................97
8.2.2. Prompting for user inpu. .......................................................................................................98
8.2.3. Redirection and file descriptors. ............................................................................................99
8.2.4. File input and outpu.. ..........................................................................................................101
8.3. Summary. ......................................................................................................................................105
8.4. Exercises. ......................................................................................................................................106
Chapter 9. Repetitive tasks. ...........................................................................................................................107
9.1. The for loop. ..................................................................................................................................107
9.1.1. How does it work?. ..............................................................................................................107
9.1.2. Examples. ............................................................................................................................107
9.2. The while loop. .............................................................................................................................108
9.2.1. What is it?. ...........................................................................................................................108
9.2.2. Examples. ............................................................................................................................108
9.3. The until loop. ...............................................................................................................................111
9.3.1. What is it?. ...........................................................................................................................111
9.3.2. Example. ..............................................................................................................................111
9.4. I/0 redirection and loops. ..............................................................................................................112
9.4.1. Input redirection. .................................................................................................................112
9.4.2. Output redirection.. ..............................................................................................................112
9.5. Break and continue. ......................................................................................................................113
9.5.1. The break built−in. ..............................................................................................................113
9.5.2. The continue built−in. .........................................................................................................114
9.5.3. Examples. ............................................................................................................................114
9.6. Making menus with the select built−in. ........................................................................................115
9.6.1. General. ...............................................................................................................................115
9.6.2. Submenus. ...........................................................................................................................117
9.7. The shift built−in ..........................................................................................................................117
9.7.1. What does it do?. .................................................................................................................117
9.7.2. Examples. ............................................................................................................................117
9.8. Summary. ......................................................................................................................................118
9.9. Exercises. ......................................................................................................................................119
iv
8011928.001.png
Zgłoś jeśli naruszono regulamin