Download Bash Guide for Beginners
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 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 document...........................................................................................................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 SHell............................................................................................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.......................................................................................................13 1.3.3. Executing programs from a script.........................................................................................13 1.4. Building blocks...............................................................................................................................13 1.4.1. Shell building blocks.............................................................................................................14 1.5. Developing good scripts.................................................................................................................16 1.5.1. Properties of good scripts......................................................................................................16 1.5.2. Structure................................................................................................................................16 1.5.3. Terminology..........................................................................................................................16 1.5.4. A word on order and logic.....................................................................................................17 1.5.5. An example Bash script: mysystem.sh..................................................................................17 1.5.6. Example init script.................................................................................................................19 1.6. Summary.........................................................................................................................................20 1.7. Exercises.........................................................................................................................................20 Chapter 2. Writing and debugging scripts.....................................................................................................22 2.1. Creating and running a script..........................................................................................................22 2.1.1. Writing and naming...............................................................................................................22 2.1.2. script1.sh................................................................................................................................22 2.1.3. Executing the script...............................................................................................................23 2.2. Script basics....................................................................................................................................25 2.2.1. Which shell will run the script?.............................................................................................25 2.2.2. Adding comments..................................................................................................................25 2.3. Debugging Bash scripts..................................................................................................................26 2.3.1. Debugging on the entire script..............................................................................................26 2.3.2. Debugging on part(s) of the script.........................................................................................27 2.4. Summary.........................................................................................................................................29 2.5. Exercises.........................................................................................................................................29 i