Announcements Check course web page under assignments for FAQs - - PowerPoint PPT Presentation

announcements
SMART_READER_LITE
LIVE PREVIEW

Announcements Check course web page under assignments for FAQs - - PowerPoint PPT Presentation

Announcements Check course web page under assignments for FAQs Read FAQs before sending mail Assignment Clarification Semantics of = differ slightly between chmod and chmod2: Suppose file1 has permission rwxrwxrwx chmod u=rw


slide-1
SLIDE 1

Announcements

Check course web page under assignments

for FAQs

Read FAQs before sending mail

slide-2
SLIDE 2

Assignment Clarification

Semantics of = differ slightly between chmod

and chmod2:

Suppose file1 has permission rwxrwxrwx chmod u=rw file1 gives rw-rwxrwx chmd2 {u=rw} file1 gives rw------- So the = in chmod2 is absolute for everything, but

chmod it affects only the party specified (ie. u).

slide-3
SLIDE 3

Clarifications Cont’d

C vs. C++: I encourage you to do the

assignment in C++, but not mandatory

Do not have to use all of C++, like streams.

OK to embed C function calls inside implementation of objects

Part 1 of assignment: no prompting! Part 2 of assignment: when using –e option,

use quotes: findcode –e ‘{.txt;.ini}’ because ; is a special character for the shell

slide-4
SLIDE 4

Getting input

Here is one way:

//-------------------------------------------------------------------------------------------- int main(void) { char buf[MAX_LINE_LENGTH]; while(fgets(buf,MAX_LINE_LENGTH,stdin)) { //strip the new line: buf[strlen(buf)-1]=0; processLine(buf); } }

slide-5
SLIDE 5

Changing permissions:

You will need stat(), and chmod() stat() was talked about in tutorial today. int chmod(const char *path, mode_t mode); Not the same as the command chmod! path tells chmod() which file’s permission to

change

mode is the octal number we talked about

last week

slide-6
SLIDE 6

Assignment marking criteria

Tests on basic functionality ~ 40% How solid is your code? ~ 40%

Tests Code inspection: memory leaks, dangling file

descriptors, memory corruption

Coding style ~20% Crashes – seg faults – are more serious than

a failed test

slide-7
SLIDE 7

Coding style: a sample of criteria

Appropriate arrangement of modules Magic numbers Variable naming Code duplication Giant methods Unnecessarily complicated or inefficient code Utterly unsafe code And…comments

slide-8
SLIDE 8

Comments: under commented

What does this code do? don’t answer if you know shell scripting

if [ $# -ne 1 ] then exit 1 fi if [ ! -d "$1" ] then exit 2 fi cd $1 ls -a | cpio -o >/dev/rmt0 if [ $? -eq 0 ] then rm * else exit 3 fi

slide-9
SLIDE 9

About right…

# A program to backup files in a directory. Removes the files if # backup is successful. if [ $# -ne 1 ] # $# refers to the number of args then exit 1 fi if [ ! -d "$1" ] # -d tests if something is a directory, $1 = 1st arg then exit 2 fi cd $1 ls -a | cpio -o >/dev/rmt0 if [ $? -eq 0 ] # $? = return value. 0 if successful. then rm * else exit 3 fi

slide-10
SLIDE 10

Overdone:

# A program to backup files in a directory. Removes the files if # backup is successful. if [ $# -ne 1 ] # $# refers to the number of args then exit 1 # quit the program with code 1 fi if [ ! -d "$1" ] # -d tests if something is a directory, $1 = 1st arg then exit 2 # quite the program with code 2 fi cd $1 ls -a | cpio -o >/dev/rmt0 if [ $? -eq 0 ] # $? = return value. 0 if successful. then rm * # remove all files… else exit 3 # exit with code 3… fi

slide-11
SLIDE 11

Software Tools

slide-12
SLIDE 12

Absolute Basics

Command Description cd change current working directory pwd print current working directory mkdir creates a directory rmdir removes directory, must be empty rm removes a file. cp copies files/dirs mv moves files/dirs chmod change file permissions – discussed in class

slide-13
SLIDE 13

Basics: covered in web tutorial

Command Description ls lists files – more discussion to come in class cat displays a file more like cat, but displays a file 1 screen at a time ps lists processes kill kills an active process who displays currently logged on users finger displays more details about a user su switch accounts du disk usage head, tail display 1st/last couple of lines wc counts the number of lines,words, characters in a file lpr, lpq, lprm printing related commands clear clears the screen

slide-14
SLIDE 14

Advanced tools – topic today

Command Description grep,egrep search file(s) for strings sort sorts the contents of files uniq eliminates duplicate lines find search for files and much more tar archive files/dirs diff show change(s) made to a file by comparing to new version compress, uncompress, gzip, gunzip compress/decompress files sed stream editor – transforms text as it streams through

slide-15
SLIDE 15

grep, egrep & regular expressions

grep, egrep searches for text Uses regular expressions

  • Reg. Exprn = sequence of ordinary chars and special

chars (\, ^, *, $, [],.)

  • Simplest reg. Exprn = just a string:

werewolf:~% grep 'con' socket*.cpp socketclient.cpp: if( connect(sockfd,(struct sockaddr *)&server,SIZE)==-1) socketclient.cpp: printf("connect call fails\n"); socketserv.cpp: printf("waiting for connection...\n"); socketserv.cpp: continue; socketserv.cpp: //parent should close the new socket connection.

slide-16
SLIDE 16

Special characters

Dot (.) matches any 1 character:

w.r matches lowercase, worship or warning.

[] restricts range:

w[ea]r matches lowercase, warning Supports negation with w[^ea]r (anything but) Specify range with dash(-) : w[a-z]r

\{ and \} matches specific # of chars:

n\{2,4\}

slide-17
SLIDE 17

Special chars cont’d

* matches zero or more of the preceding

expression:

grep 'p*' myfile displays _____________________ grep 'ppp*' myfile displays___________________

^ anchors in front, $ anchors at the end

grep '^[Tt]he' myfile displays _________________ grep '\.$' myfile displays_____________________

slide-18
SLIDE 18

egrep

Like grep, but supports more matching

  • ptions

+ matches 1 or more occurrence ? Matches 0 or 1 occurrence of anything Allows search for A or B using |

egrep 'dd+|socket' socket*.cpp displays:

________________________________

slide-19
SLIDE 19

Sort

Name Age Team Prelim Final Stanford, Jeffrey 25 HIMA 47.07 46.32 Liggett, Michael 27 DYNA 47.25 48.12 Baker, Chase 29 GMUP 56.28 57.79 Kittredge, Brad 25 TOC 45.05 46.22 Richner, Thomas 27 UCLA 50.00 48.79 McCormick, Aaron 27 RMM 49.00 49.30 Thorum, Thomas 29 UTAH 50.00 49.45 Paul, Darcy 26 NEM 52.00 50.17 Welting, Evan 27 DYNA 50.50 51.04 Wanie, Lee 28 TOC 46.00 46.39 Linderman, Ross 25 PNA 52.55 51.17 Wen, Patrick 29 UCLA 57.50 55.93 Frohlich, Jon 29 UTAH 49.10 49.20 Parnes, Jason 29 SDSM 63.00 59.11 Perunovich, Steven 27 HIMA 59.50 52.93

slide-20
SLIDE 20

Sort

$ sort swimresults Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 McCormick, Aaron 27 RMM 49.00 49.30 Parnes, Jason 29 SDSM 63.00 59.11 Paul, Darcy 26 NEM 52.00 50.17 Perunovich, Steven 27 HIMA 59.50 52.93 Richner, Thomas 27 UCLA 50.00 48.79 Stanford, Jeffrey 25 HIMA 47.07 46.32 Thorum, Thomas 29 UTAH 50.00 49.45 Wanie, Lee 28 TOC 46.00 46.39 Welting, Evan 27 DYNA 50.50 51.04 Wen, Patrick 29 UCLA 57.50 55.93 $

slide-21
SLIDE 21

Skip fields with +

$sort +2 swimresults Wanie, Lee 28 TOC 46.00 46.39 Paul, Darcy 26 NEM 52.00 50.17 Baker, Chase 29 GMUP 56.28 57.79 Wen, Patrick 29 UCLA 57.50 55.93 Welting, Evan 27 DYNA 50.50 51.04 Parnes, Jason 29 SDSM 63.00 59.11 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45 Linderman, Ross 25 PNA 52.55 51.17 Kittredge, Brad 25 TOC 45.05 46.22 Richner, Thomas 27 UCLA 50.00 48.79 Liggett, Michael 27 DYNA 47.25 48.12 McCormick, Aaron 27 RMM 49.00 49.30 Stanford, Jeffrey 25 HIMA 47.07 46.32 Perunovich, Steven 27 HIMA 59.50 52.93

  • Why didn’t it work??
slide-22
SLIDE 22

Skip leading spaces with –b

% sort -b +2 swimresults Stanford, Jeffrey 25 HIMA 47.07 46.32 Linderman, Ross 25 PNA 52.55 51.17 Kittredge, Brad 25 TOC 45.05 46.22 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 Welting, Evan 27 DYNA 50.50 51.04 Perunovich, Steven 27 HIMA 59.50 52.93 McCormick, Aaron 27 RMM 49.00 49.30 Richner, Thomas 27 UCLA 50.00 48.79 Wanie, Lee 28 TOC 46.00 46.39 Baker, Chase 29 GMUP 56.28 57.79 Parnes, Jason 29 SDSM 63.00 59.11 Wen, Patrick 29 UCLA 57.50 55.93 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45

slide-23
SLIDE 23

Stop field number with -

Sort includes rest of fields in sort

Eg sort -b +2 swimresults, rest of line used in sort: 25 HIMA 47.07 46.32

The – operator tells sort to ignore everything

after the field indicated

sort -b +2 -3 swimresults says:

_________________________________________

slide-24
SLIDE 24

Secondary sorts

Stop field is important for secondary sorts. You can tell sort to further arrange items

considered equal.

Suppose I wanted to list the fastest people

according to age groups:

sort -b +2 -3 +5 swimresults says:

________________________________________

slide-25
SLIDE 25

Results of secondary sort

Kittredge, Brad 25 TOC 45.05 46.22 Stanford, Jeffrey 25 HIMA 47.07 46.32 Linderman, Ross 25 PNA 52.55 51.17 Paul, Darcy 26 NEM 52.00 50.17 Liggett, Michael 27 DYNA 47.25 48.12 Richner, Thomas 27 UCLA 50.00 48.79 McCormick, Aaron 27 RMM 49.00 49.30 Welting, Evan 27 DYNA 50.50 51.04 Perunovich, Steven 27 HIMA 59.50 52.93 Wanie, Lee 28 TOC 46.00 46.39 Frohlich, Jon 29 UTAH 49.10 49.20 Thorum, Thomas 29 UTAH 50.00 49.45 Wen, Patrick 29 UCLA 57.50 55.93 Baker, Chase 29 GMUP 56.28 57.79 Parnes, Jason 29 SDSM 63.00 59.11

slide-26
SLIDE 26

uniq

uniq gets rid of duplicate lines

% cat nonuniqresults Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 Linderman, Ross 25 PNA 52.55 51.17 McCormick, Aaron 27 RMM 49.00 49.30 Linderman is a duplicate item. We can use uniq to remove it: % uniq nonuniqresults Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 McCormick, Aaron 27 RMM 49.00 49.30

Be careful: uniq only removes adjacent lines

slide-27
SLIDE 27

find

Searches for files and much more 3 categories of parameters:

Search criteria Action Search qualifier

Criteria = things like name, size, last access

time …etc.

Action = what to do if criteria matches modify how find performs its search – modify

depth of search etc.

slide-28
SLIDE 28

Criteria examples:

$find /usr/include/ -name "fcntl*" –print /usr/include/asm/fcntl.h /usr/include/linux/fcntl.h /usr/include/bits/fcntl.h /usr/include/fcntl.h /usr/include/sys/fcntl.h werewolf:~% find /usr/bin - size +2000 -print /usr/bin/openssl /usr/bin/gs /usr/bin/openjade /usr/bin/gimp-1.2 /usr/bin/Xvnc /usr/bin/linux /usr/bin/pdfetex /usr/bin/vim /usr/bin/ddd /usr/bin/doxygen /usr/bin/gdb /usr/bin/splint

slide-29
SLIDE 29

Actions

$find /usr/include/ -name "fcntl*" $

The above does nothing. Why?

  • print : prints the entry
  • exec cmd {} \; : executes a command if something is

found

What does this do:

find ./temp -name "sockets*" -print -exec rm -f {} \;

slide-30
SLIDE 30

Diff

Compares two files and shows the change(s) Show changes to file1 s.t. file2 3 types of changes

Adds : firstStart a secondStart, secondStop Deletes: firstStart, firstStop d lineCount Changes: firstStart, firstStop c secondStart,

secondStop

slide-31
SLIDE 31

Example:

File1 : doesn’t actually contain line #s: 1 Baker, Chase 29 GMUP 56.28 57.79 2 Frohlich, Jon 29 UTAH 49.10 49.20 3 Kittredge, Brad 25 TOC 45.05 46.22 4 Liggett, Michael 27 DYNA 47.25 48.12 5 Linderman, Ross 25 PNA 52.55 51.17 6 Linderman, Ross 25 PNA 52.55 51.17 7 McCormick, Aaron 27 RMM 49.00 49.30 8 Parnes, Jason 29 SDSM 63.00 59.11 9 Paul, Darcy 26 NEM 52.00 50.17 10 Perunovich, Steven 27 HIMA 59.50 52.93 11 Richner, Thomas 27 UCLA 50.00 48.79 12 Stanford, Jeffrey 25 HIMA 47.07 46.32 13 Thorum, Thomas 29 UTAH 50.00 49.45 14 Wanie, Lee 28 TOC 46.00 46.39 15 Welting, Evan 27 DYNA 50.50 51.04 16 Wen, Patrick 29 UCLA 57.50 55.93

slide-32
SLIDE 32

Example Cont’d

file2: (doesn’t contain line numbers) 1 Baker, Chase 29 GMUP 56.28 57.79 2 Frohlich, Jon 29 UTAH 49.10 49.20 3 Linderman, Ross 25 PNA 52.55 51.17 4 Linderman, Ross 25 PNA 52.55 51.17 5 McCormick, Aaron 27 RMM 49.00 49.30 6 Parnes, Jason 29 SDSM 63.00 59.11 7 Paul, Darcy 26 NEM 52.00 50.17 8 Perunovich, Steven 27 HIMA 59.50 52.00 9 Richner, Thomas 27 UCLA 50.00 48.00 10 Stanford, Jeffrey 25 HIMA 47.07 46.32 11 Thorum, Thomas 29 UTAH 50.00 49.45 12 Wanie, Lee 28 TOC 46.00 46.39 13 some additions in the 14 middle 15 Welting, Evan 27 DYNA 50.50 51.04 16 Wen, Patrick 29 UCLA 57.50 55.93

slide-33
SLIDE 33

Running diff

sh-2.05a$ diff file1 file2 3,4d2 < Kittredge, Brad 25 TOC 45.05 46.22 < Liggett, Michael 27 DYNA 47.25 48.12 10,11c8,9 < Perunovich, Steven 27 HIMA 59.50 52.93 < Richner, Thomas 27 UCLA 50.00 48.79

  • > Perunovich, Steven 27 HIMA 59.50 52.00

> Richner, Thomas 27 UCLA 50.00 48.00 14a13,14 > some additions in the > middle 16c16 < Wen, Patrick 29 UCLA 57.50 55.93

  • > Wen, Patrick 29 UCLA 57.50 55.93
  • The last change looks the same…why does diff say it’s different? (-w)
slide-34
SLIDE 34

Another one of those interface things ☺

slide-35
SLIDE 35

Archiving

Use tar

  • riginally designed for archiving files to tape

Create a tar file:

tar -cvf tarfile fileList

Extract a tar file:

tar –xvf tarfile fileList

  • c = create, -x = extract, -v = verbose, -f =
  • utput to file
slide-36
SLIDE 36

Sample run:

sh-2.05a$ tar -cvf mytarfile.tar *.cpp findcode.cpp hello.cpp socketclient.cpp socketserv.cpp sh-2.05a$ tar -tvf mytarfile.tar

  • rw------- kenxu/instrs

10446 2003-05-18 02:59:36 findcode.cpp

  • rw------- kenxu/instrs

59 2003-05-12 17:59:46 hello.cpp

  • rw------- kenxu/instrs

850 2003-04-18 15:36:04 socketclient.cpp

  • rw------- kenxu/instrs

1265 2003-04-18 15:42:42 socketserv.cpp sh-2.05a$ tar -xvf mytarfile.tar hello.cpp hello.cpp sh-2.05a$ tar -xvf mytarfile.tar findcode.cpp hello.cpp socketclient.cpp socketserv.cpp sh-2.05a$

slide-37
SLIDE 37

sed

sed = stream editor Does what an editor does: add new text,

replace entire lines of text with other lines, delete lines …etc.

Designed to work with an input stream Think of sed as a filter Looks for line # or regular expression

matches.

Does something when a match is found

slide-38
SLIDE 38

Text substitution

Most common use for sed Syntax:

s/expr/str/ - replaces the 1st occurrence of the regular

expression expr with str.

Consider:

sh-2.05a$ cat file3 Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17

slide-39
SLIDE 39

Example:

sh-2.05a$ sed -e 's/ /-/' file3 Baker,-Chase 29 GMUP 56.28 57.79 Frohlich,-Jon 29 UTAH 49.10 49.20 Kittredge,-Brad 25 TOC 45.05 46.22 Liggett,-Michael 27 DYNA 47.25 48.12 Linderman,-Ross 25 PNA 52.55 51.17

  • Only replaced 1st space. Add g option for global replace

sh-2.05a$ sed -e 's/ /-/g' file3 Baker,-Chase--------------29-GMUP-------56.28------57.79-- Frohlich,-Jon-------------29-UTAH-------49.10------49.20---- Kittredge,-Brad-----------25-TOC--------45.05------46.22--- Liggett,-Michael----------27-DYNA-------47.25------48.12---- Linderman,-Ross-----------25-PNA--------52.55------51.17-- sh-2.05a$

slide-40
SLIDE 40

Restricting the range

Consider:

sed -e '2,4s/ /-/g' file3

The 2,4 tells sed to restrict the range to lines 2 through

  • 4. The same thing can also be done using pattern

matches:

Baker, Chase 29 GMUP 56.28 57.79 Frohlich,-Jon-------------29-UTAH-------49.10------49.20---- Kittredge,-Brad-----------25-TOC--------45.05------46.22--- Liggett,-Michael----------27-DYNA-------47.25------48.12---- Linderman, Ross 25 PNA 52.55 51.17 sh-2.05a$ The same thing can also be done using pattern matches:

  • sh-2.05a$ sed -e '/Frohlich/,/Liggett/s/ /-/g' file3
slide-41
SLIDE 41

Deleting text

Syntax: addressRange d Example:

sh-2.05a$ sed -e '/Frohlich/,/Liggett/d' file3 Baker, Chase 29 GMUP 56.28 57.79 Linderman, Ross 25 PNA 52.55 51.17 sh-2.05a$ Middle 3 lines are gone

slide-42
SLIDE 42

Replacing lines

Syntax:

  • addressRange c\

text

Dump to file and use the –f option Example:

sh-2.05a$ cat sedtest 2,3c\ Lines 2 and 3 have been replaced by this single line sh-2.05a$ sed -f sedtest file3 Baker, Chase 29 GMUP 56.28 57.79 Lines 2 and 3 have been replaced by this single line Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 sh-2.05a$

slide-43
SLIDE 43

Other common functionality

Append Text:

adress a\

text

Insert file:

address r filename