------------------------- Week 12 Notes for CST8129 ------------------------- -Ian! D. Allen - idallen@idallen.ca *** Keep up on your readings (Course Outline: average 5 hours/week homework) Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) ------- Review: ------- Mostly I teach "Bourne shell" syntax that is usable everywhere. Exceptions: I use $() instead of `` and "let" instead of "expr" Readings: Chapter 7 - Bourne Shell 7.9 - shell functions 7.12 - "here" documents Chapter 8 - Bourne Shell Programming 8.1 - intro 8.2 - read 8.3 - arithmetic - use ksh/bash "let" statement instead (12.3.4 p.662) 8.4 - positional parameters (especially $* and $@) 8.5 - conditional constructs and flow control - new: the "case" statement 8.6 - looping - new: the "for" command the "until" command (rare) the "shift" command the "break" command the "continue" command - new: subshells with redirection 8.7 - shell functions 8.8.5 - debugging !! important !! Chapter 15 - Debugging Shell Scripts - ignore the CSH and TCSH scripts - we use the Bourne Shell syntax more than the Bash syntax, since the Bourne syntax works everywhere - 15.2 - very important advice - use the echo and verbose switches! 15.5.1 - 15.3.3 - know your script permissions - 15.3.4 - ignore the book; never put "." in your PATH - 15.3.5 - use a correct #! line - 15.3.6 - Bourne shell scripts do not load aliases - 15.4.1 - always use -u to detect undefined/misspelled variables - 15.4.2 - don't leave off keywords - indent correctly - use "test" and "[" correctly - quote properly - test script arguments (esp. files) before you use them ---------- This week: ---------- Exercise #11 - Lunar Lander Game Try the demo: ~alleni/bin/lunarlander Read the demo files under Notes: parabola.sh.txt g*.sh.txt Chapter 8 - Bourne Shell Programming 8.8 - trapping signals See Notes file: interrupts.sh.txt, input_trap.sh.txt Chapter 3 - Regular Expressions and Pattern Matching See 4.7 p.100 (with modifications): Ordinary regexp metacharacters: ^ $ . * [] Extended regexp metacharacters: ? + | () {} A backslash reverses the meaning of a regexp character. (If it was on, it turns it off; if it was off, it usually turns it on.) Chapter 4 - The grep Family: grep, egrep, fgrep egrep uses Extended regexp by default. fgrep doesn't use regexp at all. Chapter 5 - sed, the Streamlined(sic) Editor Basic regexp substitutions: sed -e 's/regexp/text/g' -e 's/regexp/text/g' [ files... ] Notes: RE patterns use some of the same characters as GLOB patterns; but, the meanings are usually different, e.g. *.bak -vs- .*\.bak$ Programs that accept RE patterns may also accept "extended" RE patterns where more metacharacters have meaning. Backslashes in a RE pattern change the meaning of RE metacharacters. If the character had a meaning, it is turned off. If the character did not have a meaning, it is turned on (if it is an extended RE metacharacter): e.g. grep '\(dog\|cat\)' foo # turn on extended RE chars e.g. egrep '(dog|cat)' foo # extended RE chars already on e.g. grep '(613) 727-4723' foo # extended RE chars are *off* e.g. egrep '\(613\) 727-4723' foo # turn *off* extended RE chars e.g. sed -e 's/\(README\|readme\)\.txt/\1.bak/' # turn RE chars on When an RE is part of a substitution (s/regexp/text/), RE metacharacters only have meaning in the RE part of the substitution (the left side). The replacement text is just text and does not need to have any RE metacharacters escaped. However, there are a few additional metacharacters that only have meaning in the replacement text and that need to be escaped to change their meaning (e.g. \1 \2 and \&). ---------- Next week: ---------- Chapter 5 - sed, the Streamlined(sic) Editor More. Chapter 6 - The awk Utility Simple awk programs.