1 / 68

Midterm Solutions

Midterm Solutions. Stats. Average: 62.7 Median: 64.5 Std dev: 16.1 Range: 18-98 >=80: 10 A 70-80: 19 B+/A- 60-70: 22 B/B+ 50-60: 10 B-/B 40-50: 12 C/B- <40: 6. HW1. Average: 88 Median: 64.5 Std dev: 11.2 Range: 53-100. Question 1. a, c, e a, c, d, e. Question 2.

maree
Download Presentation

Midterm Solutions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm Solutions

  2. Stats • Average: 62.7 • Median: 64.5 • Std dev: 16.1 • Range: 18-98 • >=80: 10 A • 70-80: 19 B+/A- • 60-70: 22 B/B+ • 50-60: 10 B-/B • 40-50: 12 C/B- • <40: 6

  3. HW1 • Average: 88 • Median: 64.5 • Std dev: 11.2 • Range: 53-100

  4. Question 1 • a, c, e • a, c, d, e

  5. Question 2 • 1: file1 • 1: file2 2: > 3: file1 • 1: file1 2: > file2 • 1: echo file1 • 1: file1 • 1: file1 2: file2 • 1: file1 2: file2

  6. Question 3 • xxxx:--xx[-N][-][[+][.][B]][...][-N][-][[+][.][B]][...]

  7. Question 3 (cont.) • zusage: od file ...]Total 11 records • $ odod: illegal optionusage: odDone!

  8. Question 4 • find ~ ! -type d -perm -001 -mtime +10 \ \( -name “*.xml” -o -name “*.html” \)

  9. Question 5 • cut -d, -f4 | grep -c ‘^A$’ • egrep ‘,MSCS,(D|F),(D|F)$’ |\ cut -d, -f1 • grep ‘,\([^,]*\),\1$’ | cut -d,\ -f2 | cut -c6- • cut -d’ ‘ -f1 | sort | uniq |\ tr ‘a-z’ ‘A-Z’ • grep -v ‘^[^,]*,[^,]*,MSIS,’ |\ grep -c ‘T[^ ]* B[^ ]*’

  10. Question 6 { lines[$0]=1 for (i=1; i<=NF; i++) { words[$i]=1 } } END { for (l in lines) { nl++ } for (w in words) { nw++ } printf("There are %d words and %d lines.\n", nw, nl) }

  11. Question 7 • awk -F, ‘{print NF}’ • tr -cd ‘,\n’ | wc -c

  12. Question 8 #!/bin/sh if [ $# -lt 2 ] ; then exit 1 fi DELIM="$1" shift fileprinted=0 for file in "$@" ; do samecol=1 nfield=-1 while read line ; do num=`echo $line | \ awk -F"$DELIM" '{print NF}'`

  13. if [ $nfield -eq -1 ] ; then nfield=$num elif [ $num -ne $nfield ] ; then samecol=0 break fi done < $file if [ $samecol -eq 1 ] ; then echo $file fileprinted=1 fi done if [ $fileprinted -eq 1 ] ; then exit 0 else exit 1 fi

  14. Lecture 10 Shell (cont) UNIX Development Tools

  15. Korn Shell / bash Features

  16. Command Substitution • Better syntax with $(command) • Allows nesting • x=$(cat $(generate_file_list)) • Backward compatible with ` … ` notation

  17. Expressions • Expressions are built-in with the [[ ]] operator if [[ $var = "" ]] … • Gets around parsing quirks of /bin/test, allows checking strings against patterns • Operations: • string==pattern • string!=pattern • string1<string2 • file1–ntfile2 • file1–otfile2 • file1–effile2 • &&, ||

  18. Patterns • Can be used to do string matching: if [[ $foo = *a* ]] if [[ $foo = [abc]* ]] • Note: patterns are like a subset of regular expressions, but different syntax

  19. Additonal Parameter Expansion • ${#param} – Length of param • ${param#pattern} – Left strip min pattern • ${param##pattern} – Left strip max pattern • ${param%pattern} – Right strip min pattern • ${param%%pattern} – Right strip max pattern • ${param-value} – Default value if param not set

  20. Variables • Variables can be arrays • foo[3]=test • echo ${foo[3]} • Indexed by number • ${#arr} is length of the array • Multiple array elements can be set at once: • set –A foo a b c d • echo ${foo[1]} • Set command can also be used for positional params: set a b c d; print $2

  21. Functions • Alternative function syntax: function name { commands} • Allows for local variables • $0 is set to the name of the function

  22. Additional Features • Built-in arithmetic: Using $((expression )) • e.g., print $(( 1 + 1 * 8 / x )) • Tilde file expansion ~ $HOME ~user home directory of user ~+ $PWD ~- $OLDPWD

  23. KornShell 93

  24. Variable Attributes • By default attributes hold strings of unlimited length • Attributes can be set with typeset: • readonly (-r) – cannot be changed • export (-x) – value will be exported to env • upper (-u) – letters will be converted to upper case • lower (-l) – letters will be converted to lower case • ljust (-L width) – left justify to given width • rjust (-R width) – right justify to given width • zfill (-Z width) – justify, fill with leading zeros • integer (-I [base]) – value stored as integer • float (-E [prec]) – value stored as C double • nameref (-n) – a name reference

  25. Name References • A name reference is a type of variable that references another variable. • nameref is an alias for typeset -n • Example: user1="jeff"user2="adam"typeset –n name="user1"print $namejeff

  26. New Parameter Expansion • ${param/pattern/str} – Replace first pattern with str • ${param//pattern/str} – Replace all patterns with str • ${param:offset:len} – Substring with offset

  27. Patterns Extended Regular Expressions Patterns • Additional pattern types so that shell patterns are equally expressive as regular expressions • Used for: • file expansion • [[ ]] • case statements • parameter expansion

  28. ANSI C Quoting • $'…' Uses C escape sequences $'\t' $'Hello\nthere' • printf added that supports C like printing: printf "You have %d apples" $x • Extensions • %b – ANSI escape sequences • %q – Quote argument for reinput • \E – Escape character (033) • %P – convert ERE to shell pattern • %H – convert using HTML conventions • %T – date conversions using date formats

  29. Associative Arrays • Arrays can be indexed by string • Declared with typeset –A • Set: name["foo"]="bar" • Reference ${name["foo"]} • Subscripts: ${!name[@]}

  30. Software Development Tools

  31. Types of Development Tools • Compilation and building: make • Managing files: RCS, SCCS, CVS • Editors: vi, emacs • Archiving: tar, cpio, pax, RPM • Configuration: autoconf • Debugging: gdb, dbx, prof, strace, purify • Programming tools: yacc, lex, lint, indent

  32. Make • make: A program for building and maintaining computer programs • developed at Bell Labs around 1978 by S. Feldman (now at IBM) • Instructions stored in a special format file called a “makefile”.

  33. Make Features • Contains the build instructions for a project • Automatically updates files based on a series of dependency rules • Supports multiple configurations for a project • Only re-compiles necessary files after a change (conditional compilation) • Major time-saver for large projects • Uses timestamps of the intermediate files • Typical usage: executable is updated from object files which are in turn compiled from source files

  34. Dependency Graph myprog link foo.o bar.o baz.o compile foo.c bar.c baz.c baz.y original generated

  35. Makefile Format • Rule Syntax: <target>: <dependency list> <command> • The <target> is a list of files that the command will generate • The <dependency list> may be files and/or other targets, and will be used to create the target • It must be a tab before <command>, or it won’t work • The first rule is the default <target> for make

  36. Examples of Invoking Make • make -f makefile • make target • make • looks for file makefile or Makefile in current directory, picks first target listed in the makefile

  37. Make: Sequence of Execution • Make executes all commands associated with target in makefile if one of these conditions is satisfied: • file target does not exist • file target exists but one of the source files in the dependency list has been modified more recently than target

  38. Example Makefile # Example Makefile CC=g++ CFLAGS=-g –Wall -DDEBUG foobar: foo.o bar.o $(CC) $(CFLAGS) –o foobar foo.o bar.o foo.o: foo.cpp foo.h $(CC) $(CFLAGS) –c foo.cpp bar.o: bar.cpp bar.h $(CC) $(CFLAGS) –c bar.cpp clean: rm foo.o bar.o foobar

  39. Make Power Features • Many built-in rules • e.g. C compilation • “Fake” targets • Targets that are not actually files • Can do just about anything, not just compile • Like the “clean” target • Forcing re-compiles • touch the required files • touch the Makefile to rebuild everything

  40. Version Control • Provide the ability to store/access and protect all of the versions of source code files • Provides the following benefits: • If program has multiple versions, it keeps track only of differences between multiple versions. • Multi-user support. Allows only one person at the time to do the editing. • Provides a way to look at the history of program development.

  41. Version Control Systems • SCCS: UNIX Source Code Control System • Rochkind, Bell Labs, 1972. • RCS: Revision Control System • Tichy, Purdue, 1980s. • CVS: Concurrent Versions System • Grune, 1986, Berliner, 1989.

  42. RCS Basic Operations • Set up a directory for RCS: • mkdir RCS • Check in a new file into the repository • ci filename • Check out a file from the repository for reading • co filename • Check out a file from the repository for writing • co –l filename • Acquires lock • Compare local copy of file to version in repository • rcsdiff [–r<ID>] filename

  43. RCS Keywords • Keywords in source files are expanded to contain RCS info at checkout • $keyword$ $keyword: value $ • Use ident to extract RCS keyword info • $Author$ Username of person checked in the revision • $Date$ Date and time of check-in • $Id$ A title that includes the RCS filename, revision number, date, author, state, and (if locked) the person who locked the file • $Revision$ The revision number assigned

  44. SCCS Equivalents Function RCS SCCS Setup mkdir RCS mkdir SCCS Check in new foo.c ci foo.c sccs create foo.c Check in update to foo.c ci foo.c sccs delta foo.c Get read-only foo.c co foo.c sccs get foo.c Get writeable foo.c co -l foo.c sccs edit foo.c Version history of foo.c rlog foo.c sccs print foo.c Compare foo.c to v1.1 rcsdiff sccs diffs –r1.1 foo.c -r1.1 foo.c

  45. CVS Major Features • No exclusive locks like RCS • No waiting around for other developers • No hurrying to make changes while others wait • Avoid the “lost update” problem • Client/Server model • Distributed software development • Front-end tool for RCS with more functions

  46. CVS Repositories • All revisions of a file in the project are in the repository (using RCS) • Work is done on the checkout (working copy) • Top-level directories are modules; checkout operates on modules • Different ways to connect

  47. CVSROOT • Environment Variable • Location of Repository • Can take different forms: • Local file system: /usr/local/cvsroot • Remote Shell: user@server:/usr/local/cvsroot • Client/Server: :pserver:user@server:/usr/local/cvsroot

  48. Getting Started • cvs [basic-options] <command> [cmd-options] [files] • Basic options: • -d <cvsroot> Specifies CVSROOT • -H Help on command • -n Dry run • Commands • import, checkout • update, commit • add, remove • status, diff, log • tag...

  49. Setting up CVS • Importing source • Generates a new module • cd into source directory • cvs –d<cvsroot> import <new-module> <vendor-branch> <release-tag> • cvs –d<cvsroot> checkout <module-name>

  50. Managing files • Add files: add (cvs add <filename>) • Remove files: remove (cvs remove <filename>) • Get latest version from repository: update • If out of sync, merges changes. Conflict resolution is manual. • Put changed version into repository: commit • Fails if repository has newer version (need update first) • View extra info: status, diff, log • Can handle binary files (no merging or diffs) • Specify a symbolic tag for files in the repository: tag

More Related