160 likes | 180 Views
Prepare for your exam covering Chapters 5 to 7, Appendix B, UNIX Shells, and Shell Scripts with C, Perl, and AWK examples. Review different script languages syntax and commands. Remember Homework #8 is due soon!
E N D
Homework / Exams • HW7 due today • Exam #3 next class • Covers Chapter 5 through end of Chapter 7 • and Appendix B Standard Library • Plus UNIX Shells / Processes / Shell Scripts • Homework #8 due class 28 • Review for Exam#3
Shell Scripts • Yet another programming language! • Save a sequence of UNIX commands in a file • Three different script languages with their own syntax – one for each type of UNIX shell! • C shell script file names have suffix .csh • File permission for execute must be set chmod u+x filename.csh • Then, type the filename at the UNIX prompt: % filename.csh
C Shell Script Language • Builds on existing shell commands • Adds Expressions and Assignments • Adds comments, labels, and goto • Adds C-like Control Statements foreach … end if … then … else … endif switch … case … endsw
C Shell Script Language • Assignment of expression value to variable set a = 3 set b = 5 @ a = $a + $b echo a = $a • Arithmetic only for integer variables
C Shell Script Language • Comment Syntax # is start of a comment to end of line # must be first character in file (for C shell script) #!/bin/tcsh Shebang gives path name to shell • Label Syntax name: # Usually on a line by itself • goto Syntax goto name
C Shell Script Language • Syntax for: foreach … end foreach name (wordList) … # use $name as loop variable end
C Shell Script Language • Syntax for: if … then … else … endif if (expr1) then list 1 # shell commands else if (expr2) then list 2 # shell commands else list 3 # shell commands endif
C Shell Script Language • Syntax for: switch … case … endsw switch (expr) case pattern1: list # shell commands breaksw # break out of this case case pattern2: … # same as above default: … # same as above endsw
C Shell Script Example blade64(26)% pwd /home/bobw/cs240 blade64(27)% cat showhw.csh # showhw.csh AT LEAST 1 COMMENT LINE! foreach subdir (hw1 hw2 hw3 hw4 hw5 hw6 hw7 hw8) cd $subdir ls cd .. end
C Shell Script Example • blade64(28)% showhw.csh • soln • reverse.in soln trim.in vt.in • bits.c bits.h buggy.c showbits.c soln • makefile soln • explore.c soln • alloc.c alloc.h alloctest.c makefile soln test.in • makefile mapss.c mapss.h soln testmapss.c • differ.in1 differ.in2 soln
C Shell Script Example blade64(94)% cat foo.csh # set a = 3 set b = 5 @ a = $a + $b if ($a > $b) then echo $a is greater than $b else echo $a is not greater than $b endif blade64(95)% foo.csh 8 is greater than 5 blade64(96)%
AWK Programs • Yet another programming language! • Authors: Aho, Weinberger, and Kernighan • Invoke the awk utility with a program on a file • awk program defines operations to do on each line blade64(43)% cat awk1 BEGIN { print "start of file: ", FILENAME} { print NF, $1} END {print "end of file"}
AWK Programs blade64(44)% cat foo.txt Now is the time for all good men to come to the aid blade64(45)% awk -f awk1 foo.txt start of file: foo.txt 4 Now 4 for 5 to end of file
Perl Scripts • Yet another programming language! • Practical Extraction and Reporting Language • aka Pathologically Eclectic Rubbish Lister • Falls between a Shell script and a C program • Often used instead of shell scripts, awk, sed • There are a2p and s2p translators available • You can actually write O-O perl scripts!
Perl Scripts blade64(46)% cat test.pl print "What is your name? "; chomp($name = <STDIN>); $length = length $name; ($length == 3)? print "$name has $length characters.\n": die; blade64(47)% perl test.pl What is your name? Bob Bob has 3 characters. blade64(48)% perl test.pl What is your name? Robert Died at test.pl line 4, <STDIN> line 1.
Review for Exam #3 • The floor is now open for questions