Homework / Exams - UMass Boston Computer Science

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
1
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
2
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
3
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
4
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
5
C Shell Script Language
• Syntax for: foreach … end
foreach name (wordList)
…
# use $name as loop variable
end
6
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
7
C Shell Script Language
• Syntax for: switch … case … endsw
switch (expr)
case pattern1:
list
breaksw
case pattern2:
…
default:
…
endsw
# shell commands
# break out of this case
# same as above
# same as above
8
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
9
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
10
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)%
11
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"}
12
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
13
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!
14
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.
15
Review for Exam #3
• The floor is now open for questions
16