SP Posted November 7, 2011 Share Posted November 7, 2011 Table of Contents 1. Tour de Shell Scripting ...........................................................................1 Introduction to the Course structure ....................................................1 Adage 1.0: ................................................................................1 What you will need ....................................................................1 What is Shell Scripting ..............................................................2 Introduction to info, man and the whatis database ................................4 info pages .................................................................................4 Man Pages ................................................................................9 The whatis Database ................................................................12 Revising some Basic Commands ......................................................13 The who command ..................................................................14 The w Command .....................................................................16 The "date" command ...............................................................17 The 'echo' command ................................................................20 File Commands .......................................................................23 System Commands ..................................................................29 stdin, stdout, stderr ..........................................................................33 stdin .......................................................................................34 stdout ......................................................................................34 Using stdin and stdout simultaneously .......................................37 Appending to a file ..................................................................37 stderr ......................................................................................38 stdout, stderr and using the ampersand (&) ................................40 Exercises: ................................................................................41 Unamed Pipes .........................................................................41 2. The Shell .............................................................................................45 Introduction ....................................................................................45 What is the login shell? ....................................................................46 Exercises .................................................................................48 The job of the shell ..........................................................................50 Command Interpreter ...............................................................50 Allows for variables to be set ....................................................51 I/O redirection .........................................................................51 Pipelines .................................................................................51 Customising your environment .................................................51 Conclusion: .............................................................................51 3. Regular Expressions .............................................................................53 Introduction ....................................................................................53 What are regular expressions? ..........................................................53 The fullstop .............................................................................54 Let's explore "sed" syntax ........................................................55 Square brackets ( [ ] ), the caret ( ^ ) and the dollar ( $ ) .............56 Using sed and pipes .................................................................59 The splat (asterisk) ( * ) ...........................................................61 The plus operator ( + ) .............................................................63 Returning from detour to our discussion on curly braces… ........66 RE's are greedy for matching patterns .......................................68 Placeholders and word boundaries ............................................70 Word boundaries ( < and > ) - a formal explanation ...................72 The tr command ..............................................................................73 The cut command ............................................................................75 First Example in stages: ...........................................................77 Second Example in stages: .......................................................78 Third example in stages ...........................................................79 Exercises: ................................................................................81 The paste command .........................................................................81 The uniq command ..........................................................................83 The Sort command ..........................................................................84 The grep command ..........................................................................88 grep, egrep and fgrep ...............................................................92 Exercises: ................................................................................93 Challenge sequence: ................................................................93 4. Practically Shell Scripting ....................................................................95 Section Techniques to use when writing, saving and executing Shell Scripts ............................................................................................95 Detour: File Extension labels ....................................................96 Comments in scripts ................................................................97 Variables .................................................................................98 Shebang or hashpling #! .........................................................100 Exit ......................................................................................102 Null and unset variables .........................................................102 Variable Expansion ................................................................105 Environmental vs shell variables .............................................106 Arithmetic in the shell ............................................................108 Examples ..............................................................................110 Exercises: ..............................................................................112 5. Using Quotation marks in the Shell .....................................................115 Introduction ..................................................................................115 Single Quotes or "ticks" .................................................................115 Exercises: ..............................................................................118 Double Quotes ..............................................................................118 Exercises ...............................................................................121 Backticks ......................................................................................121 Exercises: ..............................................................................124 Shell Arithmetic's with expr and back quotes ...................................125 Another tip when using quotation marks .........................................126 6. So, you want an Argument? ................................................................129 Introduction ..................................................................................129 Positional Parameters 0 and 1 through 9 ..........................................130 vi Shell Scripting Exercises: ..............................................................................132 Challenge sequence: ..............................................................132 Other arguments used with positional parameters .............................133 $# How many positional arguments have we got ? ...................133 $* - display all positional parameters ......................................133 Using the "shift" command - for more than 9 positional parameters ..............................................................................................134 Exit status of the previous command .......................................136 7. Where to From Here? .........................................................................139 Making Decisions ..........................................................................139 Testing for a true or false condition .........................................139 Different types of tests ...........................................................140 Logical Operators ..........................................................................146 Exercises: ..............................................................................150 Conditions in the shell ...................................................................151 Using the "if" statement .........................................................151 The "if" "then" "else" statement ..............................................152 The "elif" statement ...............................................................153 The "case" statement ..............................................................154 Exercises ...............................................................................156 Challenge sequence: ..............................................................157 Debugging your scripts ..................................................................157 The NULL command .............................................................158 The || and && commands .......................................................159 Exercises: ..............................................................................160 8. Loops ................................................................................................163 Introduction ..................................................................................163 The "for" loop .......................................................................163 while and until loops ..............................................................170 getopts Using arguments and parameters .........................................178 Exercises: ..............................................................................181 9. User input to a script ..........................................................................183 Introduction ..................................................................................183 The read command ........................................................................183 Presenting the output .....................................................................188 The echo command ................................................................188 The printf command ..............................................................191 10. Additional Information .....................................................................197 The shell environmental variables pertaining to scripting .................197 The Source command ....................................................................199 Exercises: ..............................................................................202 the exec command .........................................................................202 Other methods of executing a script or a series of commands ....204 11. Positional parameters & variables re-visited .......................................209 Introduction ..................................................................................209 PARAM:-value .............................................................................210 PARAM:=value .............................................................................211 vii ${param:+value} ...........................................................................212 ?${variable%pattern} .....................................................................214 MAGIC%%r*a ..............................................................................215 variable#pattern .............................................................................216 variable:OFFSET:LENGTH ...........................................................217 #variable .......................................................................................219 Re-assigning parameters with set ....................................................220 Explaining the default field separator field - IFS ..............................221 Setting variables as "readonly" .......................................................222 Exercises: ..............................................................................222 Challenge sequences: .............................................................223 12. Bits and pieces - tying up the loose ends ............................................225 The eval command ........................................................................225 Running commands in the background using & ...............................227 Traps and signals ...........................................................................229 Signals ..................................................................................229 Traps ....................................................................................232 Exercises: ..............................................................................236 File descriptors re-visited ...............................................................236 Exercises ...............................................................................241 Here documents .............................................................................242 Exercises ...............................................................................246 Functions ......................................................................................246 Exercises: ..............................................................................249 Challenge sequence ...............................................................249 A. Writing html pages ............................................................................251 B. An introduction to dialog ...................................................................253 C. A Comparisson of bash, tsch and ksh ..................................................255 Index ....................................................................................................257 viii Shell Scripting List of Figures 1.1. The structure of the info pages .............................................................5 2.1. Parent- and sub-shells ........................................................................47 6.1. Using Shift Command to access parameters ......................................134 10.1. Parent- and sub-shells ....................................................................200 x This page intentionally left blank List of Tables 1.1. Wildcards .........................................................................................23 1.2. Standard Input, Standard Output and Standard Error ............................34 download>>>> This is the hidden content, please Sign In or Sign Up password>>>> level-23.biz Link to comment Share on other sites More sharing options...
Recommended Posts