Announcements
Check course web page under assignments
for FAQs
Read FAQs before sending mail
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
Check course web page under assignments
Read FAQs before sending mail
Semantics of = differ slightly between chmod
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
C vs. C++: I encourage you to do the
Do not have to use all of C++, like streams.
Part 1 of assignment: no prompting! Part 2 of assignment: when using –e option,
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); } }
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
mode is the octal number we talked about
Tests on basic functionality ~ 40% How solid is your code? ~ 40%
Tests Code inspection: memory leaks, dangling file
Coding style ~20% Crashes – seg faults – are more serious than
Appropriate arrangement of modules Magic numbers Variable naming Code duplication Giant methods Unnecessarily complicated or inefficient code Utterly unsafe code And…comments
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
# 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
# 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
grep, egrep searches for text Uses regular expressions
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.
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\}
* matches zero or more of the preceding
grep 'p*' myfile displays _____________________ grep 'ppp*' myfile displays___________________
^ anchors in front, $ anchors at the end
grep '^[Tt]he' myfile displays _________________ grep '\.$' myfile displays_____________________
Like grep, but supports more matching
+ 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:
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
$ 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 $
$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
% 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
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
sort -b +2 -3 swimresults says:
Stop field is important for secondary sorts. You can tell sort to further arrange items
Suppose I wanted to list the fastest people
sort -b +2 -3 +5 swimresults says:
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
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
Searches for files and much more 3 categories of parameters:
Search criteria Action Search qualifier
Criteria = things like name, size, last access
Action = what to do if criteria matches modify how find performs its search – modify
$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
$find /usr/include/ -name "fcntl*" $
The above does nothing. Why?
What does this do:
find ./temp -name "sockets*" -print -exec rm -f {} \;
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,
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
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
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
> Richner, Thomas 27 UCLA 50.00 48.00 14a13,14 > some additions in the > middle 16c16 < Wen, Patrick 29 UCLA 57.50 55.93
Use tar
Create a tar file:
tar -cvf tarfile fileList
Extract a tar file:
tar –xvf tarfile fileList
sh-2.05a$ tar -cvf mytarfile.tar *.cpp findcode.cpp hello.cpp socketclient.cpp socketserv.cpp sh-2.05a$ tar -tvf mytarfile.tar
10446 2003-05-18 02:59:36 findcode.cpp
59 2003-05-12 17:59:46 hello.cpp
850 2003-04-18 15:36:04 socketclient.cpp
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$
sed = stream editor Does what an editor does: add new text,
Designed to work with an input stream Think of sed as a filter Looks for line # or regular expression
Does something when a match is found
Most common use for sed Syntax:
s/expr/str/ - replaces the 1st occurrence of the regular
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
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
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$
Consider:
sed -e '2,4s/ /-/g' file3
The 2,4 tells sed to restrict the range to lines 2 through
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:
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
Syntax:
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$
Append Text:
adress a\
Insert file:
address r filename