Download "user manual"

Transcript
Bash Guide for Beginners
Table of Contents
Introduction.........................................................................................................................................................1
1. Why this guide?...................................................................................................................................1
2. Who should read this book?.................................................................................................................1
3. New versions of this guide...................................................................................................................1
4. Revision History..................................................................................................................................1
5. Contributions.......................................................................................................................................2
6. Feedback..............................................................................................................................................2
7. Copyright information.........................................................................................................................2
8. What do you need?...............................................................................................................................2
9. Conventions used in this document.....................................................................................................2
10. Organization of this document...........................................................................................................3
Chapter 1. Bash and Bash scripts......................................................................................................................5
1.1. Common shell programs...................................................................................................................5
1.1.1. General shell functions............................................................................................................5
1.1.2. Shell types...............................................................................................................................5
1.2. Advantages of the Bourne Again SHell............................................................................................6
1.2.1. Bash is the GNU shell.............................................................................................................6
1.2.2. Features only found in bash.....................................................................................................6
1.3. Executing commands......................................................................................................................11
1.3.1. General..................................................................................................................................11
1.3.2. Shell built−in commands.......................................................................................................11
1.3.3. Executing programs from a script.........................................................................................12
1.4. Building blocks...............................................................................................................................12
1.4.1. Shell building blocks.............................................................................................................12
1.5. Developing good scripts.................................................................................................................14
1.5.1. Properties of good scripts......................................................................................................15
1.5.2. Structure................................................................................................................................15
1.5.3. Terminology..........................................................................................................................15
1.5.4. A word on order and logic.....................................................................................................15
1.5.5. An example Bash script: mysystem.sh..................................................................................16
1.5.6. Example init script.................................................................................................................18
1.6. Summary.........................................................................................................................................18
1.7. Exercises.........................................................................................................................................19
Chapter 2. Writing and debugging scripts.....................................................................................................20
2.1. Creating and running a script..........................................................................................................20
2.1.1. Writing and naming...............................................................................................................20
2.1.2. script1.sh................................................................................................................................20
2.1.3. Executing the script...............................................................................................................21
2.2. Script basics....................................................................................................................................23
2.2.1. Which shell will run the script?.............................................................................................23
2.2.2. Adding comments..................................................................................................................23
2.3. Debugging Bash scripts..................................................................................................................24
2.3.1. Debugging on the entire script..............................................................................................24
2.3.2. Debugging on part(s) of the script.........................................................................................25
2.4. Summary.........................................................................................................................................27
2.5. Exercises.........................................................................................................................................27
i