w mach02 - Bash Guide for Beginners, Książki IT
[ Pobierz całość w formacie PDF ] //-->Bash Guide for BeginnersMachtelt GarrelsGarrels BVBA<tille wants no spam _at_ garrels dot be>Version 1.11 Last updated 20081227 EditionBash Guide for BeginnersTable of ContentsIntroduction.........................................................................................................................................................11. Why this guide?...................................................................................................................................12. Who should read this book?.................................................................................................................13. New versions, translations and availability.........................................................................................24. Revision History..................................................................................................................................25. Contributions.......................................................................................................................................36. Feedback..............................................................................................................................................37. Copyright information.........................................................................................................................38. What do you need?...............................................................................................................................49. Conventions used in this document.....................................................................................................410. Organization of this document...........................................................................................................5Chapter 1. Bash and Bash scripts......................................................................................................................61.1. Common shell programs...................................................................................................................61.1.1. General shell functions............................................................................................................61.1.2. Shell types...............................................................................................................................61.2. Advantages of the Bourne Again SHell............................................................................................71.2.1. Bash is the GNU shell.............................................................................................................71.2.2. Features only found in bash.....................................................................................................71.3. Executing commands......................................................................................................................121.3.1. General..................................................................................................................................121.3.2. Shell built-in commands........................................................................................................121.3.3. Executing programs from a script.........................................................................................131.4. Building blocks...............................................................................................................................131.4.1. Shell building blocks.............................................................................................................131.5. Developing good scripts.................................................................................................................151.5.1. Properties of good scripts......................................................................................................161.5.2. Structure................................................................................................................................161.5.3. Terminology..........................................................................................................................161.5.4. A word on order and logic.....................................................................................................161.5.5. An example Bash script: mysystem.sh..................................................................................171.5.6. Example init script................................................................................................................18.1.6. Summary.........................................................................................................................................191.7. Exercises.........................................................................................................................................19Chapter 2. Writing and debugging scripts.....................................................................................................212.1. Creating and running a script..........................................................................................................212.1.1. Writing and naming...............................................................................................................212.1.2. script1.sh................................................................................................................................222.1.3. Executing the script...............................................................................................................232.2. Script basics....................................................................................................................................242.2.1. Which shell will run the script?.............................................................................................242.2.2. Adding comments..................................................................................................................242.3. Debugging Bash scripts..................................................................................................................252.3.1. Debugging on the entire script..............................................................................................252.3.2. Debugging on part(s) of the script ........................................................................................26.2.4. Summary.........................................................................................................................................282.5. Exercises.........................................................................................................................................28iBash Guide for BeginnersTable of ContentsChapter 3. The Bash environment..................................................................................................................293.1. Shell initialization files...................................................................................................................293.1.1. System-wide configuration files............................................................................................293.1.2. Individual user configuration files........................................................................................313.1.3. Changing shell configuration files........................................................................................333.2. Variables.........................................................................................................................................343.2.1. Types of variables.................................................................................................................343.2.2. Creating variables..................................................................................................................373.2.3. Exporting variables................................................................................................................383.2.4. Reserved variables.................................................................................................................393.2.5. Special parameters.................................................................................................................413.2.6. Script recycling with variables..............................................................................................433.3. Quoting characters..........................................................................................................................443.3.1. Why?.....................................................................................................................................453.3.2. Escape characters..................................................................................................................453.3.3. Single quotes.........................................................................................................................453.3.4. Double quotes........................................................................................................................453.3.5. ANSI-C quoting....................................................................................................................463.3.6. Locales...................................................................................................................................463.4. Shell expansion...............................................................................................................................463.4.1. General..................................................................................................................................463.4.2. Brace expansion....................................................................................................................463.4.3. Tilde expansion.....................................................................................................................473.4.4. Shell parameter and variable expansion................................................................................473.4.5. Command substitution...........................................................................................................483.4.6. Arithmetic expansion............................................................................................................493.4.7. Process substitution...............................................................................................................503.4.8. Word splitting........................................................................................................................503.4.9. File name expansion..............................................................................................................513.5. Aliases.............................................................................................................................................513.5.1. What are aliases?...................................................................................................................513.5.2. Creating and removing aliases..............................................................................................523.6. More Bash options..........................................................................................................................533.6.1. Displaying options.................................................................................................................533.6.2. Changing options...................................................................................................................543.7. Summary.........................................................................................................................................553.8. Exercises.........................................................................................................................................55Chapter 4. Regular expressions.......................................................................................................................564.1. Regular expressions........................................................................................................................564.1.1. What are regular expressions?...............................................................................................564.1.2. Regular expression metacharacters.......................................................................................564.1.3. Basic versus extended regular expressions...........................................................................574.2. Examples using grep.......................................................................................................................574.2.1. What is grep?.........................................................................................................................574.2.2. Grep and regular expressions................................................................................................584.3. Pattern matching using Bash features.............................................................................................604.3.1. Character ranges....................................................................................................................60iiBash Guide for BeginnersTable of ContentsChapter 4. Regular expressions4.3.2. Character classes...................................................................................................................604.4. Summary.........................................................................................................................................614.5. Exercises.........................................................................................................................................61Chapter 5. The GNU sed stream editor..........................................................................................................625.1. Introduction.....................................................................................................................................625.1.1. What is sed?...........................................................................................................................625.1.2. sed commands.......................................................................................................................625.2. Interactive editing...........................................................................................................................635.2.1. Printing lines containing a pattern.........................................................................................635.2.2. Deleting lines of input containing a pattern..........................................................................645.2.3. Ranges of lines......................................................................................................................645.2.4. Find and replace with sed......................................................................................................655.3. Non-interactive editing...................................................................................................................665.3.1. Reading sed commands from a file.......................................................................................665.3.2. Writing output files...............................................................................................................665.4. Summary.........................................................................................................................................675.5. Exercises.........................................................................................................................................68Chapter 6. The GNU awk programming language........................................................................................696.1. Getting started with gawk...............................................................................................................696.1.1. What is gawk?.......................................................................................................................696.1.2. Gawk commands...................................................................................................................696.2. The print program...........................................................................................................................706.2.1. Printing selected fields..........................................................................................................706.2.2. Formatting fields...................................................................................................................716.2.3. The print command and regular expressions.........................................................................726.2.4. Special patterns......................................................................................................................726.2.5. Gawk scripts..........................................................................................................................736.3. Gawk variables................................................................................................................................736.3.1. The input field separator.......................................................................................................73.6.3.2. The output separators............................................................................................................746.3.3. The number of records..........................................................................................................756.3.4. User defined variables...........................................................................................................766.3.5. More examples......................................................................................................................766.3.6. The printf program................................................................................................................776.4. Summary.........................................................................................................................................776.5. Exercises.........................................................................................................................................77Chapter 7. Conditional statements..................................................................................................................797.1. Introduction to if.............................................................................................................................797.1.1. General..................................................................................................................................797.1.2. Simple applications of if.......................................................................................................82.7.2. More advanced if usage..................................................................................................................847.2.1. if/then/else constructs............................................................................................................847.2.2. if/then/elif/else constructs......................................................................................................877.2.3. Nested if statements...............................................................................................................88iiiBash Guide for BeginnersTable of ContentsChapter 7. Conditional statements7.2.4. Boolean operations................................................................................................................887.2.5. Using the exit statement and if..............................................................................................897.3. Using case statements.....................................................................................................................907.3.1. Simplified conditions............................................................................................................907.3.2. Initscript example..................................................................................................................927.4. Summary.........................................................................................................................................927.5. Exercises.........................................................................................................................................93Chapter 8. Writing interactive scripts............................................................................................................948.1. Displaying user messages...............................................................................................................948.1.1. Interactive or not?..................................................................................................................948.1.2. Using the echo built-in command.........................................................................................948.2. Catching user input.........................................................................................................................978.2.1. Using the read built-in command..........................................................................................978.2.2. Prompting for user input.......................................................................................................98.8.2.3. Redirection and file descriptors.............................................................................................998.2.4. File input and output...........................................................................................................101.8.3. Summary.......................................................................................................................................1068.4. Exercises.......................................................................................................................................106Chapter 9. Repetitive tasks............................................................................................................................1089.1. The for loop...................................................................................................................................1089.1.1. How does it work?...............................................................................................................1089.1.2. Examples.............................................................................................................................1089.2. The while loop..............................................................................................................................1099.2.1. What is it?............................................................................................................................1099.2.2. Examples.............................................................................................................................1109.3. The until loop................................................................................................................................1129.3.1. What is it?............................................................................................................................1129.3.2. Example...............................................................................................................................1129.4. I/O redirection and loops..............................................................................................................1139.4.1. Input redirection..................................................................................................................1139.4.2. Output redirection................................................................................................................1139.5. Break and continue.......................................................................................................................1149.5.1. The break built-in................................................................................................................1149.5.2. The continue built-in...........................................................................................................1159.5.3. Examples.............................................................................................................................1169.6. Making menus with the select built-in..........................................................................................1179.6.1. General................................................................................................................................1179.6.2. Submenus............................................................................................................................1189.7. The shift built-in...........................................................................................................................1189.7.1. What does it do?..................................................................................................................1189.7.2. Examples.............................................................................................................................1199.8. Summary.......................................................................................................................................1209.9. Exercises.......................................................................................................................................120iv
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plmement.xlx.pl
|