Linux Learning
Linux Learning
" #
&'( $ %
) * + , -!
1/322
:
.
( )
: .
: ()
.
:
() .
.
.
.
.
.
.
:
:
.
:
.
() shell :
Pipes
.
( )
2 .
.
.
.
. .
2/322
.
.
.
1 .1
.
.
.
.
. Return . Return
. Return
. Return
.
() Return . .
Control Ctl CNTL CL RL .
. Return : Return
Contrl m . Return CTl-d
CTl-g CTl-h
backspace CTL-I Tab
Tab . Tab
( )8 . backspace Tab
. . delete
rub-out break interrupt .
delete . CTL-c
. break delete
CTL-c .
3/322
: . :
.
.
4/322
.
(
) .
.
.
.
. break interrupt .
.
:
. Return .
( ) .
.
. $ ( )% . ()
shell .
.
.
. .
. :
$ date
5/322
Return
.
. ( STTy )
. Tab
Tabs .
$ stty tabs
Tabs spaces . Tab
Tabs ( . Tabs $
Tabs ).
6/322
Return :
. (
@ )
() :
@$ ddtae Completely botched;start over
date on a new line
Mon sep 26 12:23:39 EDT 1983
$
sharp # / #
( ) . .
$ dd#atte##e Fx it as you go
Mon sep 26 12:24:02 EDT 1983
$
. (
) ( back space )
. :
$ datee Try
datee :not found It`s not
$ datee# Try #
Mon sep 26 12:26:08 it is #
$
backspace ) . CTL-
C . sharp #
.
.
# @
back slash .
# @
\ @ \ @
. @ backslash
. @ Backslash . escape
7/322
.
\ # backslash . #
.
.
( ) . Return
( )kernel .
backslash ( )kernel backslash
. Return
. :
shell backslash .
3 . ( kernel )
backslash
.
@ \ date . 1 .1 $
2 .1 ( shell shell ) #
# .
# :
$ date
Mon sep 12 : 39 : 56 EDT 1983
$ # date
Mon sep 26 12 : 24: 21 EDT 1983
$ \ # date
$ \ \ # date
( ) # date : not found
$
( )kernel
.
.
.
8/322
delete . Break
. delete
.
. :
CTL-S .
. CTL-q .
delete . .
CTL-d
. write Mary $ .
.
talking to the moon .
.
write ( ! ) shell
2 1 . write
.
( ) 0 ( )over
( )00 out over .
$ Eof $
write delete . .
CTL-d . write Mary $ .
.
talking to the moon .
.
write ( ! )
shell 2 1 . write
9/322
.
( ) 0 ( )over
( )00 out over .
.
CTl-d delete . )mesg)1 .
. news $ .
useNet .
. 1
. 2
7 6 . C
( . ).
.
.
.
man comman-name .
:
; shell (.
)
.
10/322
.
:
. mail$ .
.
. d Return
( ) P
S q
( .
. 102
. .
. . .
nico . :
$ mail nico
Gntrd-d .
$Ctl d
Ctl-d mail
delete ctl-d .
dead.letter ( )Send .
mail .
( ) .
.
mail 1 . ( mail )1
mail 1
( 1 ) 4
.
. . :
11/322
Learn
C .
learn $ . Learn
. teach .
.
.
6 .
2 )1 :
.
( )
. .
. .
. 2
.
. ( )
. emacs,vi
12/322
. ed
.
. (
) . 1 .
.
ed
. ed junk (
) :
ed $ ed
a
.
w junk .
39 .
ed junkq .
$ .
ed .
( a ( ed .
( ) . .
. ed
) ( w)
w junk junk junk
Ed . .
w
( .
ed.hup
). (
)
13/322
. ( w)
ed junk $
.
( q)
.
Temp , junk .
$ ed
a
W junk
19
q
$ ed
A
w Temp
22
q
$
ed ()
Return . ( 1s ) :
$ 1s
junk
Temp
$
( )
1s .
.
. 1s-t :
.
$ 1s-t
Temp
Junk
$
1 .
14/322
$ 1s-1
Total 2
- rw-r- - r- - 1 jou 19 sep 26 16:25 junk
- rw - r - - r - - 22 sep 26 16:26 Temp
- rw-r - - r - -
" " total 2 512
1024 .
- - rw r - - r - . ( )
" "1
2 " . "
22 . 19 . ed
. 1s, 1t :
1s-1 . u
:
1s - 1ut ( )1- . r - .
1s-rt .
1s :
$ 1s - 1 junk
- rw -r - - r - - 1jou 19 sep 26 16:25 junk
$
junk , -1
.
. ( ) t - -
1t .
.
. 1s
1s - 1 -t $
1s-1t .
.
(
)
. . (
)
.
15/322
cat , pr
. :
ed 19 junk
ed junk $ 1
19
p$ , 1
q
$
ed junk p 1$ .
. ed
( )
. . cou
. :
$ cat junk
To be or not to be
$ cat Temp
That is the question
$ cat junk Temp
To be or not to be
That is the question
$
( cat )
Ctl-s Cat
.
make pg p 6
. cat pr
66 ( 11)
. junk Temp :
$ pr junk Temp
16/322
mr,cp,rm
.
" " .
:
mv junk precious $
junk precious
. 1S junk : precious .
$ 1s
Precious
Temp
$ cat junk
cat con,t open junk
$
.
( ) cp :
$ cp percious percious save
percious Percious.save .
rm rm $ .
17/322
Temp junk
Rm:junk nenexistent
$
rm
. .
.
14 .
. 1s-t , 1 s .
t - ( . )
( ) .
(
)under score Precious. Save
) Junk , Junk, junk
.
. . () poem
Augustus de morgan.
ed .
$ ed
a
Great fleas have little fleas
Upon their back to bite em,
And little fleas have lesser fleas,
And so ad infinitum.
And the great fleas themselves , in turn
Have greater fleas to go on
18/322
And so ad infinitum.
Have greater fleas To go on,
Upon their backs to bite,em,
And little fleas have lesser fleas,
And the greater fleas themselves , in Turn,
great fleas have little fleas,
while these again greater still,
$
sort
:
. .
Sort - v
Sort - n
Sort - nr
Sort-f Fold upper
n+1-st Sort +n
4 Sort.
Tail 10 .
. . Tail
:
$ Tail -1 poem
and greater still,and so on
$
Tail :
$ tail +3 filename
( )
. New poem:
$ cat poem
Great fleas have little fleas
upon their back to bite em ,
And little fleas have leser fleas,
and so ad infinitum.
And the great fleas themselves , in turn,
Have greater fleas to go on,
While these again have greater still,
And greater still , and so on.
20/322
$ cat Newpoem
Great fleas have little fleas
Upen their back to bite them,
And little fleas have greater fleas,
and so on ad infinitum
And the great fleas themselves, in Turn,
have greater fleas to go on,
while these again have greater still,
and greater still ,and so on.
$
cmp . :
$ cmp poem new-poem
Poem new-poem differ : char 58, line 2
.
diff
:
$ diff poem new-poem
2c2
< upon their backs to bileem,
>and so on ad infinitum.
$
2 ( ) 2 ( )
. cmp
. diff
. diff .
1-1 .
3 1
junk .
21/322
.
login
. .
.
.
( ).
.
. .
. pwd( )
:
22/322
1-1
()
s-1
1s filenames
1s-t
1s-1t
1s-1
1s-u
1s-1u,1s-1ut .
rt,-rlt-
1s-r
ed - filename
file2 cp file File 2file
1 file 2
file 2 .
rm filename
cat filenames
pr filenames
66
n
pr-n flienames
pr-m flie
names
( )
wc
flienames
wc-1 filenames
grep pattern
flienames
grep-v
pattern files
sort
filenames
10
tail filename
filename Tail - n n
Tail +n filename n
23/322
usr
. / 14
( .
usr/you/ pwd
) .
1S/usr/you 1S $
. 1S
1S/usr . $
. .
:
/ 1S $
bin
boot
dev
etc
lib
tmp
unix
usr
$
( /:
) .
.
cat / usr/usr/you/junk . 2 . $
( junk ).
24/322
Boot bin
1S/bin/usr/bin .
.
() ( ) bin/ usr/bin/
. 1S cat
.
/ $ :
bin/date
Mon sep 26 23 : 29 : 32 EDT 1983
bin/who / $
Srm tty/ sep 26 22:20
Crw ttJ4 sep 26 22:40
you tty5 sep 26 23:04
$
3 S/usr/games /$:1 .
.
.
cd
( )Hary
.
cd cd/usr/Mary $:
( ) / S cat pr Mary
.
.
. :
. mkdir .
mkdir book $
cd book $
pwd $ .
26/322
usr/you/book/
( )
( )
$ cd
$ pwd
/usr/you
$
.. 0 .
.
cd $
.
.
rmdir book ( ) cd $
rmdir .
4 Shell 1
$ . ( )kernel
go-between Shell Shell . date who
. Shell ( )Kernel
. :
:
Shell
.
:
.
.
:
.
27/322
. .
.
. .
1 2 .
2 .1 1 .1
2 .1 1 .2
1 .3
.
.
:
pr ch 1.1 ch 1.2 ch 1.3 $
.
. *pr ch $
Shell * *ch
ch Shell . pr .
pr * Shell
pr . pr
Shell . .
:
$ wc *ch.1.
113 562 3200 chl.0
935 4081 22435 chl.1
974 4191 22756 chl.2
378 1561 8481 chl.3
1293 5298 28841 chl.4
33 194 2030 chl.5
75 323 2030 chl.6
)88933 16210 ) 3801 ToTal
echo . echo
:
$ echo hello world
hello world
$
28/322
:
$ *echo ch1
1 .
$ * echo
.
* $ pr
( )
* $ rm
( . !) . *
rm * S . $
* save save .
. 10
$ 10 2 :
* echo
Ch 1.1 ch1.2... ch10.1 ch10.2 ...ch2.1 ch2.2...
$
* Shell
. [ ]... .
.
$ * [pr ch ]1 2 3 4 6 7 8 9
9 8 7 6 4 3 2 1 5
* ]pr ch ]1-46-9 $
.
1S . $ . :
$ 1S > file list
filelist . < :
.
.
( ) cat :
cat f1 f2 f3 > temp $
<< < .
:
$ cat f1f2f3 >> temp
f1 f2 f3 Temp
. < ( Temp )
. >
.
30/322
sort $
ghi
abc
CTL d
Abc
31/322
Def
ghi
$
.
.
.
. :
Standard error
date who
cmp diff
( . - ).
who / sort $ 7 )1
who > sort $
.
Shell .
33/322
) ) Shell
( ) :
date ; who $
Tue sep 27 01:03:17 EDT 1983
Ten TTJo sep 27 00:43
Dmr Tly 1 sep 26 23:45
Rob TTJ2 sep 26 23:59
Bwk TTJ3 sep 27 00:06
Jj TTJ4 sep 26 23:31
You Tty5 sep 26 23:04
Ber TTJ7 sep 26 23:34
$
( ) sell .
.
:
WC .
process- id
.
WC . WC
.
process-id
& pr ch* |pr . & $
precess-id of |pr /695
$
& .
wait process-id . $
& .
. wait
delete . precess-id shell
& :
kill 6944 $
process-id ps
.
kill O shell .
ps-ag
.
:
ps - ag $
PID Tty TIMe CMD
Co 6:29 /etc/Cron 36
5 6423
Sh - 0:02
Sh - 0:04 1 6704
35/322
PS .
.
( (Ps )1
).
. .
Shell
.
Shell .
1
ed
Shell
.
36/322
.
& . ( nohup )
: & nohup command$
.
nohup.out .
nohup .
nice
nice & nice expencive command $
)at )1 .
at tme $
Whaterer commands
Jeu want
Cti-d
$
:
37/322
.
@ .
STTJ erase , Kill K $
( ) . e
K .
Shell .
. . profile
Shell
. . profile
()
.
profile :
STTJ erase
.
) .
Tab Tabs STTy :
STTy erase h - Tabs
who/wc - 1 .
news .
38/322
usr/games/fortune/
( ) . . profile
. Shell Shell
.
$ Shell ps1
:
?Ps1= 1yes dear
.
= Shell . Hmoe MaIL
HOME .
profile . MaIL
.
Shell
.
Shell Shell
.
Shell
bin/ . usr/bin/
Shell PATH .
. profile
11 Shell .
.
Shell .
. 7 5
.
: .
39/322
. :
usr/games/ .
...PATH: :/ bin ;/usr/bin : /usr/ games one way
: :
. . . .
PATH .
( )PATH (PATH= $ PATH: /usr
d= /harribly / lony / dinectory / name
profile cd $ d $ . d
Shell PATH
. Shell
expart 3 .
Exprot MAIL PATH TERM
profile :
$ cat . profile
STLy erase h - Tabs
MAIL = /usr/ spool/ mail / you
PATH= $ HOME /bin : /bin: / usr/ bin:/usr/ games
B=$ HOME / book
Export MAIL PATH TERM b
Date
Who / WC-1
$
Shell .
shell .
. 3
.
5 1
. .
.
. FORTRAN
71 )bc)1( cu)1 )uncp)1
)units)1 .
.
.
.
41/322
K.L.thompson , D.M.Ritchie .
ACM 1974 CACM 1983 ( . 89
1983 ) .
. )Bell )BSTJ
( )1978
CA CM Ritchie thompson . RSTJ
1984 . J.R.Mashey ,
B.W.kernighan ( . IEEE )1981
.
.
1 . 1 HolT Rinehart
winston . 2
.
. .
Aun Nico Lomuto )1983 Hall )prentice
.
42/322
:2
. .
.
.
.
.
-
(inode ) .
.
( . .
).
.
.
.
:
$ed
a
now is the time
for all good people
.
w junk
36
q
$ ls -l junk
-rw-rw-r-- 1 mahdi mahdi 36 Jul 4 11:21 junk
$
Junk 36 36( . ) .
$ cat junk
now is the time
for all good people
$
43/322
cat . 2od :
$ od -c junk
0000000 n o w i s t h e t i m e \n
0000020 f o r a l l g o o d p e o
0000040 p l e \n
0000044
$
c () . b
( )octal :
od -cb junk $
0000000 n o w i s t h e t i m e \n
156 157 167 040 151 163 040 164 150 145 040 164 151 155 145 012
0000020 f o r a l l g o o d p e o
146 157 162 040 141 154 154 040 147 157 157 144 040 160 145 157
0000040 p l e \n
160 154 145 012
0000044
$
.
PDP 11 .
16 x od .
012 . ( newwline )
enter . C \n .
od
012 .
newline .
( backspace ) ( 010
\ )b )\tab)011t )\r 015( 3.
.
backspace .
backspace backspace .
\ (\ )backspace
backspace \ 010
. backspace \
.
backspace backspace
octal dump2
Carriage return3
44/322
. od backspace 010
d \b c .
tab : tab
. tab . tab
. .
tab .
25 17 9 . stty -tabs tab
. )stty)1 .
enter . enter newline newline
. newline .
newline .
(
).
newline .
( . linefeed CRLF ).
.
newline newline newline
.
. .
.
newline
. ( )\e . od
.
.
.
( ) read . read
. read
. read
. read .
.
read
45/322
.
newline .
.
Enter . .
cat . cat
. u :
$cat -u
123
123
456
456
789
789
ctl-d
$
return cat .
ctl-d Enter :
$ cat -u
123ctl-d123
Cat . ctl-d
.
Enter ctl-d . ctl-d
:
$ cat -u
123ctl-d123ctl-d$
Cat .
ctl-d .
. ctl-d
. ctl-d . .
.
. file :
$ file /bin /bin/ed /usr/share/man/man1/ed.1.gz
/bin: directory
/bin/ed: ELF 32-bit LSB executable, Intel 80386, version 1 )SYSV(, for GNU/Linux
46/322
2.2.5, dynamically linked )uses shared libs(, stripped /usr/share/man/man1/ed.1.gz: gzip compressed
data, from Unix, max compression
$
ed :
.
file . .c
C a.c .
file ( .
file
).
od .
16 2 :
$ od /bin/ed
0000000 042577 043114 000401 000001 000000 000000 000000 000000
0000020 000002 000003 000001 000000 107140 004004 000064 000000
0000040 127244 000000 000000 000000 000064 000040 000007 000050
042577 . .
042577 ASCll . .
.
. file #include
C.
. sort bin/ed/
. .
home/you/ . junk ls
usr / you / junk / . .
$ IS
junk
$
.
. . IS
. .
$ pwd
/ usr / you
$
( ) .
47/322
.
: ( )
. .
( ) .
. .
usr / . usr( . user)
usr / you / :
usre / src / usr / src / smd / . usr / src / smd / sh / .
.
mkdir .
$ pwd
/ usr / you
$ mk dir recipes
$ cd recifes
$ pwd
/ usr / you / recipes
$ mkdir pie cookie
$ ed pie / aplole
pie/apple . .
apple pie .
/ recipes / apple pie
pies . crust
pie recipes / pie .
. . IS
.
$ cd
$ Is
junk
recipes
* $ file
jund ascit text
48/322
recips : directory
$ IS re cipes
cookie
pie
$ IS recipes / Pie
apple
crust
\$
.
usr /you /
junk recipics
pie cookie
( du )
.
$ du a
2 . / recipes / pie / apple
3 . / recipes / pie / crust
6 . / recipes / pie
3 . / recipes / cookie/ choc.chip
4 . / recipes / cookie
11 . / recipes /
1 . / junk
13 .
$
.
1024 512
Du . wll a .
du .
dw a grep .
$ du a / grep choc
$ . / recipes / cookie / choc. Chip
$
. ( ).
49/322
Du .
( ). . junk junk / . .
.
.
.
:
. .
16 14 ASCll Nuls ( .
) . .
dot-dot( . . (.)dot( and
( )Root .
( )Root .
4 2
.
. .
.
.
super-user .
Root . super-user
. Root . Su shper-user
. super-user . .
super-user (
) . crypt . crypt supper-hser
crypt crypt . crypt
.
.
super-user .
.
.
.ligin-id . user-id uid
50/322
.
login-id uid .
. uid group-id
. ( loging-id )roat other .
.
uid group-id .
etc/passwd/ . . uid group-id
etc/passwd/ .
$ grep you / etc / passwd
you : g k m b c T r j o A com : 604 .1 : y. O. A . people : / usr/ you
$
: .
login-id : en crypted passwd : uid : group ld : miscell any
: login directery : shell
.
.
. bin / sh / .
.
. . login
. etc/passwd/
.
. g k m b c t r y o 4
.
etc / passwd / .
. ( ) ( ) (
). .
. ( )group .
IS-1 .
$ IS 1 / etc / passwd
- rw r r - - 1 root 5115 Aug 30 10 :40
$ IS Ig / etc / passwd
- rw r - - r - - 1 adm
51/322
who root .
shell :
who $
who bin / .
.
.
.
.
$ IS - Id
d r w x r w x r 3 y-u 80 sep 27 06 : 11
$
IS-d d . r
. ( IS )od W .
. .
root .
$ who try to wer write .
. : cannot creat you cant
$
.
w
.
.
. rm .
( .
F rm ) X
.
. ( )X - - .
. IS
. ( )- - r . IS
53/322
. usr games / .
( chmod ) .
$ chomed permissions filemond
. .
. . .
:
$ chomd rw rw rw junk
$ chomd G G G junk
. 4 2 1 .
IS .
. )chmod )1 . +
$ chmod + x commond
.
$ chmod w file
.
super user .
.
$ IS Id / user / mary
d r w x r w a r w x 5 mary 704 sep 25
$ chmod 444 / usr / mary
chmod : cant change / usr / mary
$
.
.
$ cd
$ date > temp
$ chmod w.
$ Is Id .
dr xr xr x 3 you
rm : temp not removed
$ chmod 77 5
$ Is - Id
d r w x r w x r -x 3 you
$ rm temp
54/322
$
temp . .
.
( )index nod .
5:inodes 2
.
inode :
inode . (
) inode . .
$ date.
The sep 27 12 : 07 : 24 EDT 1983
$ date > junk
- rw rw rw 1 you
$ Is Iw junk
- rw rw rw junk
$ Is Ic junk
- rw rw rw - 7 you
$
. Is-Iu inode
. Is-Ic
$ chmod 444 junk
$ Is Iu junk
- r - - r - - r - - 1 you 29 sep 27 06:11 junk
$ Is Ic junk
- r - - r - - r - - 1 you 29 sep 27 12 : 11 junk
$ chmod 666 junk
$
Is-t C u .
inodes .
$ Is recipes
cookie
pie
$ Is Iwt
total 2
d r w x r w x r w x 4 you
- rw rw rw 1 you
$
recipes . . inodes
55/322
Is inoder .
. I-number . inode Is-1 . I-
number ( ) .
$ date > x
$ Is i
1 5 7 6 8 junk
1 5 2 7 4 recipes
15852 x
$
.
inode i-number . .
inode , rm . .
inode . i-number .
. . i-number
.
. i-number
.
Inold file new file In $ .
. .
bin / ed called / bine / e / e . inode
.
$ In junk likt ojunk
$ Is Ii
total 3
1 5 7 6 8 - rw rw rw - 2 you
1 5 7 6 8 - rw rw rw - 2 you
1 5 2 7 4 d r w x r w x r w x 4 you
$
. inode .
. .
Is .
. .
$ echo x > junk
$ Is I
total 3
- rw rw rw 2 you
- rw rw rw 2 you
d rw x rw x rx x 4 you
56/322
$ rm link to junk
total 2 -rw rw rw 1 you
drw x rw x rw x 4 you
$
link to junk rm
. . .
.
. .
( .
. .
.
.
.
( ) .
.
CP . .
$ cp junk copy of junk
$ Is Ii
total 3
1 5 8 5 0 - rw rw rw- 1 you
15768 - rw rw rw 1 you
15274 drw x rw x rw x 4 you
$
i-number junk copy of junk . . .
.
$ chmoe w copy of junk
. copy of junk
rm .
mv .
cp In.
$ mv junk sam old junk
$ Is Ii
total 2
1 5 2 7 4 drw x rw x rw x 4 you
1 5 7 6 8 - rw rw rw - 1 you
57/322
$
some old junk junk . i-number ( 8 7 5 1
inode )
. In .
.
mv . . mv cp
.
mv )or cp( file 1 file 2 directory
.
. :
$ cp / usr / src / cmd / ed
.
$ mk dir sh
$ cp / usr / src / end / sh * sh
cp sh ( . usr /
src / cnd / sh cp ).
In . .
mv cp In .
6 2
bin
usr / you / .
. ( )root .
$ Is / boot
dev
etc
lib
tmp
unix
usr
$
/ . unix / .
. boot / unix/ .
)boot )8 .
58/322
.
. . 1 2
.
bin / . who ed .
)dev )devies / .
)etc )et ceterw / passwd /
etc / getty
bin/login / .
etc / rc / .
etc / group / .
libclibrar / . lib / cpp /
tmp )temporaries0 / .
ed tmp/e00512/ .
.
tmp / . . e 00512
. tmp / .
. tmp / .
tmp/ .
ed .
5 6 .
usr / .
usr / buk , / usr / rob / . usr /
. , / usr / bin / usr / lib / usr / tmp /
. nroff
usr / bin / bin / fortRan usr / lib / .
. th Edition 7 bin /
usr / bin/ . usr / bin /
.
usr / usr / adm / .
usr / dic / . usr / man .
/ usr / man / man 1 / spen 1.
59/322
usr/src/ .
usr / .
7 2 :
dev / . dev /
.
dev / mto /
.
dev / mto / .
$ cp / dev / mto junk
junk . cp .
dev / mto / . .
b c . IS inode .
Inode . . inode
( )minor ()major
.
.
/ . dev / ttyo /
dev / tty 1 / . .
dev / rpo 0/ . dev / rpo 1 / D E C
RP06 .
.
.
/ dev / rp 10 / dev / rp 11
.
.
.
. etc / mount / .
$ /etc/mount
rp 01 on /usr
$
60/322
cpu
cpu .
Egrep grep 4
grep . grep egrep
dev / nu 11 / .
62/322
:
. .
C .
.
.
.
.
.
.
.
.
( ) :
/bin/Who
$who
You tty sep :
$
:
; $ date
web sep :: EDT
$ date ; who
web sep : EDT
$ date ; who
. who date .
date ; who :
$ date ; who | wc
wed sep :: EDT
$
who wc . who wc
date . | ;
.
:
($ )date ; who
wed sep :: EDT
tee wc
tee .
$ .
. $
64/322
$ long running - command
- id long running commend $
. sleep
:
$ sleep 5
$
$ )sleep 5 ; date( $ date
5278
wed sep 28 09 : 18 : 20 EDT 1983 date
$ wed sep 28 09 : 18 : 25 EDT 1983
date
date
. sleep date .
( .
5 ).
$ )sleep 300 ; echo Tea is ready( $ Tea will be ready in smunutes
5291
$
( . ctl-g
) . $ ; .
$
:
$ pr file | lpr $
.
:
($ )pr file | lpr $
( )pr .
65/322
. pr
grep
. .
> | < . $ .
. .
$ echo hello > junk
Hello junk String > junk .
.
:
$ > junk echo Hello
.
. .
$ cat file | pr
$ pr < file
$ pr file
( <
< file | cat file).
*
* .
*$ echo
ls .
. ..
.
:
. echo echo :
ls$
.Proflie
Junk
Temp
* $ echo
Junk Temp
* $ echo .
66/322
. .. .profile
$
* . : .
.
:
'***' $ echo
***
$
... $
... \ ... .
\
*\*\*\ $ echo
*\*\*
.
:
''! $ echo '' Dont do that
Dont do that
$
$ echo X' * 'y
X*y
' $ echo '*' A' P
?* A
$
: .
> > Prog
> >> Prog
< < Prog
| P2 | P1 P1
<<str Str : .
*
[ ]ccc
9 0 a-z
; 2P : 1P 1P 2P.
67/322
$ ; 2P .
' '. . . ... ' '...
()... ...
{}... ... ( )
.etc $2 , $1 $0 $4
$ var var
{$}var var
. .
\ \ C C \
. . . . . . '. . . ' $ \
# # (
)
Var = value var .
P1 $$ P2 ; P1 P2
P1 | | P2 ; P1 P2 .
echo
.
:
$ echo hello
> world
hello
world
$
> .
. : . ps2
.
$ echo x * y .
x y .
* .
(
68/322
) .
:
$ ls x * y
x * y : lS
$ > xyzzy xyzzy
$ ls x *y
xy zzy xyzzy x*y
$ ls x*y * ls
x * y
$
.
\ $ echo abc
\ > def
> ghi
abcdefghi
$
.
# #
:
$ echo hello # there
hello
$ echo hello # there
hello # there
$
# .
* $ ls . . .
.
. :
pure - echo Enter $ :
69/322
: $ Enter
:
! $ pure echo Hello
>
! Hello
$
.
n
:
$ : echo - n Enter
: $ Enter
$ echo
n-
$
: n-
$ echo - n - n
>
-n
$
. V
C \ b ( \ c c)
:
$ echo Enter : \ C V
: $ Enter
.
.
. ( )-n
.
.
:
70/322
.
.
.
.
.
.
.
.
! .
.
. \ n .
\n \ n
\n
.
\ c .
\c \n .
.
.
.. grep
\\ grep \$ grep
\\ \\ grep \\ $ grep
'' \'' $ grep \\\$ grep
''' ' $ grep \$ grep
'' '' $ grep \$ grep
.
. . grep a
71/322
: C .
-
*$ echo */
( - ) . / ( / )
-
$ cat xy / y
$ cat x >> x
.
-
* $ rm
rm
.
.
.
$ who | wc - 1
nu .
' ' who | wc 1
:
$ echo who | wc 1 > nu
( nu )
who wc sh .
. nu
:
$ who
07:51 28 sep 2 you tty
10:02 28 sep 4 you tty
09:38 28 sep 5 you tty
72/322
pwd $ usr/yo u /
bin $ mkdir bin
echo $ PATH $
asr/you/bin:/bin:/usr/bin /:
nu mv nu bin $
$ ls nu
nu
$ nu
4
$
PATK profile
73/322
.
.
:
Cs0
( )
What 0 who ps - a
Where 0
- ( .
ps1 ).
. . / bin,/usr/bin
. : (.)1
..
nu
.
cx
cx nu $
chomd+xnu $
.
cx
Chomd + x filename
74/322
. cx
cx .
1$
$2 $9
. cx
Chomd+x $1
$ cx nu " " 1$
nu .
:
'$ echo chomd + x $1 >cx Cx
$ sh cx cx Cx
$ echo echo Hi, there !>hello
$ hello
hello :
$ cx hello
$ hello
Hi, there! Cx
$ mv CX/urr/you/bin
$ rm hello
$
$ sh cx cx
cx
$ cx cx
cx
.
Chomd+x$l $2 $3 $4 $5 $6 $7 $8 $9
( $9 $10 1$ 0 !) .
75/322
chomd .
.
* $ .
cx * x chomd + $ .
* $ 1c : m
$ cd/usr/you/bin
$ cat 1c
1 # c:
wc -1 * $
$ cat m
: #m
*mail $
$
. $ *
wc mail . :
* $ 1c / usr / you / bin /
usr / you /bin /cx 1
/ usr /you / bin /1c 2
usr / you / bin / m 2
/ usr / you / bin / nu1
2/ usr / you / bin / what
1 / usr / you / bin / where
9 total
$ 1s /usr/you/bin | 1c
4
$
bin
.
.
. .
/ usr/you/bib/phone-book
grep (. lib
) . grep
.
:
411$ < ' echo 'grep $* /usr/you/lib/phone book
411$ cx
joke 411 $
dial - a joke 976 3838 212
Dial 411 $
3838 212 976 dial - a joke
246 4200 212 dial - a prayer
976 3636 212 dial santa
dow jones' 411' $
Grep : can't open jones
jones
$
: jones dow
grep :
$ grep dow jones /usr/you/lib/phone book
.
. ''
" " \ 's s,$ " " 's .
Grep "$*"/usr/you/lib/phone -book
$ grep
.
dow jones 411 $
dow jones report 212-976-4141
( grep ) y-:
$ grep - y pattern
-y ( .
grep ).
77/322
. $ 0 cx $0 cx . $0
who | 2 $ :
drh ttyo sep 28 21:23 cvw tty 5 28 21:09
dmr 4Tty sep 28 22:10 scj tty 7 28 22:11
You ttyg sep 28 23:00 J1b b tty 28 19:58
$
2 3 :
1n 26 1n 25 1n 24 1n $ 3 2
] -1i - $ 1s [9-1
sep 51 you 5 -rwxrwxrwx 16722 28 23:21 2
sep 51 you 5 -rwxrwxrwx 16722 28 23:21 3
sep 51 you 5 -rwxrwxrwx 16722 28 23:21 4
sep 51 you 5 -rwxrwxrwx 16722 28 23:21 5
sep 51 you 5 -rwxrwxrwx 16722 28 23:21 6
1s/usr/you/bin $ | 5
2 3 4 411 5
1c cx 6 m nu
where what
cat 5 $
3 : 2# n
-Pr-$o-t 11 *$
$
-t -1n n .
pr $0
.
. *
( )
. ' ' 1000 :
$ 'echo at the tone the time will be 'date
19830 EDT 02:15 : 00 29 At the tone the time will be thu sep
' ' 1000 :
$ echo ''At the tone
$ ' the time will be 'date ''.
1983 EDT 03:07 : 00 29 The time will be thu sep
78/322
$
login
.
.
$ mail 'cat mailing list' < letter
cat ( .
) .
.
:
$ cat mailing list
echo don whr ejs mb
$ cx mailing list
$ mailing list
don whr ejs mb
$
$ mail / 'mailing list' < letter
. pick :
$ pick arguments
. pick
( y ) .
$ pr 'pick*. C' | pr
C pr 1pr ( pick
.
.
.
..\ echo 1 'echo\'date . . $
'date' $ . . .
.
$ grep -1 pattern filenames
.
$ 'command 'grep -1 pattern filenames
.
.
.
$1 - .
. PATH : Home
login .
PATH $ PATH $1 $1 .
.
.
$ PATH = : /bin:/usr/bin
.
. :
$ PATH = $ PATH:/usr/games
$ echo $ PATH
:/usr/you/bin:/bin:/urs/bin/:usr/games
$ PATH=:/usr/you/bin:/bin/usr/bin
$
.
.
:
pwd $
usr/you/bin/
dir = pwd $
80/322
cd/usr/mary/bin $
1n $dir/cx $
$
cd $dir $
pwd $
usr/you/bin/
$
.
.
$ set
Home=/usr/you
=IFS
PATH=:/usr/you/bin:/bin:/usr/bin
PS1=$
>=PS2
Dir=/usr/uou/bin
$ echo $dir
/usr/you/bin
$
.
x = Hello $ x
sh $
$ echo $ x
X :
ctl-d $
$
$ echo $ x
Hello x
$
:
"echo "X="GoodBye $
> echo $ X" > set X ...... X
cat setX $
"X="GoodBye
Echo $X
echo $ X $
81/322
Hello x
sh setx $
Good Bye x
echo $ X $
Hello
$
.
PATH . ( .)
.
prfile :
$ cat / usr / you / bin / games
PATH= $ PATH : /usr/ games
$ echo $ PATH
:/ usr / you / bin : / bin : / usr / bin
$ . games
$ echo $ PATH
:/ usr / you / bin : / bin : / usr / bin : / usr / games
. PATH bin .
. .
. -
.
. $1 $2
. .
:
echo echo $ x > echo x $
CX echo x $
echo $ x $
Hello
x echox $
x = Hi echo x $ x
Hi
$
( ( dd ) ).
.
. usr/games/ PATH
82/322
:
$ /S / usr / games : grep fort
fortune
$ fortune
fortune :
$ echo $ PATH
:/ usr / you / bin: / bin: / usr / bin usr/games PATH .
$ PATH = /usr / games / fortune
.
$ echo $ PATH
: / usr / you / bin: / bin: / usr / bin
$ cat / usr / you / bin / games
PATH = $ PATH :/ usr / games
$ . games
$ fortune
- Khuth
$ echo $ PATH
: / usr / you / bin: / bin: / usr / bin: / usr / games PATH
$
. games
game PATH PATH usr/games/ :
$ cat / usr / you / bin / games
*$ * $ PATH = $ PATH : / usr / games
$ CX / usr / you / bin / games
$ echo $ PATH
:/ usr / you / bin:/ bin: / usr / bin /usr/games
$ games fortane
I'd give my right arm to be ambidextrous.
$ echo $ PATH
:/ usr / you / bin:/ bin:/ usr / bin
$ . games
$ echo $ PATH
:/ usr / you / bin:/ bin:/ usr / bin:/ usr / games
$ for tune
$
games PATH usr/games/
. * $ PATH
83/322
. games .
export ( .
).
:
$ x = Hello
$ export x
$ sh
$ echo $x x
Hello
' $ x = 'Good Bye
$ echo $ x
GoodBye
$ ctl d
$
$ echo $ x
Hello x still Hello
$
expor t
( . ! S ed ).
PATH HOME .
. PATH
. 1/0
:
diff file 1 file 2 > diff. Out $
: diff : file 2
$
diff . out
diff .
( ) . 0 1
84/322
.
.
.
.
$ time wc ch 1 . 3
22691 4288 931 ch 1 . 3
real 0/1
rse 4/0
sys 4/0
real 1/0
user 4/0
sys 0/3
$
2 > $1 .
. 1> $2
:
Echo 1 > $2
85/322
.
.
. .
:
$ cat 411
grep "$ *" << End
976 dial a joke 3838 -212
246 dial a prayer 4200 -212
976 dial santa 3636 212
976 dow jones report 4141-121
End
$
<< . ( End )
. '' $ 1
.
.
. . .
. . .
:. I/O
<file
<< file
>file
2P. | P 1P 2P
|
n>file n
n>>file n
n>$ m n m
86/322
n<$m n m
>>S : S
'' $ 1
>> S1
'>> 'S
: .
.
for
. :
for var in listof wrds
do
commands
done
for .
* $ for I in
> do
> echo $ i
> done
i i . i$
i . *
. .
( ) :
$ 1S ch2 . * | 5
ch1 . 2 ch2 . 2 ch3 . 2 ch4 . 2 ch5 . 2
ch6. 2 ch 7. 2
*$ for i in ch2.
> do
> echo $ i:
> diff b old / $ i $ i
> echo
87/322
Done ; $I 2 in do ; 6 5 4 3 $ for i in
$
for pick
:
$ for i in 'pick ' *ch2.
88/322
> do
> echo $i:
> diff old / $ i $i
> done | pr | lpr
chr. 1 ? y
ch2 . 2
ch 3 . 2
y ch 4 . 2
y ch 5 . 2
ch 6 . 2
ch7 . 2
$
:
.
. diff pick
. .
> done | pr | 'pr $
.
. :
.
somewhere!bob bin
. :
$ cd /usr/you/bin
'*$ for i in 'pick
> do
> = = = = = = = = echo = = = = = = = = = = = = = =$i
> cat $i
> done | mail / somewhere ! bob
$
somewhere ! bob :
.
.
.
89/322
.
. bundle
:
$ cat bundle
# bundle : group filles into distribution package
'echo '# To unbundle , sh this file
for i
do
"echo "echo $i 1>$2
"'echo "cat >>ii << 'End of $ i
cat $i
"echo "End of $i
done
$
End of $i .
somewhere 1bob :
$ bundle CX 1C > junk
$ cat junk
# To unbundle , sh this file
echo CX 1>$ 2
'cat SCX <<'End of CX
*chomd + $
End of CX
Echo 1C 1>$2
'Cat >1C <<'End of 1C
# 1C : count numbr of lines in files
* $ wc 1
End of 1C
$ mkdir test
$ cd test
$ sh .. / junk Try it out
cx
1c
$ 1S
cx
1c
$ cat CX
*chomd + $
$ cat 1C
# 1C : count number of lines infiles
* $ wc 1
$ cd .. looks good
90/322
:
End of filename
.
.
bundle bundle I/O
bundle. ( )mail
bundle . -
.
.. bunddle :
.
bunddle .. 1 S1
. bunddle ar .
:
.
.
:
. :
( )
.
I/O > < *
.
.
.
:
91/322
.
.
RUN .
.
.
:
- .
. if goto lables
goto lable ( .
).
.
: I/O .
;
.
csh ( ) C
. C
( ) . .
.
.
Pick bunddle .
92/322
93/322
:4
. grep tail sort
Wc . .
. grep
. grep
egrep fgrep.
tr dd
unig Sort .
.
.
.
.
sed a w k .
grep :
$ program pattern action filenames
. grep ed
.
sed awk Sed . ed
awk .
sed
c .
.
grep .
grep .
$ grep pattern filp-nomes
.
grep
:
94/322
c
[$ grep n variable * . ]ch
$ grep from $ maIl
mary $ grep from $ maIl | grep v mary
$ grep y mary $ Home/lib/phone-book
$ who | grep mary
temp $ Is | grep v temp
n v y
( ).
grep . grep
grep : .
Grep .
ed grep ( ) ed
.
* .
. .
.
^ $ (^) ( )$ .
$ grep From $ MAIL
From
$ grep ^ From $ MAIL
Form .
grep
.
grep [ ]a-z
. : grep ^
.
]9 0 ^ [ . l
grep ed . ]-[ ]sil[
[ ] .
. : ( .
95/322
) :
Is - 1 | grep ^d $
Is - 1 | grep ^ rw $
^ [ Is-1
.
* ( )
. *x xs
[ * ]a ZA Z * * .x
x .
. * xy x
ys . xy xy xy
. [a-
]ZA-Z[ * ]a ZA Z [ Z ].
* * ..
grep .
grep .
. :
$ grep ^ ] ^ :[ * : : / etc / passwd
: grep
Fgrep egrep . fgrep
egrep grep
or .
fgrep egrep f .
.
fgrep
:
$ fgrep - f common errors document
( egrep . )
grep .
( *) x y xy xy xy xy
xy xy . | " " or; - today | tommorow
. )to )day | morrow egrep
96/322
+ . . x + xs ? x x
.
Grep
.
.
usr/dict/words / :
.
:
$ cat alphvowels
^ ]^ aeio[ * a ] ^ a eiou[ * e ] ^ aeiou [ * i ]^ a eiou * o ] ^ a eiou[ * a
] ^ aeio[ * $
$ egrep f alphrowels / usr / dict / web 2 | 3
abstemious abstemiously abstentious
acheilous acheirous acleistous
affectious annelidiou arsenious
arterious bacterious caesoious
facetious facetiously fracedinous
majectious
$
. egrep
egrep .
. grep
egrep
.
:
$ cat monotonic
^a ? b ? c ? d ? e ? f ? g ? h ? i ? j ? k ? l ? m ? n ? o ? p ? q ? r ? s ? t ? u ? v ? w? x ? y ? z ? $
$ egrep f monotonic / usr / dict / web 2 | grep | 5
abdest acknow adipsy agnosy almost
befist behint beknow bijoux biopsy
chinte debors dehort deinos dimpsy
egilops ghosty
$
( egilops ) . grep grep
.
grep fgrep
( .
)
. fgrep
grep egrep . grep egrep
97/322
grep . ed
egrep .
( )
( )
.
egrep grep
.
: . grep egrep
( )
c c
\c c
^
$
0
[] a-z
[ ^ ] .
\n )\ ( \ n th ( (grep
*r r
+r ( r (egrep
?r ( r (egrep
r1r2 r1 r2
r1 | r2 r1 ( r2 (egrep
\ ()\r ( r )grep
() r ( r )egrep
.
.- ( (\ ( ) | )and ( ed ) grep
. : .
. . grep grep .
.
98/322
.
.
.
sort . sort sort :
ASCII .
sort
. f
. ( d ) .
. n
r .
s | sort f / $
s | sort n / $
s - s | sort nr / $
sort
. +m m +o .
s - 1 | sort + 3 nr / $
who | sort + 4n $ login
sort - o (
) - u sort .
sort sort :
$ sort + of + o - u filenames
+ of .
+ o sort ASCII . -u
. .
sort sort . sortcu .
uniq -u sort :
. . uniq
.
99/322
uniq - d :
uniq-u ( . ) uniq- c
.
.
comm . f1 f
2 comm :
f2 f1
.
:
$ comm 12 f1 f2
$ comm 23 f1f2
.
.
tr . tr :
tr a- Z A - Z $
tr AZ a Z $
dd .
os /360 dd . (
)tr dd ASCII EBCDIC
. dd
dd .
10
:
cat | * $
|'tr sc A-za-z '\ 012
| sort
| uniq c
| sort n
| tail
5
100/322
cat tr tr :
.
uniq-c sort
n ( . uniq
) . tail .
( ) .
| .
..
usr/ dict / words / .
..
.
. sed
18 ch 4. 1
13 ch 4. 2
14 ch 3. 4
17 ch 4. 4
2 ch 4. 9
$
sed () *
:
$ du a ch 4. * | sed s / . * //
ch 4.1
ch 4.2
ch 4.3
ch 4.4
ch 4.9
$
( * )0
( ).
login who :
$ who
1r tty1 sep 29 07 : 14
ron tty3 sep 29 10 : 31
you tty4 sep 29 08 : 36
td tty5 sep 29 08 : 47
$ who | sed s / . * //
1r 07 : 14
ron 10 : 31
you 08 : 36
td 08 : 47
$
s (
) . .
sed getname :
$ cat getname
who am : | sed s / . * //
$ getname
you
$
.
ind :
* $ sed s / ^ / / 1 ind
. sed
.
:
* $ sed /.s / ^ / / ind
/. / s
. sed
.
ind .
! .
* $ sed / ^ $ / ! s / ^ / / ind
/$ ^ / ( ) says ! / $ ^ /
.
sed
( ) . ed . sed
( )say :
sed 3 q
3q ed sed :
.
() .
sed ind sed
( ) sed :
sed s/^//
3q
: . sed
.
head . sed
( 3q )10q . ind sed
( . c
103/322
) .
bin
.
sed
sed f cmd file $
.
:
$ sed / pattern / q
$ sed / pattern / d
pattern
.
. c-n
P .
$ sed n / pattern / p
grep .
sed n / pattern / ! p $ !
grep v ( sed / pattern / d).
sed grep grep sed .
grep sed . grep
sed grep : grep( .
sed . : -b ) . .
gres sed
.
sed ed :
\ $ sed s / $ /
>/
\ / * ]sed s / ] [ ] $
<g/
104/322
( . ' [] '
[] * '
.
.
sed - n 20 , 30 p $
$ sed 1 , 10 d (tail =( +11
sed 1 , /^ $/ d $
sed - n / ^ $ / , / ^ end / p $
end
sed $d $ .
. sed
ed : . +
.
sed $ - 1 d $ :
d 1 - $ :
$
:
( . sed .
hold ) . :
:
$ sed / thing / + d
sed .
$ sed n / pat / w file /
/ pat / ! w file r filenames
pat 1 pat .
105/322
sed 1 $
. :
/^$|\$q
/ $ \ $ $ . older
:
$ cat older
# older f : list files older than f
Is tr | sed / ^ $ | $ / q
- r IS .
:. sed
\
\a
/ b /abe abe// :
\c a
d
i/ .
l
p
q
r file
s / old / new / f New old . f = g
. f = p f = wfile
.
: /abp/ .
/t /abe
wfile
/y / strl / str 2 strl str2
( )
=
! cmd sed cmd
/ abe / : / abe / b t
106/322
} }
sed
ed sed
. .
sed .
Sed ed
. sed
( ) sed
/ / +1 sed
sed .
Older .. Newer .
.
.. sed bundle . :
.
. awk
sed awk . awk
sed c
$ . awk sed :
awk program filenames
:
{ pattern } action
{ pattern } action
awk .
awk . sed .
egrep c .
$ awk / regular expressino / } print{ filenames
egrep : .
.
$ awk / regular expression / filenames
107/322
. .
.
$ awk } print{ filenames
cat .
. sed awk
:
$ awk f cmd file filenames .
awk
. who :
$ who
you tty 2 sep 29 11 : 35
jim tty 4 sep 29 11 : 27
$
awk $2 $1 NF $ NF .
NF ( . NF $ NF . awk
$ ) .
du-a
$ du a | awk } print $ 2 {
login :
$ who | awk } print $ 1 , $ 5 {
you 11 : 53
jim 11 : 27
$
login :
etc / passwd / .
$ sed 3 g / etc / passwd
root : 30 . FHR 5 KOB. 3 s : 0 : 1 : S . user : / :
ken : y 68 wd 10 ijayz : 6 : 1 : k . Thompson : / usr / ken :
dmr ; z 4 a 3 d j w bg v w c k : v : 1 : D-M. Ritchie : / usr / dmr :
$
$ sed 3 q / etc / passwd | awk f : } print $ 1{
root
ken
dmr
$
.
.
. .
.
awk . NR
. :
$ awk } print NR , $ o {
$0 . print
.
print print
f .
:
$ awk } print f % 4 d % s \ n , nR , $o {
4d % ( )NR S % ( )$0 \n
print f . print
f awk c; )print f ) .
( ind )
* awk } print f \ t % s \ n , $o { $
( )\ t .
109/322
etc / passwd / .
:
$ awk f : $ 2 = = / etc / passwd
( = = )
:
==2 $
/$^/2 $
/./ !2 $
= = )0length )$ 2
! .
.
length awk . !
(! ) $ 2 = =
! c sed ! .
awk .
.
( ) .
% :
NF % 2 !=0 # print if old number of fiflds
length :
/ ength )$o( > v2 # print if too long
awk : . # .
substr :
length )$o( > v2 }print line , NR , too long : , substr
()$o , 1 , 4
( s , m , n( substr s m n ( . 1
) . n m Substr .
. date
$ date
Thu sep 29 12 : 17 : 01 EDT 1983
110/322
END,BEGIN
awk BEGIN . END BEGIN
BEGIN
FS :
$ awk { BEGIN } FS = :
> $2 = = / etc/passwd
$ :
END :
$ awk END } print NR{
. awk
.
awk .
:
{}s=s + $1
END { } prints
NR .
END { } print s , s / NR
.
awk S . . .
.
awk c
111/322
{} s + = $1
END { } prints
S + = $1 $ + S1 = S .
:
} nc + = length )$o( + 1 # number of chars 1 form \ n
nw + = NF # number of words
{
END { } print NR , nw , nc
wc (
).
66 pr
. prpages :
$ cat prpages
# prpages : compute number of pages that pr will
| * print wc $
{(awk ! / total $ / } n + = int )) $1 + 55( / 56
END } print n{
$
pr ( ) .
int wc total .
.
$ * wc ch 4.
753 3090 18129 ch 1.4
612 2124 13242 ch 2.4
637 2462 13455 ch 3.4
802 2986 16904 ch 4.4
50 213 117 ch 9.4
2854 11172 62847
* $ prpages ch 4.
53
$
pr awk :
$ *pr ch 4. | awk End } print NR / 66{
53
$
awk .
112/322
S + = $1
x = abc
x > y
( . awk ) .
.
.
Awk NR .FS
. . . .
.. prpages . .
( )
.
( )workbench double .
double awk :
$ cat double
awk
FILENAME ! = prevfile } # New file
NR=1 # vesttine number
Prefile = FLIENAME
{
} NF> 0
(If )$( = = last word
Printf " double %s , file %s , line %d \ n", $1, fILENAMENR
(For )i = 2 , i < = NF, i + t
((If ) $ I = = $ ) i 1
Printf " double %s , file % s , line %d \ n " $I FLLENAME, NR
( If ) N F > 0
Lastword = $ NF
*{'$
$
+ + - - .
FILENAME . NR
.
if C:
(if )condition
113/322
statement 1
else
statement 2
condilion statement 1 else
statement 2 else .
for C :
(fo ) expressionl , condition , expression 2
statement
for while awk :
expression 1
} (while ) condition
statement
expression 2
{
)+ + for ) I = 2 , i < = NF, i
i NF .
break for while continue
condition while expression 2 .) for Next
awk . exit END.
: . awk
FILENAME
FS ( )
NF
NR
OFMT ( % g ( printf )
OFS ( )
ORS ( )
RS ( )
: . ( awk )
= + = - = * = / = % = ) )expr V= Vop , expr = Vop )
| | or : exprl || expr 2
114/322
expr 2 expr
$ $ AND : expr 1 $ $ expr 2
expr 2 expr 1
!
==>>= < < ! !
.
+ -
\ * %
+ + - - ( )
awk . awk
:
$ cat back words
# backwords : print in put in backward line order
{ awk } line ] NR[ = $o
* END } for ) i = NR ; i > o ; i - - ( print line ] i [ $
$
.
. .
tail :
$ tail 5 / usr / dict / web 2 | backwards
zymurgy
zumotically
zymotic
zymothenic
zymosis
$
tail seeking
. seek 1 ( . tail -r
).
115/322
.
Split :
(n=split)s,arr,sep
n 1 n arr . sep
Fs . " : "( a 0$ ,( split
/ etc/passwd ( " " / date " split )" 9/29/ 83 date
.
$ sed 1q/etc / passwd: awk' ]split)$o,a, ":",print a ]1[ [' root
'[[$ echo 9/29/83 : awk ' ]split)$o,date," / " ( , print date ]3
83
$
. awk .
: . awk
)cos)expr expr
) exp )pxpr expr: eexpr
) ( get line :
1
)index)s1,s2 expr S2 :s1
)int)expr
)length )s s
)log )expr expr
)sin )expr expr
s ] [ ]n a ]1 c ( s ,a,c( split n .
), sprint f)fmt fmt
/ .
.
susie 400
John 100
Mary 200
Mary 300
John 100
Susie 100
Mary 100
116/322
:
John 200
Mary 600
Susie 500
awk .
awk .
[]sum]$1[+=$2
}]END }for )name in sum( print name , sum ]name
-
.
( )$1 for
. for
(For )var in array
statement
for . for
Var .
.
Sort .
$ awk ' ' | sort + lny
( ) .
:
$ cat word freg
{awk' }for )I=1,i <=NF, I+t(num]$I[++
{[END }for )word in num(print word , num]word
*' $
$ word freg ch40* | sort +1 -nr | sed 20q | 4
the 372 .cw 345 of 22 is 185
to 175 a 167 in 109 and 100
.p1 94 .p2 94 pp9 $87
awk87 sed 83 tha 76 for 75
The 63 are 61 line 55 print 52
$
for num ( i$
awk i ) . for
.
117/322
. word freg cw
. tr wordfreg
word freg .
:
*[] [] Sed' s /
/g'$*:fort | uniq c | sort - nr
fold . .
.
fieldn n
who | field $
login awk . n
awk . :
' { 'awk' }print $' $1
$1 ( ) awk .
:
" { awk " }print 1 $ $ 1
1$ $ $1 n .
s,1 awk .
addup n n :
119/322
{'awk' } s += $' $ 1
'{END }print s
-n :
'awk
{'BEGIN}n = ' $ 1
} (for )i=1 ,i <n, I+t
sum ]i[ t = $I
{
} END }(for )i=1 , i <=n, , i+t
[Printf " % 4g " , sum]i
{
printf " , total = % 4g \ n " , total
'{
BEGIN n .
( )
n
.
awk
.
.
)calender )1 . ).
-
.
. calender in/ usr / you
.
$ cat calender
sep 30 mothers birthday
oct 1 1unch with joe.noon
oct 1 metting 4 pm
$
. awk
sed egrep
. .
120/322
. at .
:
$ date
Thu sep 29 15:23 : 12 EDT 1983
$ cat bin / calender
# calendar:Version 1 - - today only
awk < $ HOME / calendar
{(BEGIN } split )" " \ date \ " " , date
[$ 1= = date ]2[ $ $ $2 = = date ]3
| mail $NAME
$
BEGIN date .
Name login .
awk .
:
$ cat bin / calendar
# celendar : version 2 - - today only .no quotes
| ()date , cat sHOME / calendar
' awk
NR = = 1 }mon = #2 , day = $3{# set the date
NR > 1 $ $ $1= = mon $ $ $ 2 = = day # print calendar lines
| mail $ NAME
.
. .
.
. Nextman ,days
. days]" Jan" [ . 31 [" ( Jan"[ Feb,next mon) .
"dsys ]" Jan"[ = 31 , next mon ]"Jan"[ = " Feb
"days ]" Feb" = 28 , nextmon ]" Feb"[ = " mar
split :
$ cat calendar
# calendar: version 3 - - today and tomorroew
awk <$ HOME / calendar
} BEGIN
" x = " Jan 31 Feb 28 Mar 31 Apr 30 may 31
"" Jan 31 Aug 31 sep30 oct31 nov30 Dec31 Jan 31
(split )x , data
121/322
. - .
. .
. -
//
.- $NAME getname
122/322
. - rm
at .
awk .
:
print : print printf > (
) . <<
. > | .
: Rs
. .
: sed , ed .
.
NR = = 10 , NR = = 20
.
awk awk
. -
.
.
.
.
.
.
- - wc grep
.
.ls-1
awk
.
123/322
.
. (
)grep .
.
. .
- .
.
ps . . ls-1 . .
grep
( . . )
Sed .
. - Awk
- .
awk SNOBOL 4 sed
lex , yacc c . awk <
- c c .
.
FFC :
( )
awk .
124/322
125/322
:
.
. .
( sh)1
.
.
.
.
.
.
.
. . cal
.
( cal)1 :
$ cal
usage: cal ]month[year Good so far
$ cal october 1983
bad argument Not so good
$ cal 10 1983
october 1983
S M Tu W Th F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
$
126/322
cal 10
.
cal cal
. bin cal
. cal .
cal : cal .
cal cal
.
cal .
case :
Case word in
;; pattern( commands
;; pattern( commands
...
esac
word,case ( )pattern
.
. ; ;;( .
).
cal cal
. $#
- .
$ cat cal
$ cal: nicer interface to /use/bin/cal
case $ # in
# ;; set `date` ; m=$2; y=$6 : )0
# ;; m=$1; set `date`; y=$6 : )1
(* ;; m=$1; y=$2 : #
esac
casc $m in
(*jan*|Jan m=1 ;;
(*feb*|Feb m=2 ;;
( * mar * | Mar m=3 ;;
(* apr * | Apr m=4 ;;
(*may * | May m=5 ;;
127/322
$ # . * case : catch-all
( . catch-all
). y,m cal
.
case
`Set `date
.
:
: .
$#
*$
@$ * $ -
$- .
?$
$$
!$ &
$Home cd
$ IFS
$ MHJL you have mail .
$ PATtl
$ PS1 ''$
$ PS2 '>'
128/322
$ date
sat oct 1 06 : 05 : 18 EDT 1983
` $ set ` date
$ echo $1
sat
$ echo $4
06 : 05 :20
$
set .
. 2$ $1 . 'set 'date' $
2$ . case cal
.
Set -x -V
.
. case
. | case egrep big | :
small big . small * ]jJ[ an .
date . - .
: -
*
?
[]CCC ccc .
?$ [ ]a-do-3 []abcdo123
.
""
''
\c C .
a|b a,case b
/ case
/
.
0 0 .
case case
.
129/322
.
usr/bin/cal ) cal ) . cal
:
$ date
sat oct 1 06 : 09 : 55 EDT 1983
$ cal
october 1983
S M Tu W Th F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
$ cal dec
December 1983
S M Tu W Th F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 34
25 26 27 28 29 30 31
$
cal .
cal . cal
( calendar ) . ncal
.
case
ed .
.
ed
? .
^ $
.
:
$ 1S ?*. C$
130/322
$ 1S *.C
.
. - cal
usr/bin/
. - cal cal
cal . - :
$ cal oct nou
:
$ cal oct dec
cal jan
cal .
which .
cal . Mary
cal mary cal
Mary cal bin .
cal .
PATH
. echo
/ bin/echo PATH usr/bin/echo/0
. .
test :
test .
.
PATH
. FOR .
:
For i PATH
do
i .
131/322
done
'' sed $ PATH
. echo .
$ echo $ PATH
: usr/you/bin : .bin : / usr/bin
$ echo $ PATH | sed ''s/ : / /g
/usr / you / bin / bin / usr / bin !
$ echo `echo $ PATH | sed ` ' 's/ : / g
/usr / you / bin / bin / usr / bin 3
$
. PATH . . PATH
. .
test . .
PATH .
:
$ echo $ PATH | sed 's/^ : / . : /
> s/^ : : / : . : /g
> s/^ : $ /: . /
> 's/^ : / /g
. / usr / you / bin / bin / usr / bin
$
sed sed
.
PATH ( test)1 .
. test . test-r file
file test-w file test-x
( V ). test-f
. test
.
.
Q ( )
132/322
( ) . C.
grep
. test .
.
? $ :
$ cmp/usr/you/ .profile / usr / you / . profile
$ :
? $ echo $
0 Zero implies ran O.K .: files identical
$ cmp / usr /you / . profile / usr / mary /. profile
/ usr /you /.profile /usr /mary /.profile / usr / mary /. profile differ : char 6, line 3
? $ echo $
1
$
cmp grep S
.
if
if
then
else
fi
fi then : else else .
.
if case .
V Sysetem test if test
case . test case if
:
Case "$/" in
hello ( command
esac
Will be faster than
133/322
case if
. case
test if .
which
:
$ cat which
# Which cmd : Which cmd in PATH is executed , version 1
case $ # in
0( echo 'usage : Which command' 1>&2 ; exit 2
esac
for i in ` echo $ PATH | ' s / ^ : / . : /
s / : : / : . : /g
s / :$ / : . /
` 's/:/ /g
do
if test f $i /$1 # use test -x if you can
then
echo $i /$1
exit 0 # found it
fi
done
exit 1 # not found
$
:
$ cx which
$ Which Which
. / Which
$ which ed
/ bin / ed
$ mv Which / usr / you / bin
$ Which Which
/ usr / you / bin / Which
$
exit . exit 2
exit 1 exit 0
. exit .
test
( test ).
Test . .
$ echo 'echo hello >test
$ cx test
Which Which Which $
hello !
. /Which
$
&& if .
if :
dose not exist echo file test - f :
if test ! f ! .
then
echo file filename dose not exist
fi
. OR
. ( ) .
()
. OR .
&& AND .
. - PATH Which Opath
. - esac case fi if done
do
. - a which . PATH
.match=exit O . :
Which . - exit .
Which. - .
.
136/322
while :Loop
for . For
' For i in *.c ' * . 'i in For $ for
Which .
for . until , while , for :
.
Until while .
( ) while ( ) unit While .
unit .
:
for I in
do
$i
done
( for i ) * $
do
i $ .
Done
While
do
.
Done
until
do
Done
for * $ .
while until . Loop
(:)Mary
While sleep 60
do
Who | grep mary
dcne
Sleep 60 (
137/322
)
Mary .
Mary 60
.
mary .
until
mary :
until who grep mary
do
sleep 60
done
. . (
) watch who /tmp .
$ cat watch who
# watchwho : watch who logs in and out
.
( ) .
:
$ echo {? $ }var
hello O.K.;var is set
{?$ echo $ } junl
junk : parameter not set
{ !$ echo $ } junk?error
! junk : error
$
.
140/322
- .
t = $ } 1-60 {
60 . t $1
:-
$ Var
{$ } Var Var Var
{$ } Var-thing .
Var thing $ Var {$ } Var = thing
.
thing {$}var?message Var $ Var
thing.
$ }Var+thing{$
. $ Var message
. message Var: parameter :
not set
$ Var thing
.- true False bin / user /bin / .
Water for . - Joe
| mary .
. - watchwho Comm awk .
.- Watchwho who .
141/322
Trap .22126
-
0 ( )
1
2
3
9 ( ) DEL
19
( Ctl-1 )
( )
( Kill )1 .
watchwho trap
:
...
trap ' rm -f $new $ old ; exit 1' 1 2 15
while :
142/322
...
Trap
.
. trap exit .
: trap .
Trap
. - f rm .
Trap
:
& ( $ ) trap ' ' 1; long-running- command
2134
$
. trap
trap login long-ranning-comman .
( nohup )1 .
:
` $ cat `which nohup
trap '' '' 1 15
if test -t 2> &1
then
'' ' echo '' sending output to ' nohup . out
exec nice -5 $ * >>nohup.out 2>&1
else
exec nice -5 $ * 2>&1
fi
$
Test-t . nice
( . nohup PATH . )
exec exec .
. exec cal
/ usr/bin/bin .
:
143/322
:
$ Kill 9 . . .
kill -9 is not the default becaise a process killed that way is given no chance to put its affairs in order
before dying.
Kill-a
. - nohup .
. - times profile
CPU .
. - etc/passwd . (
) .
. Overwrite
command - 0
sort file1 0 file2 $
sort file > file2 $
file1 file2 > . - 0
.
- 0 . sed :
$ sed 'S/UNIX/UNIX(TM)/g' Ch2-O Ch2 !
. :
> . Overwrite .
:
sed 's / UNIX/UNIX)TM( / g' ch2 | overwrite ch2 $
:
# overwrite : copy standard input to output aftert EOF
# version 1. BUG here
PATH= /bin:/usr/bin
case $# in
(1 ;;
( ;echo 'Usage : overwrite file' 1>&2 exit 2
esac
new= /tmp/overwr . $$
trap ' rm -f $ new ; exit 1' 1 2 15
144/322
DEL .
CP CP Overwrite
.
. :
$ sed 's/UNIX/UNIX)TM(g' percious | overwrite precious
command garbled : s/UNIX/UNIX)TM( g
$ 1s -1 precious
-rw-rw-rw- 1you 0 oct 1 09:02 precious !*@# $%
$
Overwrite Overwrite
.
Overwrite .
Overwrie . ( ) Test-z :
.
Overwrite
145/322
: Overwrite
. nohup time nice
:
# Overwrite : copy standard input to output after EOF
# final version
opath=$ PATH
PATH=/bin:/usr/bin
case $# in
0 | 1 ( echo `Usage: overwrite file cmd ]args[' 1>&2; exit 2
esac
file=$1; shift
new=/tmp/overwr 1 . $$; old=/tmp/overwr2.$$
trap 'rm -f $new $old; $old; exit 1' 1 2 15 # clean up fikes
if PATH=$opath ''$@'' >$new # collect input
then
cp $file $old # save original file
trap ' ' 1 2 15 # we are committed; ignore signals
cp $ new $file
else
echo ''overwite : $1 failed ,$ fild unchanged'' 1>&2
exit 1
fi
rm -f $new $old
@ $" . $2 $3 . $1 $2 : Shift
. 5-7 . $* )" (
usr/bin/ bin/ PATH
. Overwrite
Overwrite now works )if somewhat clumsily(:
$ cat notice
UNIX is a Trademark of Bell laboratories
$ overwrite notice sed 's/UNIXUNIX)TM( /g' notice
command garbled: s/ UNIXUNIX)TM(/g
overwrite : sed failed, notice unchanged
$ cat notice
UNIX is a Trademark of Bell Laboratories Unehanged
$ overwrit notice sed 's /UNIX/UNIX)TM(/g' notice
$ cat notice
UNIX )TM( is a Trademark of Bell Laboratorise
$
146/322
Sed . Overwrite
:
$ cat replace
# replace: replace str1 in files with str2, in place
PATH = /bin:/usr/bin
case $# in
0 | 1 | 2 ( echo ' Usage: replace str1 str2 files ' 1>&2; exit 1
esac
left = '' $1 '' ; right = ''$2''; shift; shift
for i
do
overwite $i sed ''s@$ Left@$right@g'' $i
done
$ cat footnote
UNIX is not an acronym
$ replace UNIX Unix footnote
$ cat footnote
Unix is not an acronym
$
( for * $ ). @ /
@
22126.
replace PATH bin:/ust/bin/ .
Ovewrite replace usr/bin / .
. overwrite usr / bin / HOME/bin $ PATH replace
overwrite . .
. - overwrite O trap :
DEL :
trap '' echo exiting; exit 1'' 0 2
Sleep 10
. - -V replace deu/tty/ .:
S/$/sedt/$right/gsuflog
Replace . - .
. - replace i index
. - replace usr/bin/ . sed
147/322
( . -) $ overwrite file who | sort
. . eval)1(: ( sh )1 .
.
:Zap ..
kil .
PS
. .
say
.
ZAP Pick .
:Pick y
Pick( . )
ZAP Pick
.
$ cat zap
# zap pattetn: kill all ptocesses matching pattern
# BUG in this vetsion
PATH=/bin : /usr/bin
cas $# in
(0 ecvgo 'Usage: zap pattern' 1>&2; exit 1
esac
` ' {!kill `pick \ `ps sg | frep '' $* ''\ ` | awk ' }print $
$
...
$ zap sleep
?22126
148/322
0? q
PS Pick
.
:
For i in 1 2 3 4 5
.
( IFS ) `
. for IFS
:
$ echo ' echo $# ' >nargs
$ cx naegs
$ who
you tty0 0ct 1 05:59
pjw tty2 oct 1 11:26
`$ nargs `who
10
' = $ IFS
'
`$ nargs `who
2 .
$
: zap IFS
$ cat zap
# zap pat: kill all procsses matching pat
# final version
22126
PATH= /bin :/usr/bin
'=IFS
' # just a newline
case $ 1 in
( '' '' ;; echo 'Usage: zap ]-2[ pattern' 1>&2; exit 1
(* SIG= $1 ; shift
esac
' esac PID TTY 'TIME CMD
` ' {kill $SIG ` pick \` ps -ag | egrep '' $ * ''\` | awk '}print $1
$ ps ag
PID TTY TIME CMD
...
22126 0 0:00 sleep 1000
149/322
...
$ zap sleep
PID TTY TIME CMD
22126 0 0:00 sleep 1000? y
23104 0 0:02 egrep sleep? n
$
: (
SIG
) egrep grep
.Sleep | date echo
Ps .
zap kill .
cal Kill ZAP : - Kill
Zap . - ( Ps
) .
.
ZAP . - PS pipeline Ps
.
:Pick
pick .
. read
( ).
$ read greeting
. hello , world greeting
$ echo $ greeting
hello , world
$
read .profile
.TERM
Read . (
) for > < .
goodbye
illegal io
$ echo $greeting
goodbye Greeting
$
. for
. pick:
# pick: select arguments
PATH= /bin:/usr/bin
for i # for each argument
do
echo -n ''$i? '' >/dev/tty
read response
case $response in
(*y ;; echo $i
(*q break
esac
done </dev/tty
echo-n .
dev/tty/ .
break C : : . For q
. q
.
Pick :
$ pick ' 1 2 ' 3
?1 2
?3
Pick Return
. For i : .
:
$ grep for pick see what this version does
* for i in $
$ pick '1 2' 3
?1
?2
?3
151/322
$
. " * $ .
$ grep for pick
''*for i in ''$
$ pick ' 1 2 ' 3
?1 2 3
$
* $
.
: @ $
:
$ grep for pick
''@for i in ''$
$ pick ' 1 2 ' 3
?1 2
?3
$
@ $ * $
Overwrite .
:
* $ @ $
.
''* ''$ .
* ''@ ''$ :
.
Pick :
$ Pick > < mailing list
` $ Pick Cat mailing list
. Pick :
C .
.
152/322
: ( SIG
) egrep grep
' . 'sleep | date echo ps .
. - pick
. . q .
. - read set
. ( sh)1 enec dev/tty
( . ) .
(. - ) read . protile TERM .
. :news
news .
. news
.
news .
usr/news/
(news . )news usr/news/ (). news-time
.
. . news-time ;
usr/news/.
$ cat news
# news: print news files, vrsion 1
HOME=. # debugging only
cd . # place holder for /usr/news
` for i in `1s -t * $ HOME/ . news time
do
case $i in
( / .news time ;; break
( echo news: $i
22126
esac
done
touch $HOME/ . news time
$ touch x
$ touch y
153/322
$ news
news : y
news : x
$
touch
.
. news-time . *
case / .
. newd-time
$ rm .news time
4 news
$
LS
.
( . ) .
$ cat news
# news: print news files, version 2
HOME= . # debugging only
cd . # place holder for / usr/news
' = IFS
' # just a newline
`for i in ` 1s t * $ HOME/ .news time 2>&1
do
case $i in
( ' ' not found ;;
( / .news time ;; break
( ;; echo news : $i
esac
done
touch $ HOME/ .news time
$ rm .news time
$ news
news : news
news: y
news: x
$
IFS
./.news-time no found
.
154/322
news .
set LS-1
:
$ 1s -1 news
r wxrwxrwx 1 you 208 o ct 1 12:05 news
.
news :
# news: print news files , final version
PATH= /bin:/usr/bin
' =IFS
' # just a newline
cd/ usr/news
`for i in `1s - t * $ HOME/ .news time 2>&1
do
' ' =IFS
case $i in
;; ( ' ' not found
;; /.news time( break
( `set X` 1s -1 $i
'' echo
$i : )$3( $5 $6 $7
cat $i
esac
done
touch $HOME/ . news - time
155/322
. IFS
( not faund ) LS . IFS
LS .
. - -n news . news time .
. profile .
. - news .
get . :put
sed ,
awk .
.
: .
.
.
$ diff - e old new
ed old new
() .
:
.
. .
.
@@@
.
get : put
.
get put :
156/322
. 2d
$ rm junk
$ get junk
$ cat junk
a line of text
another line
$ get -1 junk
$ cat junk
a link of text
$ get junk
$ reolace antother 'a different ' junk
$ put junk
summary: second line changed
$ cat junk . H
a line of text
a different line
@@@ you sat oct 1 13:34:07 EDT 1983 second line changed
2c
another line
.
@@@ you sat oct 1 13:32:28 EDT 1983 one line added
2d
@@@ you sat oct 1 13:31:03 EDT 1983 make a new file
$
157/322
:
ed .
@@@ (diff)1
. @@@
.
get put
. Put . :
# put: install file in to history
PATH=/bin: / usr / bin
case $# in
(1 ;; HIST=$1 . H
(2 ;; echo ' Usage: put fill ' 1>&2; exit 1
esac
if test ! -r $1
then
echo ''put: can ' t open $1'' 1>&2
exit 1
fi
trap 'rm -f /tmp/put . ]ab[$$; exit 1' 1 2 15
' echo -n ' Summary:
read Summary
if get -o /tmp/put.a$$ $1 # previous versoin
then # merge pieces
cp $ 1 /tmp/put.b$$ # current versin
echo ''@@@ ` getname ` `date` $Summary '' >>/tmp/put.b$$
diff -e $1 /tmp/put.a$$ >>/tmp/put.b$$ # Latest diffs
sed -n '/^@@@/, $p' <$HIST >>/tmp/put.b$$ # old diffs
overwrite $HIST cat /tmp/put.b$$ # put it back
else # mske a new one
''echo ''put: creating $HIST
cp $1 $HIST
echo ''@@@ `getname ` ` date` $Summary'' >> $HIST
fi
rm -f /tmp/put.]ab[$$
put ger . -o
get . get put
. then
@@@ .
158/322
. overwrite
. put get
# get: extract file from history
PATH=/bin:/usr/bin
VERSION=0
while test ''$1'' ! = '' ''
do
case ''$1'' in
-i ( INPUT=$2; shift ;;
-o ( OUTPUT=$2; shift ;;
-]0-9[ ( VERSION = $1 ;;
* ( echo ''get: Unknown srgument $i'' 1>&2; exit 1 ;;
( case ''$OUTPUT'' in
'' '' ( OUTPUT = $1 ;;
( INPUT = $1.H ;;
esac
esac
shift
done
OUTPUT= $ }OUTPUT? ''Usage: get ]- o outfile[ ]-i file . H[ file ''{
INPUT = $ } INPUT - $OUTPUT.H {
test -r $ INPUT | | } echo ''get: no file $ INPUT'' 1>&2; exit 1; {
trap 'rm -f /tmp/get.]ab[ $$; exit 1' 1 2 15
# split into current version and editing commands
sed < $ INPUT -n ' 1 ,/^@@@/w /tmp/get.a'$$'
/^@@@/ ,$w /tmp/get.b'$$
# perform the edits
awk </tmp/get.b$$ '
/^@@@/ } count ++ {
!/^@@@/ && count > 0 && count < = '$VERSION'
END } print ''$d''; print ''w'' , '' ' $OUTPUT ' '' {
' | ed - /tmp/get.a$$
rm -f /tmp/get.]ab[ $$
- ]0-9[ . - O i .
while -1 ) ( O :
shift ) - i O( for shift test
ed - . for shift shift for
.
test -r $INPUT | | } echo '' get: no file $INPUT'' 1>&2; exit 1; {
:
159/322
if test ! -r $INPUT
then
echo ''get: no file $ INPUT'' 1>&2
exit 1
fi
( put ) | | .
{ }, exit get
. { }, do done ;
.
get . sed :
. awk . @@@ ( )
(
awk ) . ed $d : @@@ sed
W Overwrite .
get .
. - version :
version -5 file $
:
version sep 20 file $
`get `version sep 20 file $
version( . ).
get . - put .
.H .
. - .
.
. .
.
.
160/322
.
.
C ( . ).
.
cal .
. checlcmail , watchfor C
news C .
. .
UNIX .
: C
c . c
. c.
.
. - -c c
.
c :
ANSI C-C
W.C .M
C PDP-11 (
). c ( ) ( BCPL : )
( B) . PDP_7 R:C
K . c . (
)ANSI c ( ) ANSI C .
ANSI C-C . ANSI
. (
)
.
># include <stdio .h
( ) void main
}
;(print f )\ nHello world \n
{
hello.c gcc hello.c () .
a.out .
Hello world . c .
.
162/322
( main) .
. c main.
printf ,hello.c ( I/O /) stdio.h )
. c I/O ( ) . .
... ANSI C
. K & R ( ) .
printf hello word ( stdout x )
.
/n . printf .
:
<include < stdio.h#
()void main
}
;("printf)"\n
;("printf)"Hello World
;("printf)"\n
{
" \n .
"> #include<stdio.h I/O c . c
. " h
c . "> <....
.
void main main void . .
.
"; . { }.... .
c . . ( )
. ( ) . :
. sine sine.c
/************************/
/* Table of */
/* Sine Function */
/************************/
/* Michel Vallieres */
/* Written: Winter 1995 */
void main)(
}
int angle_degree;
double angle_radian, pi, value;
/* Print a header */
printf )"\nCompute a table of the sine function\n\n"(;
.
.
: *\ *\ ,
. .
# include math.h .
atsn sin( . compilation gcc sine.c
-lm).
( . . )
c .
: int
: short
:long
:float
: double ( )
:char ( )
. .
. ( .
) .
printf
:
; (printf)format ,variable
format variable .
:%nd ( n = 0 )
: %mnf ( m = n )
: %ns ( n = )
:%c
\ : n\t
: \g ( " ) ( )
165/322
. .
c . while.
( while )
{ ............... ......
}
for
( for )
{
........ .......
}
while . .
( ) .
for while.
( while )
{
....... ........
}
:
i = i ;
while ) i < = i )
{
... ...
i = i + i ;
}
for :
= for )i ; = >i ; = i + i
{
........ ............
}
( ( ; ; ) . ) for !
166/322
c break .
:
;angle_degree = 0
( ; ; ) for
}
......... ...........
;angle_degree = angle_degree + 10
;if )angle_degree == 360( break
{
if angle ( degree )
( )
# define .
#define ANGLE_MIN 0
#define ANGLE_MAX 360
ANGLE_MAx,ANGLE_MIN C .
. .
if while .
if ) i )
{
............. .........
}
( eles if )
{
...... ......
}
else
{
......... ...........
}
167/322
. .
( ) .
<
= <
= =
= !
= >
>
&&
| |
not
switch.
(switch )
{
; case
{
.......... .........
; break
}
; case
{
......... ........
; break
}
: default
{
.......... .........
}
}
case . break
case case .
. break .
.
168/322
c .
. .
.
; float X
x = 6.5
x 6.5
.
. ( )
"& . & ampx x.
c . ( "( ) points to
) .
;float x
;float* px
;x = 6.5
;px = &x
px x
"* ( .
)
*px x .
c . ( )UNIT
( ) . px x
px + 1 ( x
) .
x px +1 ( ) . . x
px +1 .
;char* pc
;float* px
;float x
;x = 6.5
;px = &x
169/322
;pc = )char*( px
(( )*chart " ) px : pc
. .x px+1 pc+1 .
:
()void main
}
;float x, y /* x and y are of float type */
;float *fp, *fp2 /* fp and fp2 are pointers to float */
/* perform arithmetic */
;*fp = *fp + 1.5
;(printf)"Final value of x is %f = %f \n", *fp, x
/* transfer values */
;y = *fp
;fp2 = fp
;(printf)"Transfered value into y = %f and fp2 = %f \n", y, *fp2
{
. ( printf
) .
.
c . :
[]
170/322
c 0 . .
c .
. v *V [ *)v+1( , v]0 [ v]1
.
.
#define SIZE 3
()void main
}
;[float x]SIZE
;float *fp
;int i
/* initialize the array x */
/* use a "cast" to force i */
/* into the equivalent float */
(for )i = 0; i < SIZE; i++
;x]i[ = 0.5*)float(i
/* print x */
(for )i = 0; i < SIZE; i++
;([printf)" %d %f \n", i, x]i
/* make fp point to array x */
;fp = x
/* print via pointer arithmetic */
/* members of x are adjacent to */
/* each other in memory */
/* *)fp+i( refers to content of */
/* memory location )fp+i( or x]i[ */
(for )i = 0; i < SIZE; i++
;((printf)" %d %f \n", i, *)fp+i
{
"I am a string
. c ( )ASCII .
" i " a " m ........ /0
.
printf .
;("printf)"Hello, world\n
;(printf)"The value of a is: %f\n", a
c . char .
. .
\o !
c .
string .
.
:
()void main
}
;[char text_1]100[, text_2]100[, text_3]100
;char *ta, *tb
;int i
/* set message to be an arrray */
/* of characters; initialize it */
/* to the constant string "..." */
/* let the compiler decide on */
[] /* its size by using */
;"?char message][ = "Hello, I am a string; what are you
}
k & .
: . R
void main)(
}
char line]100[, *sub_text;
/* initialize string */
strcpy)line,"hello, I am a string;"(;
printf)"Line: %s\n", line(;
/* add to end of string */
strcat)line," what are you?"(;
printf)"Line: %s\n", line(;
/* find length of string */
/* strlen brings back */
/* length as type size_t */
( /)I/o
I/o
( c ) 1/0 . ( ) getchar
stdin . ( ) putchar stdout . :
()void main
}
;int i, nc
;nc = 0
;()i = getchar
} (while )i != EOF
;nc = nc + 1
;()i = getchar
{
;(printf)"Number of characters in file = %d\n", nc
{
( . )
( ) ( stdin ) stdout ( x )
( stderr x) .
) getchar ( EOF . ) . stdioh (
end- of-file . . c
EOF . stdin nc .
"( end of file ) .
c :
<include < stdio.h#
()void main
}
;int c, nc = 0
174/322
()void main
}
;int c, nc = 0, nl = 0
I/O
printf stdout scanf stdin .
; ( scanf )format string ,variables
printf . ( . )
stdin printf
. :
;(sprintf)string, "format string", variables
;(scanf)string, "format string", variables
175/322
foo.dat .
. main
. .
C .
.
. ( ) stdion.h
.
math.h
> # include <math.h
. :
> : <stdio.h I/O
> : <ctype.h
> : <string.h
> : <math.h
> : <stdlib.h ( )
> :<time.h
:
> :<assert.h
> :<setjmp.h
> : <signal.h
> :<limits.h
> : <float.h
B K & R .
176/322
:
) ( )
{
.......... .........
........... ...........
; return
{
C int . .
. :
void ( )
}
........... ........
......... ..........
{
x :
/*includeheadersoflibrary */
/* defined for all routines */
/* in the file */
>#include < stdio.h
>#include < string.h
/* prototyping of functions */
/* to allow type checks by */
/* the compiler */
()void main
}
;int n
;[char string]50
/* strcpy)a,b( copies string b into a */
/* defined via the stdio.h header */
;("strcpy)string, "Hello World
. .
.
( ) ( C
).
>#include < stdio.h
()void main
} /* WRONG CODE */
;int a, b
;a = 5
;b = 7
;(printf)"From main: a = %d, b = %d\n", a, b
;(exchange)a, b
;(" printf)"Back in main:
;(printf)"a = %d, b = %d\n", a, b
{
;temp = a
;a = b
;b = temp
178/322
()void main
} /* RIGHT CODE */
;int a, b
;a = 5
;b = 7
;(printf)"From main: a = %d, b = %d\n", a, b
;(exchange)&a, &b
;(" printf)"Back in main:
;(printf)"a = %d, b = %d\n", a, b
{
;temp = *a
;*a = *b
;*b = temp
;(" printf)" From function exchange:
;(printf)"a = %d, b = %d\n", *a, *b
{
:
( ) .
.
)switches( .
179/322
. . ls .
- ls -1 . ls -1 -a
. tail -20 ( )
.
C . C .
) main . . (
( ) main
.
argv argv .
. :
(main ) integer ,char** argv
( char**argv argv (
) . ( ) chat*argv
. ) .
argv .
) argv
a.out -i . . 2 -g -x 3 q
argc = 7
"argv]0[ = "a.out
"argv]1[ = "-i
"argv]2[ = "2
"argv]3[ = "-g
"argv]4[ = "-x
"argv]5[ = "3
"argv]6[ = "4
.
.
.
>#include < stdio.h
.
. - .
.
.
For switch .
:
>#include < stdio.h
>#include < stdlib.h
;int a_value = 0
;float b_value = 0.0
;char* c_value = NULL
;int d1_value = 0, d2_value = 0
;int i
} ([switch )argv]i[]1
{
{
{
[ argv]++i i
. atoi atof stdlib.h .
( )ints ( )doubles .
:
a.out -a 3 -b 5.6 -c I am a string -d 222 111
( ('') -c
)
.
.
/********************************/
*/ */
/* Getting arguments from */
*/ */
/* the Command Line */
*/ */
/********************************/
/* Steve McMillan */
/* Written: Winter 1995 */
}
int i;
if )argv]i[]0[ == '-'( }
switch )argv]i[]1[( }
default: fprintf)stderr,
"Unknown switch %s\n", argv]i[(;
{
{
{
{
int a = 0;
float b = 0.0;
183/322
. printf/scanf
.
.
.
.
;(" printf)"Please enter the value of n:
;(scanf)"%d", &n
.
.
.
( )pipeline .
plot data .
. .
n ( x )
:
/* Simple program to illustrate use of a dialog box */
()main
}
/* Define default values: */
;int n = 0
;float x = 0.0
;(create_int_dialog_entry)"n", &n
;(create_float_dialog_entry)"x", &x
;(set_up_dialog)"Setup", 0, 0
;()read_dialog_window
184/322
cgfx .
create set- up dialog .
. read- dialog-window ( )
.
.
OK ( . Enter ).
. .
stdin/dstout .
.
185/322
:
sed awk
. C .
. :
() .
.
. .
. .
.
0 ) . zap
. :
.
C C C
. C
. C . W.B M.D
. ( .)1978
I / O I /
O C .
I / O C
.
:
.
. .
.
17vis:
: I / O
.
vis
186/322
nnn 1 nnn
Vis .
. vis 1010 :
cat x $
a b c
$ vis sx
a b c \ 01010101010 - - -
$
vis cat :
$ cat file1 file 2 ?| vis
) main (
}
187/322
; int c
while (( ) )) ) = getchar (! = EOF
if )isascii ) c ( $$
| | ) isprint ) c( | | c = = \ n c = = \ t | | ((
; ( put char ) c
else
; (print f ) \ \ % 30 , c
; ( ) exit
{
get char EOF ( ) .
EOF 2 . EOF
C int
.char EOF .
# > include < stdio.h
.
( )usr / include / stadio.h/ EOF . > stadio.
< h .
> . < ctype . h usr / include /
. isascii isprint ( Ascll
)0200 1 6 .
> < ctype . b .
exit vis
( )
main return o main . return exit
.
C .C vix.c cc
a.out a ( . ) :
$ cc vis . c
$ a .out
hello world ctl-g
helo world \ 007
ctl d
$
a.out . o cc
:
188/322
1 .6
\ t \ 011 vis
Vis .
c \ t \ \ \ b \ f .
.
sed - n 1 $ .
2 Vis .6 .
26 :vis 2
c . argc argv
main . ]argv ]0
argc ] argv ]1[ argv ] g r g c - 1 .
< >
.
vis vis-s :
.
( CRLF ) .
( ( / * vis : 2 */
> # include < stadio . b
> # inclue < stype . h
if )isassiicc( $ $,
(( ) isprintcc( | | c = = \ n | | c = = \ t | | c =
; (putchar ) c
(else if ) ! strip
; (print f ) \ \ % 30 , c
; (exit )0
{
6 1 <>ctype.b
) isalpha )c A-Z a-z :
) isupper )c A Z :
) islower ) c a z :
) isdigit ) c 9 : 0
) isxdigit ) c af AF 9 : 0
)isalnum ) c
) isspace ) c
) ispunct ) c
) isprint ) c :
) iscntrl )c c || 040 c < = 0 < = = 0177 :
) isascii )c Ascl : = < 0177 0 = < c
argv
NUL )Ascll )10
. vis
s .
( ) . ( strcmp )3
.
2 . 6 strmp.
(
).
190/322
3 . 6 s vis sn n
.
. string
. vis .
4 .6 c
. c
.
2 6
)stract )s , t t s s
)strnact )s , t , n n t s
)strcpy )s , t t s s
)strncpy )s , t , n n pad
)strcmp )s , t s t < , 0 > 0 = = > ,
)strncmp )s , t ,n n
)strlen )s s
)strchr )s , c c s Null
)strrchr )s , c c s null
index rindex
)atoi )s s
)atof )s s
)atof )s .
)malloc )n n null
)calloc )n,m nxm
null Malloc . * calloc char
) free )p malloc calloc
vis 3 6 :3
vis .
vis .
191/322
.
) ( getchar )getc )stdin ) putchar )c )putc c , stdout .
> < stdio . h
. 3 6
> <stdio . h .
vic .
. .
/ * vis : make funny characters visible )version 3( * /
; (exit )0
{
. argc
argv . vis
. 1
.
. ( get opt )3
.
.
ris :
vis )fp( / * make chars visible in FILE * fp * /
; FILE * Fp
}
; int c
(while )) c = get c )fp( ! = EOF
if )isaciicc( $$
) isprint )c ( | | c = = \n | | c = = \ t / | c = = ((
; ( put cahr ) c
(elw if ) ! strip
; ( printf ) \ \ % 30 , c
{
fprintf printf .
fclose Fopen
. ( )20
.
printf putc chunk
( .
) . fclose
Fclose . exit main
.
Stderr stdin stdout .
stdder Vis . stderr
stdout
( .
194/322
).
vis
.
.
5 .6 pritable
Printable . :
$ pr printable * | pr
7 grep .
Printable printable
4 6P :
cat .
cat ctl-s ctl- q
.
chunk
()
195/322
. P
P( .
).
p :
$ p vis . c
$ grep # define * . ] ch [ | p
c c
.
.
22 : 24
1
66 3
. p
22 .
. RETURN .
. ctl-d g p
.
:
p filenames $
t filenames | p . $
For :
$ for i in filenames
> do
> echo $ i :
> cat $ I
> done | P
.
.
.
P vis : print
196/322
.
/ * p : print input in chunks )version \( * /
># include < stdio . h
# define PAGESIZE 22
char * programe ; / * program name for error massage * /
(main )argc , argv
; int argc
; [ ] char * argv
}
; int i
; FILE * fp , * efopen < 1
; [Programe = argv ]0
( if ) argc = = 1
; (print )stadin , PAGESIZE
else
} ( for ) i = l ; i < argc ; i + +
; (fp = < fopen )argv ]i [ , r
;(print )fp , PAGESIZE
;(fclose )fp
{
;(exit )0
{
efopen .
. ( ) efopen
main .
(FILE * efopen )file , mode / * fopen file , die if )an.t * /
; Char * file , * mode
}
; ( ) FILE * fp , * fopen
; Extern char * programe
efopen .
.
. efopen
.
197/322
efopen .
p print .
Print )fp , pagesize( / * print fp in pagesize chunks * /
; FILE * fp
; Int pagesize
}
static int lines = 0 ; / * number of lines sofar * /
; [char buf ] BUFSIZ
(while )fgets )buf , size of buf , fp( ! = NULL
(if ) + + lines < page size
; (fputs )buf , stdout
} else
;buf ] strlen )buf( 1[ \ 0
; (fputs )buf , stdout
; (fflush )stdout
; ( ) ttyin
; lines = 0
{
{
atoi ( ( atoi )3 ).
p . ed
. ( system )3
. ttyin :
} ( ; ; ) for
if )fgets )buf , BUFSIZE , tty( = = NULL | | buf ]0[ = = q
; (exit )0
} ( else if )buf ]0[ = = !
; (system )buf + 1 / * BUG here * /
; (printf ) ! \ n
{
else / * ordinary line * /
; [return buf ]0
{
{
ttyin . system
p p :
$ cat / etc / passwd | p 1
root : 3 D : f HR 5 KoB. 3 s : o : 1 : s . usr : / : ! ed ed p
etc / passwd ed /
!
4 7
. ttyin
system 7 .
vis p cat .
cat p v
.
.
.
.
cat vis Cat .
vis . . cat p
Cat . p . p
: . .
6 6 p pagesize
200/322
7 6 p ( )
( . ) .
.
.
9 6 p p .
)isutty )3 :
5 6pick :
Pick 5 . c c
5 . .
Pick . .
zap 6 5
:
kill $ SIG pick [ ps ag | egrep $ * \ | awk }print $ 1 {
egrep pick
zap .
cat .
/ * pick : offer choice on each argument * /
> # include < stdio. h
char * programe ; / * program name for error message * /
pick .
.
10 6 pick rm i
6 6
.
.
.
.
.
pick . ( .
).
pick ) s( / * offer choice of s * /
; Char * s
}
; ( fprint ) % s ? , s
( if )ttyin ) ( = = y
; ( print f ) % s \ n , s
{
202/322
pick . c o pick ( ( $
pick * . c $
! )memory fault )ore dumped
memory fault
. Bus error .
.
cor dumped core .
\ctl 1
kill-3 .
adb .sdb Adb .
sdb .
.
:
stack trace .
stack trace .
stack trace adb c $ :
adb pick core $ adb
c $ stack trace
) - strout 0 011 )011200 01155772
adjust :
fillch : 060542
( 0 011 doprnt ) 011200 01155722
( f print f )0177345 011200
iop : 011200
fmt : 0177345
args :
(pick )0177345
s: 0177345
( main )0177234 035
argc : 035
argv : 0177234
i: 01
buf :
ctl d
203/322
$
main pick fprintf fprintf doprnt
doprint - strout . dopmt pick.c fprintf
( . traceback c$ .
c$ adb )
sdb :
$ sdb pick core
warning : a . out not compiled with g
. iseek : address oxa 64
* t stack trace
( ) isseek
(fprintf )54 91 47 47 21 540 61
( pick ) 54 91 47 47 21
( 21 47 47 89 88 main ) 12 91 47 47 21 30
*q
$
:
fprintf . ) traceback 11/750 VAX
I / O) . pick fprintf :
; ( f print f ) % s , s
stderr % s? FILE
.
.
)lint )1 c .
Lint c . lint
pick.c :
$ lint pick . c
fprintf 28
. .
Lint Lint .
. lint
204/322
. lint
.
7 6zap :
zap 5
. zap :
. zap c .
: ps Ps .
zap . ps
. ( popen )3 fopen . .
pclose .
/ * zap : interactive process killer * /
> # include < stadio . h
> # include <signal. h
; char * programe / * program name for error message * /
char * ps = ps ag ; / * system dependent * /
( main ) argc , argv
; int argc
; [ ] char * argv
}
; ( ) FILE * fin , * popen
; [Char buf ] BUFSIZ
; int pid
; [progname argv ]0
} (if )) fin = popen )ps , r (( = = NULL
; (fprint f )stderr , % s : cant run % s \ n , progname , ps
; (exit )1
{
; (fgets )buf , size of buf , fin / * get header line * /
; ( fprintf )stderr , % s , buf
( while )fgets )buf , size of buf , fin( ! = NULL
} ( if )argc = = l | | strindex )buf , argv ]1[ (> = 0
buf ] strlen )buf ( 1[ = \ 0 ; / * suppress \ n * /
; (fprintf )stderr , % sp , buf
} ( if )ttyin ) ( = y
;sscanf )buf , % d , $ pid0
; (kill )pid , SIGKLL
{
{
205/322
; (exit ) 0
{
ps ag ( )
. fgets ps
.
sscanf )scanf )3 .
.
kill SI GKI LL
> < signal. h . 5
9 .
.
zap Ps
. . zap
ps . ( s1 , s2( strindex
ps
st rn cmp ( 2 .)6
Strindex s1 s2 1
s2 .
4 6 I / O .
11 6 zap . zap
. :
.)get pid )3 :
206/322
12 6 )fgrep )1 strindex .
. fgrep
8 6idiff :
Diff .
diff
.
idiff )diff ) diff
from to .
idiff . idiff . out .
:
file 1 : file 2:
this is this is
a tegt hot a test
of of
your our
skill ability
and comprehension
diff produces
4 6 I / O
)fp = fopen ) s , mode < r w a
)NULL )
)c = gctc )ff ) ( getc ) st din( , get char
)putc )c , fp )putc ) c , std out( , put char ) c
)ungetc )c , fp fp char
.
) , scanf )fmt , al ( a) , , ( stdin fmt
. ai .
EOF .
) , f scanf )fp fp
) , ss can f )s s
207/322
> our
> ability
$
: idiff
$ idiff file 1 file 2
2c2
< a test
> our
208/322
> ability
<? (>)
idiff out put in file idiff . out
$ cat idiff . out
this is
not a test
of
your
skill
and comprehesion
$
our
ability
ed .
! cmd idiff . diff
. idiff diff
. idiff diff :
/ * idiff : interactive diff * /
> # include < stdio . h
> # include < ctype . h
char ; * programe
# define HUGE 10000 / * large number of lines * /
; [ progname = argv ] 0
} (if )argc ! = 3
; (fprintf )stderr , usage ; idiff file 1 file 2 \ n
; (exit )1
{
209/322
mktemp )tempfile( ;
nf1 = nf2 = 0 ;
while )fgets )buf , size of buf , fin( ! = NULL( }
parse )buf , $ from1 , $ from 1, $ to 1 , $ cmd , $ from 2 , $ to 2(;
n = tol from1 + to2 from 2 + 1 ; /* # lies from diff * /
if )cmd = = c (
n+=2;
else if )cmd = = a (
from 1 + + ;
else if )cmd = = d (
from 2 + + ;
printf ) % s , buf( ;
while )n - - > 0( }
fgets )buf , size of buf , fin( ;
210/322
print f ) % s , buf ( ;
{
do }
print f ) ? ( ;
fflush )stdout( ;
fgets )buf , sizeof buf , stdin( ;
switch )buf ]0[ ( {
case > :
nskip )f1 , to 1 nf1( ;
n copy )f2 , to2 nf2 , fout( ;
break ;
case < :
nskip )f2 , to2 nf2 ( ;
n copy )f1 ,to1 nf1 , fout( ;
break ;
case e :
ncopy )f1 , from 1 1 nf1 , fout( ;
nskip )f2 , from 2 1 nf2( ;
ft = efopen )tempfile , w ( ;
ncopy )f1 , tol + 1 from1 , ft( ;
fprintf )ft , - - \ n ( ;
ncopy )f2 , to 2 + 1 from 2 , ft (;
fclose )ft ( ;
sprint f )buf2 , ed % s , temp file(;
system )buf2( ;
ft = efopen )tempfile , ed % s , temp file( ;
system )buf2( ;
ft = efopen )tempfile , r ( ;
ncopy )ft , MUGE , fout( ;
fclose )ft(;
break ;
case ! :
system )buf + 1( ;
printf ) ! \ n ( ;
break ;
default :
print f ) < or > ore or ! \ n ( ;
break ;
{
{ while )buf ]0[ ! = < $ $ buf ]0[ = > $ $ buf ]0[ ! = e (;
nf 1 = to 1 ;
nf 2 = to 2 ;
{
ncopy )f1 , HUGE , fout ( ; / * can fail on very long files * /
unlike )tempfile( ;
211/322
{
diff parse
. )d a b c (
. diff parse
parse )s , pfrom 1, pto 1 , pcmd , prom 2 , pto2(
char * s ;
int * pcmd , * pfrom1 , * pto1 , * pfrom2 , * pto2 ;
}
# define a2i )p( while )is digit )*s( ( p = 10 * )p1 + * s + + - o
* prom1 = * ptol = * pfrom 2 = * pto 2 = o ;
a2i ) * pfrom1 (;
if )* s = = , ( }
s++;
a2i )* pto1( ;
{ else
* pto 1 = * pfrom 1 ;
* pcmd = * s + + ;
a2i ) * pfrom 2( ;
if )* s = = , ( }
s++
a2i ) * pto2( ;
{ else
* pto 2 = * pfrom 2 ;
{
. Ascll a2i
: ncopy nskip
nskip )fin , n ( / * skip n lines of file fin * /
FILE * fin ;
}
char buf ]BUFSIZ[ ;
while )n - - > 0(
fgets )buf , sizeof buf , fin( ;
{
n copy )fin , n , fout( / * copy n lines from fin to fout * /
FILE * fin , * fout
}
char buf ]BUFSIZ[ ;
while ) n - - > 0( }
if )fgets )buf , sizeop buf , fin( = = null(
return ;
fputs )buf , fout( ;
{
{
212/322
idiff idiff
tmp/ .
.
zap idiff .
.
.
16 6 diff
. idiff .
.
9 6
c
.
24 . p
p :
$ p 36
: bin
$ cat / usr / you / bin / p
* exec / usr / bin / p 36 $
$
P .
213/322
PAGESIZE profile :
PAGESIZE = 36
Export PAGESIZE
( ) var getenv var
NULL
. getenv p .
. getenv
/ * p : print input in chunks )version 3( * /
; ( ) char * p , * getenv
; [progname = argv ]0
if )) p = getenv ) PAGESIZE (( ! = NULL
; (pagesize = atoi )p
} ( if )argc > 1 $ $1 argv ]1[ ]0[ = =
;( [pagesize = atoi ) $ arg v ]1[ ]1
; argc - -
; argv + +
{
.
.
17 6 idiff .
PAGESIZE 2 3 .
I/O .
.
p .
adb sdb lint .
Idiff Diff .
( .
] .w. G.T CACM 1977
) . diff D.M W.J 41
214/322
1976 .
diff .
.
215/322
.
.
. 1/0
. pntc,fopen winod .
. .
:
Delete .
6 .
.
2
I/O17 :
I/O . .
.
.
.
. . .
( )
. . I/O
. .
216/322
. FILE 6
. .
fileno)ff( macro . > <stdio.b .
.
.
0,1 2 .
0
1 2 1/0 . 3
4 .
1/0
0 1 . 2
. < filename 2 2 &1
( .
.
I/O
write , read c
. .
. .
;Int fd,n,nread , written
;[Char buf ]SIZE
;(Nread = read )fd,buf,n
;(Nwritten=write )fd , buf,n
.
n ( . red
).
-1 .
.
1
( ) 512 1024 ( BUFSIZ
> <stdio .b ) .
.
217/322
cat
/*cat:minimal version*/
#define SIZE 512/*arbitrary*/
main]1
.
[char buf]SIZE
;int n
while ))n=read )0,buf , size of buf((>0
;(write )1,buf,n
; (exit )0
SIZE read write
read .
I/O
.
. ed . cat
54000 6 : SIZE
(Tim )user + system, sec
SIZE Pdp= 11.70 Vax-11.750
1 0/271 8/188
10 9/29 3/19
100 8/3 6/2
512 3/1 0/1
1024 2/1 6/0
5120 0/1 6/0
.
redslow cat
redslow . I/O
EOF .
/*readslow:keep reading, waiting for more*/
# define SIZE 512/*arbitrary */
[]main
[char buf]SIZE
; int n
}(; ;)for
while ))n=read )0,buf , sizeof buf(( >01
;(write )1,buf , n
;(sleep )10
{
sleep )sleep )3 .
readslow cpu
. readslow
. . read .
. 7-1 n readslow n .
( f ) tail tail readslow .
.
. 7-2 readslow
fstat 7-3 .
.
. creat , open
Open fopen
int .
; Char*name
;Int fd , rwmode
;(Fd=open )name , rwmode
fopen name . mode rw :
mode 1 2 open 1- .
.
. creat
219/322
.
; Int perms
;(Fd = creat )name , perms
Creat name -1
. Creat perms .
Creat .
( ) perms .
2 .
. 0755 .
.
c .
cp .
.
.
/*cp:minimal version*/
#include cstdio-hs
# define PERMS 0644 /*RW for owner , R for group , others*/
; char * progname
main)arg,argv(/*cp:copy fito f2 */
;int argc
;[ ] char * grgv
; int f1, f2 ,
;[char buf ]BUFSIZ
[programe = qrgv ]0
(if )argc !=3
;(error )usage:%s from to , progname
if ))f 1= open )argv ]1[ , 0((=-1
;([error )cant create %s , argv]2
(while ))n=read )f,buf , Bufsiz((>0
;(if )write error , )char *(0
;(exit )0
{
error .
( 20 NOFILE > <sys/param.hs)
.
. close
. exit
.
220/322
unlike .
error
-1 . .
error .
( 2 ) error
sys-errlist error
. error :
&
lseek
I/O : .
. lseek
.
; Int fd , origin
;()Long offset , pos, lseek
;(Pos=lseek )fd , offset , origin
fd offset
origin . origin . 0 1 2
offset -1 .
:
;(Lseek )fd , ol,2
( )
; (Lseek )fd , ol ,0
:
;(Pos = lseek )fd,ol,1
offset : ol 1( . lseek
seek ).
: seek .
.
Get )fd , pos , buf , n(/*read n bytes from position pos*/
;Int fd,n
Long pos
;Char*buf
If )lseek)fd ,pos,0(=-1( /*yet to pos*/
;Return -1
Else
;(Return read )fd,buf ,n
{
. 7-3 readslow e :
& readslowe
readslow lseek .
222/322
27 :
.
. spname
.
;(N=spname )name ,new nume
. .
newname . n spname 1-
. n 0 1 .
Spname P . ( p
:
&p/urs/.YX/comd/p/spnam.c
/usy/syc/cmd/p/spname.c:y
/*spname: return correctly spelled filename*/
spname
. .
.
inode
inocle . .
. inode . > <sys>dir.h
:
& cat/usr/include / sys/dir.h
#define DIRIZ 14 /*max length of file name*/
struct direct /*structure of directory entry */
}
ino-td- ino; /*inode number */
char d-name ]DIRSIZ[ ; /*file name */
;{
&
ino-t typedef inode ino-t . short
PDP-11 VAX
: . typedef >
223/322
int d , nd , fd;
struct}
ino-tino ;
char name ]DIRSIZ+1[ ;/*1 more than in dir.b*/
{nbuf ;
nbuf . name ]DIRSIZ[=IO ;/*+1 for terminal io */
if )dir ]0[ == 0( /*current directory */
dir =0;
d=3; /*minimum distance*/
if ))fd = open)dir , 0(( == -1(
return di
while )read )fd,)char *(&nbuf , sizeof)structdirect((>0(
if)nbuf.ino(}
nd = spdist )nbuf name,guess(;
if)nd<=d&nd !=3(}
strcpy)best , nbuf . name(;
d=nd;
if )d==0( /*exct match*/
break ;
{
{
close )fd( ;
returnd ;
{
mindist 0 mindist
sizeof read .
.
cnode ( )
.
If )nd <=d...(
if )nd <d...(
. 0
/*spdist : return distabce between two names*/
/*
*very rough spelling metric:
*o if the string are identical
* 1 if the string are identical
*2 if one char wrong,added or deleted
*3 otherwise
*/
# define E& )s,t()strcmp )s,t(==0(
spdist )s,t(
char*s,*t;
225/322
}
while )*s++==*t(
if )*t++== io(
return o ; /*exact matoh */
if )*--s(}
if )*t(}
if )s)1[&&t ]1[&&*s == t ]1[
&&*t == s]1[&&E* )S+2 , t+2((
return 1; /*transposition */
if )Ea )s+1 , t+1(
return2 ; /*1 char mismatch */
{
if )Ea)s+1, t((
return2; / *extra character */
{
if )*t&& E&)S,t+1((
return 2; /*missing character */
return 2 ;
{
: p spname
/*p:print input in chunks )version 4 (*/
# include cstdio.bs
# define PAEGSIZE 22
char *progname ; /*programname for error message */
main )argc , argv(
int argc ;
char*argv ] [ ;
}
FILE *FP,*efopen ) ( ;
Int ; pagesize= PAGESIZE ;
Char *p,get v ) ( , buf ]BUFSIZ[;
Programe = argv]0[ ;
If ))p=getenv )PAGESIZE(( !=NULL(
Pagesize = atoi)p(;
If )argc>1&&argv ]1[ ] 0[ == - }
Pugesize = atoi)&argv ] 1[ ]0[ (;
Argc --;
Argv ++;
{
if )argc==1(
print )stdin ,pagesize(;
else
for )I=1 , I<argc;I ++(
switch )spname )argv ]I[ ( , buf((}
case -1 ; /*nomatchpossible */
fp = efopen cargv ]I[ , r(;
break;
226/322
casel: /*corrected */
;(fprintf )stderr, 1 %s \, buf
(if )ttyin ) ( ==n
; break
; argv ]I[ = buf
/*fall through...*/
case 0: /*exact match */
;(fp= efopen cargv ]I[ , r
;(print )fp,pagesize
;(fclose )fp
{
;(exit )0
{
p p
.
. 7-5 spname
.
. 7-6 tx tc c .
.
mindist . 7-7 . p
:
spname . 7-8
.
. 7-9 spname
&...fix progfilenames
cd spname
37 inodes :
.
2 .
inodes . inode stat ><sys /stat .b
:
227/322
:SV
SV CP
SV .
. SV SV .
inode checkmail . sv :
& sv file 1 file 2... dir
1 dir/file1 2 dir/file 2
.
sv s/ .
/*sv: save new files */
># include <stdio .h
># include <sys/types.h
># include <sys/stat .h
># include <sys/stat .h
; char*programe
(main)argc,argv
230/322
int argc;
char *argv ] [ ;
}
int I ;
struct stat stbuf ;
char *dir = argv ]argc-1[
progname = argv ]0[ ;
if )argv<=2(
error )usage :%s files ...dir , progname(;
if )stat )dir,&stbuf(== -1
error )cant access directory % s, dir(;
if ))st buf.st-mode &s-ifmt(! =s-ifdir(
error )%s is not a directory , dirl;
for )I=1,I<argc-1 , I ++(
sv)argv ]I[ , dir( ;
exit )0( ;
{
inode 0:00( GMT )1970
st-mtime .
SV)file , dir( /*save file in dir*/
Char *file , * dir
}
struct stat sti , sto ;
int fin , fout , n;
char target ]BUFSIZE[ , buf ]BUFSIZ[, *index2
sprint f )target , %s/%s , dir , file( ;
if )index )file , /(! =Null(/*strchrcin some systems */
error )wont handle/s in %s , file(;
if )stat )file, &sti (=-1
error)can't stat %s , file( ;
if )stat)target , &sto (==-1( /*target not present*/
stoost - mtime =0 ; /* somake it look old */
if )sti.st-mtime< sto .st-mtime( /*target is newer */
fprint f )stderr , % s : % s not copied\n ,
progname ,file ( ;
else if ))fin=open )file ,0( == -1 (
error )can't open file % s , file (;
else if ))fout = creat )target , stist - mode(( == -1
error )can't create % s , target(;
else
while ))n=read)fin , buf , sizeof buf ((>0(
if )write )fout , buf , n (!= n(
error )error writing % s , target(;
close )fin( ;
close )fout ( ;
{
231/322
I/O creat sv ( .
strchr , index )string )3
) .
sv .
.
> <dir . b > <stat.h
.
.
sv .
.
.
Sv sv
. write . write
. .
. write
.
.
. .
. 7-10 chekemail .
lseek-ss canf
chekmail . 7-11 .
( ) checkmail
. 7-12 watchfile
.
sv . 7-13 .
.
sv . 7-14 :
cp . . sv,cp cp-v
.
. 7-15 random :
232/322
47
.
system . . 6 system .
( ) .
sprintf .
system
.
execup,execlp
execlf . .
:
; )Execlp )date , date , )char *(0
execlp execlp ( PATH )..
.
argv . ( .
exec)2( , execlp ) .
execlp .
. .
(execlp )date, date , )char*(0
;fprintf )stderr , couldn't execute date \n
233/322
;(exit )1
execlp execup .
:
;(Execvp )filename , argv
argp ( ) argv Null
execvp . execlp
argp argv [ argp ]0 .
> * <, .
bin/sh/ execlp .
:
; )Execlp )/bin/sh , sh , -c , comrnadline, )char* ( 0
c .
exec waitfile . & ]wait file filename ]command
. .
. wailfile troff
& & wait file troff . out echo troff done
wait file fstat .
/* wait file : wait until file stops changing*/
># include <stdio.b
># include <sys/types.h
># include <sys/stat.h
; char * progname
(main)argc,argv
; int argv
; [ ] char *argv
}
; int fd
; struct stat stbuf
;time-t old - time = 0
; [progname = argv ] 0
(if )argc<2
; (error )dsage : % s file name ])md[ ,progname
(if )) fd=open )argv ]1[ , 0(( == -1
;([error )cant open % s , argv ]1
; ( fstat )fd , &stbuf
}(while )stbuf . st -mtime != old - time
; old - time = stbuf.st - mtime
; (sleep )6-
234/322
wait,fork
execlp execbp .
.
fork :
; ( )Proc-id=fork
. fork
. process-id )proc id )child . )proc - id ,)parent
process - id .
:
( If )fork ) ( = = 0
;(Execlp )/bin /sh, sh , c , command line , )char *(o
fork . .
fork execlp .
fork execlp ( fork 1-
) .
wait
235/322
:
; Int status
(If )fork ) ( = = 0
;(Execlp )000 /*child */
;( Wait )&status /*purent*/
execlp fork
)process-id , wait( . . fork
) . .
system .
Stats wait 8
8 .
exit main .
. fork exec
.
execlp
. .
I/O 6
I/O .
execip system :
system .
ed . ed
.
system ,p .
dev/tty/ .
)dup)f fd
.
:
;In fd
; (Fd = open )file , 0
; (Close )0
; (Clup )fd
; (Close )fd
)Close )0 .
236/322
progname ) system(
. . .
/*
*safer vesion of system for interactive programs
/*
# include <sigul.h>
# include <stdio.h>
system )s( /*run command lines */
char*s ;
}
int status >,pid ,w ,tty ;
int )*istat() ( , )*qstat () ( ;
extern char * progname ;
fflush )stdout(;
tty= open )/dev/tty , 2(;
if )tty= = -1(}
fprintf )stderr,%s : can't open /dev/tty\n , progname(;
return-1;
{
if ))pid = fork )1( = = 0(}
close )0( ; dup )tty( ;
close )1(; dup )tty( ;
close )2( ; dup )tty( ;
close )tty( ;
execlp )sh , sh ,-c , s , )char *(
exit )127( ;
{
close )tty( ;
istat = signal )SIGNT , SIG -1 GN( ;
qstat = signal)SIGQUIT , SIG- IGN(;
while ))w=wait )&status((! = pid &&w! = -1(
{
close )tty( ;
istat = signal )SIGNT , SIG-!GN(;
q stat = signal )SIGQUIT , SIG - ICN(;
while)) w= wait )cstatus(( ! = pid&& w! = -1(
;
if )w = = -1(
status = -1 ;
signal )SIGINT , istat(;
signal )SIGQUIT ,gstat(;
return stats ;
{
dup ed . 2 dev / tty/
.
237/322
. :
& echohello 1>&0
hello
&
2 dup'ed
dev/tty/ . system : tty
ttyin p .
system - ed
system .
.57
( ) .
.
DEL
( ctl -1 (FS kill .
.
( )sdb)1( adb)1 ) .
. .
.
. > <signal .h . :
># include <signal.h
.
; (Signal )SIGINT , SIG-IGN
; (Signal )SIGINT , SIG - DEL
. signal .
signal ( ) .
signal .
.
># include < signal-h
;char * tempfile = temp .xxxxxy
main)1
238/322
}
; ( ) extern onintr
(if )signal )SIGINT , SIG-1 GN( ! = SIG-IGN
; (signal )SIGINT , onitr
;(mktemp)tempfile
/*process...*/
; (exit )0
{
onitr ) ( /*clean up if interrupted */
}
; ( unlike )tempfile
; (exit )1
}
double test signal pmain
( & )
.
onitr .
. signal .
.
. :
.
:
># include <signal.h
># include <sp-timp.h
; jmp -buf sjbuf
( )main
}
; ( ) int onitr
(if )signal )SIGINT , SIG-IGN(!=SIG-IGN
;(signal /)SIGINT , onitr
setjmp )sjbuf(; /* save cvrrent stack position */
} (; ;) for
/* main processing loop * /
{
.
{
onitr ) ( /*reset ifinterrupted */
}
239/322
.
240/322
( ! ) ed
. .
(If )fork ) ( = = 0
; (Execlp )...
Signal )SLGINT , SIG-IGN(; /*parent ignores interrupts */
Wait )& status ( ; /*until child is doen */
Signal )SIGINT , onitr(; /* restore interrupts */
.
.
. wait
.
.
. system :
># include <signal -h
sgstem )s( /*run command line */
; char * s
}
; int status , pid .w,tty
; () (int )*istat( ) (, )*qstat
.
} ( If ))pid = fork ) (( = = 0
.
; (Execlp )sh , sh , -c , s )char *(0
; (Exit )127
{
..
;(istat = signal )SIGINT , SIG-IGN
;(qstat = signal )SIGQBIT , SIGIGN
while ))w = wait )&status (( ! = pid&& w ! =-1
;
(if )w = = -1
; status = -1
;(signal )SIGINT , istat
;(signal )SIGQVIT , qstat
; return status
}
signal .
signal . SIG- DFL , SIG LGN
. VAX , PDP-11
> <signal.h .
241/322
)alarm)n SIGALRM .
( alarm )
alarm alarm .
time out
. alarm . watch for 1 .
:
& & time out - 3600 wathcfor dmg
time out .
alarm . alarm .
.
/* time out : set time limit on a process */
># include <stdio .h
># include <signal .h
intpid ; /* child process id */
;char * progname
(main )argc , argv
; intargc
; [ ] char* argv
}
; ( ) int sec = 10 , status , on alarm
; [progname = argv ]0
}(if )argv>1&& argv] 1[ ]0[= = -
; [ sec = atoi )&&rgv ]1[ ]1
; argc --
;argv ++
{
(if )argc <2
;( error )usage . % s ] 10[command , progname
} ( if ))pid = fork )1( = = 0
; [execvp cargv ]1[ ,& argv ]1
; [error )couldnt start % s ,argv ]1
{
;(signal )SIGALRM , onalarm
;(alarm )sec
(if )wait)status ( = = -1 : : )statis & 0177 (!= 0
; [error )% s killed , argv] 1
;(exit ))status ((8( & 0377
242/322
{
onalarm )( /* kill child when alarm arrives */
}
;( kill cpid , SIC KILL
{
. 7-18 SLEEP )PAUSE )2
alarm , sleep
.
1978( ) BSTJ .
]BST (
) 1979 79
.
readslow
.
readslow
( .
) .
b spname .
CACM 1983
.
243/322
9
UNIX .
.
Basic .
.
.
:
: Yacc- ( )parser .
. : Make-
: Lex- yacc .
.
.
.
. :
/ - + .
.
a z .
exp,sin ...
( 1 T n 1 T .
) .
. ( )statument
. feature
( )5 .
uelse : while { }and
...
> . ... , . . . < ,
.
----- .
6 9 .
244/322
UNIX . ( )2 .
. C
UNIX
.
. 3
.
. hoc high order calculator .
hoc 2, hoc 1 ... .
81 :1 :
hoc 1
. / - + (
)
. RETURN
.
245/322
- ( )grammars
list .
.
. .
( )constracts . list expr expr NUMBER
NUMBER .
.
:yacc
yacc . yacc ( )
yacc .
. yacc :
. ( )syntax
yacc . .
( )action .
. c
c . ( )semantic
.
246/322
.
( )chunks
. NUMBER .
* + . ( )to ken
.
( )routine yacc
4yacc .
. yyparse c .
yacc
c .
. yylex yyparse
( . yacc y
. yacc .
}%
C statements like#include, declarations , etc. This section is optional.
{%
yacc declaration: lexical tokens ,grammar variables,
precedence and associativity information
%%
grammer rules and actions
%%
more C statements )optional(:
{ main )( }. . .; yyparse ) ( ;. . .
} yylex )( }. . .
yacc y.tab.c .
C statements form between%}and%{, if any
C statements from after second %% , if any:
{ ; ( ) Main) ( } ; yyparse
{ } ( ) Yylex
0 yacc C
yacc . 4 ------- yet another comiler . Steve Johnson
yacc ( )1972 .
247/322
.
. yacc .
yacc
. yacc
( . ) .
.
( ) .
( )1
hoc1
main hoc.y ( . yacc y
. cc c yacc
) . hoc.y :
.
. yacc .
I .
.
. c . }and{ .
( $n $1 $2 ) ... n
248/322
$$ .
$ 1 NUMBER
expr . $s :::$ 1 $$ $
.
' '
expr expr . $ 2 , .
.
{ n list
' '
.
. list ()string
. .
yacc .
.
. ( hoc 4 )
.
( )parse tree 1-8
.
249/322
234 : 1-8
( )stck .
. nt/
.
#define YYSTYPE double
( )double .
-
'' ,'
. token %
.
left% right % token %
a-b-c ( a-b(-c
( . )a-)b-c
.
- .
. ( ) .
.
hocy :
250/322
Yyparse,main .
list .
yyparse main list
.
yylex,yyparse yylex .
. blank tab
. \ +,-,* ,/,,(,),n
yyparse .
end file yyparse .
\ + , n . yylval
.
yacc%token NUMBER yacc
y.tab.c . NUMBER
c yacc . ASCII
.
yyerror,yyparse
syntax error .
yacc yyerror . yacc
( )worning .
hoc .
hoc.y .
yacc :
yacc .
hoc 1
34 .
hoc.y . UNARY MINUS token
.
% left + -
%left * /
%left UNARYMINUS / * new*/
expr :
%prec ( ) ( UNARYMINUS) . .
.
)2-8 ( % ) + hoc 1 .
: fr exp 3. 0 .
- make
hoc 1 .
( )shell .
wiJI
. make .
( )consistent
make .
yacc
make .
make
hoc 1 . make hoc 1
. make makefile .
253/322
$ cat makefile
hoc 1 : hoc . 0
cchoc . 00 hoc 1
$
hoc 1 hoco hoco
cc C hoc 1 hoc 1 make .
yacc hoc.y ( hoc . 0 )abject .
)82 :2 ( )errorrecovery
( ) hoc 1 hoc 2 .
26 a z .
.
. hoc 1
.
$ hoc 1
1/ 0
Floating $ exception core dumped
35 yylex
. :
expr: VAR
Var = expr
X=Y=Z=0
254/322
.
-26 .
.
ya.cc
int .
-------- . define #
typedef .
yacc $$=$ 2
. hoc.y hoc . 2
:
. =
.
.
. execerror
.
( floating point excaption )
)averflow(. main .
.
yacc .
. yacc .
.
yyerrork ( )flay
. .
yacc . hoc 2 .
main setjrp
execerror . longjmp ( . 5-7
longjmp,setjmp ) .
execerror ( . abort 3 )
adb sdb .
longjmp .
hoc 2 .
256/322
yylval yylex .
:
)3/1416
( ) NUMBER ( .
hoc 2 :
ppp-II
hoc 2 .
)3-8
.
make p . previons-o
hoc )4-8
. .newline-o
83 :3
hoc 3
. :
257/322
.
.
.
.
.
. hoc 2 x=expr
.
$ hoc 2
x =23.14159
6. 28318
Value printed for assignment to variable
hoc 3
.
$ hoc 3
x =23.14159
x Assignment:novalueisprinted
6. 28318 : Expression
Value ( 250)
is printed
258/322
. 5 :
C make
.
make . . ( VAR
)BLTIN . Var . BLTIN
. hoc.y .symbol.c init.c .
. hoc.h .
( . h ). make
hoc.h .
Install list .
, emalloc malloc ( )<<malloc3
. symbol.c . y.tab.h
yacc.d . define # yacc
... VAR , NUMBER , BLTIN .
init.c ( ).... PI .
260/322
init main .
. .
.
261/322
sqrt,log .
262/322
asgn , expr
VAR expr
() .
.
yacc . 26
. ( header( hoc.c .
( )VAR
( . )BLTIN yylex .
PI VAR .
yyparse .
. VAR
. X
( . )X==1
yylex :
Main init PI
.
263/322
math.c . .
sqrt . math.c 2
UNIX . 7 .
. ( afficial) .
> <math.h <error.h> .
.
264/322
yacc :
$ yacc hoc.y
Conflicts: 1 shift/reduce $
shift /reduce hoc 3 :
X=1
:
265/322
:make
hoc 3 5 make
. vedvce/veduce conflict yacc
.
266/322
$ make clean
yacc ( )0 .
make
. make .
.
lex
lex yacc .
( fragements( c
lex . yacc,lex .
. lex .
. lex 2B UNIX .
lex lex .1 .
yylex .
egrep awk .
( escape,lex - ) c n,t .
. * +
268/322
.
.
lex yylex .
Makefile lex :
make 1 .
( . lex 11
CC .
:
) .
lex .
. . ( Lex
)
C . lex
lex .
hoc . . lex ( )
C .
-9-8 hoc 3 . : size 1. 0
.
84 )4
Hoes hoc 4 .
hoc 3 .
hoes . hoc 4
. .
hoc 4
.
. .
( )push
( .
) .
X=2*y
.
270/322
... 2
y
:
... x
x Pop .
.
.
. ( ) .
.
hoc 4 hoc 3 .
init.c math.c . hoc 3
.
. VAR : Varpush
eval
. * mul
.
271/322
Inst ( int ). .
.
Main . . .
Yyparse .
272/322
.
()
. yylex :
. .
mul
. hoc.h
.
( .
.
) .
273/322
code.c . 150
:
274/322
prog . (
hoc 4 ) .
.
.
275/322
( )PC
. PC .
opcode STOP .
pc,varpush,constpush
.
.
. add .
.
276/322
bltin * pc
d.val
.
assign,eval
. .
( )
. C
.
( )switch
277/322
- make
hoc
. make
( )
.
make .
yacc y.tab.h
: C hoc.y
. make
x.tab.h y.tab.h (
)update . ( pr )
.
( )
.
make hoc 4 :
278/322
hoc.y . y.tab.h
.
)10-8 prag
malloe.o hoc 4
.
hoc 4 )11-8 switch
.
8-5- :5 :
hoc 5
. while,y-else C
{ }and .
)...,=,<,<( . OR,AND ::: SS ( .
C
).
while , up , for
. ( .
wile, up ) .
280/322
shift/reduce hoc 3 .
stop
. pragp WiJI WiJI .
stop .
( subroutine ) end
. subroutine Stop
.
while up .
while whilecode
281/322
While:WHILE
.
. while .
.
while while
( .
).
while . $1
. ] $ 2[ 2 ] , $ 1[ 1
if while . if else,then
if . .
.
282/322
Follow
ungetc .
hoc.h .
hoc 4 . :
C
. ( Ie )
283/322
if,while .
stop
( )
stop
. while
else,then if
( )parent .
if , while
if , while .
. while
. while
pc .
pc+1 pc+2 .
284/322
if . pc then pc+1
else pc+2 pc+3 .
init :
~ 0 C
. tab
hoes .
.
hoes )12-8
.
)13-8 = ... * = + ++
- - : : SS .
285/322
C .
)14-8 for C hoes break .
Continue .
)15-8 ( ) hoes
)16-8 hoes ( )runaway
.
)17-8
. hoes
hoe
: opcode .
8-6- :6
hoc
. ( )
. hoc 6
- . 235
810 hoc
.
. 3
.
.
2 . .
:
286/322
. -
. .
1$ ... .
. .
. ( )global
.
Hoe
.
.
hoes hoc 6 . .
%
:
287/322
288/322
arglist .
expr
.
. defin
yacc .
.
.
).
defonly .
( )
. .
defonly (
) .
C
. lex adhocc .
lex .
C
hoch .
( .
290/322
).
:
hoc.h :
define
291/322
define
( )updata .
( . ).
opcode .
.
.
.
2-8 .
procret funcret .
292/322
)2-8
ret fp
.
293/322
.
. while code ,
if code call . .
294/322
295/322
prexpr,prstr .
:
.
.
hoc 6 )18-8
subroutine $ .
)19-8 .
.
. . wiu
.
)20-8 hoc
)21-8
.
:
.
printf . C
)8-7
Hoc UNIX
WEB .
297/322
PDP 11170 .
ack 3,3 . CaB
. 2432 .
1000 .
.
hoc 6 $ OBJS
CC $ CFLAGS $ OBJS 1 m 0 hoc 6
.
main prof
.
hoc 6
:
.
hoc
.
C Subroutine Cret , CSU
. me aunt CC-P ) .
.
C .
( )popm , push .
299/322
8-8-
.
.
. .
.
.
( )
.
.
lex,yacc,pic,eqn make .
.
,make .
. (
)
.
300/322
.
make .
.
.
UNIX hoc .
Dr , Is . .
.
. grep . diff
. idff .
we . .ep
.
make .
UNIX .
.
Yacc Steve Johuson . yacc
( LALR)1 :
.
yacc .
A.V.Aho,CACM,J.D.U//man,S.C,Johnson 1975 .
.
yacc .
( Addsiam-wesly( J.D.Ullman,A.V.Aho 1997 .
yacc 2 UNIX .
301/322
hoc 2 .
.
Lex mike lesk . lex Ullman , Aho
lex UNIX yacc .
lex C
VV,Ratfor,awk,be,eqn,pic .
Make stu Feldman : MAKE
1979 .
( prentice-Hall( John Bentley,1982
.
.
302/322
UNIX . Belllabs
PDP-11 . .
roff .
nroff joeossanna . nroff
.
1973 nroff
. troff nroff . troff
. troff . nroff
. troff troff .
. .
troff UNIX troff
.
- .
.
.
.
. -
... () .
.
. . ms
mm v . man
.
ms . thEditionman
.i .
in this font .
troff troff .
troff .
. . .
303/322
. troff
. ( )pipes .
UNIX .
troff . (. 8
).
. tbl eqn .
noc noc . 2 .
19 :ms
(.) )
.
. - -
.
.
troff .
.
.
pp
ft B this is a little font paragraph
troff .
. pp ms ft B troff
( )
troff .
FB troff .
pp ms 12
. 2 noc
14 .
ms .
TL
(title of document )oneor mor lines
au
304/322
HOC .
Brian kernighan
Rob pike
HOC : C-
.
HOC : C
.
troff .
. . DE)display
End ( , DC)display start(ms
. noc .
( )293
305/322
noc c .
=- .
. . :
en pr
number
va riable
enpr
enpr binop enpr
unop enpr
functron cagnments
.
DS . . L
C B .
. troff .
.
troff .
ms R . I.
B .
this taxt is roman,but
I
this text is italic
R
This is roman agcin, and
B
this is blod fale
I B . troff
.
I B .
. ( parenthetical. I italic words
parenthetical italice words ()parenthetical i italic word parenthetical italic
words .
306/322
troff .
nroff
.
FS FE .
.
idetifying nerk like an asterisk or a dagger.
fs
dg like this one.
FE
this foot note was creat ea with
IP .
- 2 .
.
IP
first little paragtaph
IP
SECOND PARAGRAPH...
. IP,)LEFT-INSTIFIED PARYRAPH,.PP
IP -()String . .
.
.KS KE .
KF KS . KF
.
ms troff ms .
. 10
1/72 . 12 9 11
vs,ps .
LL PI PD PP
LP .
307/322
:9-1 ms
:MM
mm ms ms
. 9-2 mm ms 9-1
.
troff
ms mm . ( )troff
.
. .
-
. c c cd cd .
troff ascll ( )hyphan .
. . . 9-3
troff
. .
. . troff
.
. ms .
tl
title if document
Au
Author name
AB
.
tl
i title of document
au
.
308/322
9-2
mm
ae AS .
AU
B
DF
DE DS.
eqn EN EQ.
FE FS
I
th-n H
HU
nr pt 1 P
R
mm TL
( ()tb TE TS.
. troff
. .
.
i title /of/docn ment
309/322
9-3
troff
--
\hy
\-
em dash\cem
&\
\blank
\
\e
\)bn
\)bg
\)*a
x*\)f
xx\fxx
n=o\sn n
\s+-n
. .I
. .
. troff .
troff .ms
troff f . \ s
.
\ f f .
a\fbf riv\fiolous\fr\fivar\fbiety\fr
of\fifonta\fp
\fp - - 2
\f)xx xx .
cw keyword .
310/322
\f)cw keyword\fp
.
ms mm back slash .
troff :
the
cw troff
for matter
. \sn
n s8\ . .
. .
\s-25mall caps\so
so .
\fp troff \sp . ms ())uc)upper case .
troff:
troff
br .br .
.
nf . . fi
.ce .
bp . sp . sp
.
. .sp bp .
ta ( )
\
sp r .
311/322
c
ps n \ ft x x.
\ f\s .
:
troff . .
.
troff . . TS.TE
.
.
tbl .
. noc.
( )303
.
-1
( )**FORTRAN
( )
(
)
()center,box
. (all box daublebox )
expand .
.
. .
1 .
cr . t n
s . . cs
313/322
. tbl
cifce cw .
. troff .sp
. - == tbl
tbl .
.
volume 2Aunix .
eqn . eqn
eqn - - .
zeta zeta over
.
.
.
{}and
eqa . .
lambda LAMBDA grad,infinty,int,sum
314/322
over,to,from,sup,sub
.
eqn eqn
.
word . eqn to fro
. .
troff . .
tbl . eqn troff . troff .
)troff-ms filenames )or-mm$
.
eqn filenames :troff -ms
or
tbl file names: eqn:troff-ms
315/322
doctype (. )306
doctype 4 . awk .
.
pp ( )ms .
( -n egree .
) .
. t
roff . .
doctype bundle .
.
troff roff .
.
.
egrep sort awk . .
. egrep sort .
awk .
doctype awk .
.
.
49
usr\man (
\UNIX . \usr\man
. noc .
\usr\man\man1\noc.1
sll name
ed\teat editor
sh synopsis
Bed
I
B\-X
NAMEI
NAME
ed-text editor
synopsis
.
DESCRIPTION .
)cc)1 c . c cc
seealso
)cc)1 . )man)7 .
317/322
DESCRIPIION
I RI
RI I man .
files .
.
BWGS . .
BWGS,DIAGNOSTICS .
. \ )uis\man\man1\hoc\hoc)1 9-1 9-2 .
:hoc
:
[hoc]file
:
noc
c . .
noc .
noc .
.
SEE Also
:noc Rob pike,Briankerrighan
5-9 : .
.
refer .
refer .
.
318/322
pic,deapic .
HOC
Brian kerni ghan
rob pike
HOC : c
.
- 1HOC :
. :
number
vaviable
()eapr
enpr binop enpr
unop enpr
function
.
.
binop .
unop . . not
1 .
319/322
:1
))**FORTRAN ^
!
*\
-+
<<=
>>=
.==!=
( )&&
=
( ) . 2
.
2
( xatan)x
(xcos)x
(xeap)x
(xint)x
( log)x x
(log10)x x 10
(xsin)x
(xsqrt)x
1/0 0.0 c .
HOC 3 .
- 3 :
HOC :
320/322
expr
variable expr
(pyocedur )arglist
while
if)expr(stmt else stmt
stmtlist
print expr list
return optional-expr
nothing
stmlist stnt
.
.
hoc .
.
if .
(if )x<0(print)>( else print)z
(if )x<0
else
(print)z
. if
.
hih cwhile. if
.
3 :
.
.
.
.
.
321/322
hoc .
. .
:
functron func name )(stmt
procedur proce mame)(stmt
.
c
.
.
3$ . . .
.
.
. stack . hoc Ackermann
.
hoc
}()func ack
if )$ 1==0(return$s
if)$2=0( return ack
(return ack)$1-1,ack)$1,$2-1
(ack)3,2
29
ack)3.3
61
(ack)3,4
hoc:sta ek too deep hear line 8
.....
n!2n
322/322
hoc
func stirl )(}
return sqrt)2*$1*pi(
{
stirl)20(
2.4328818e+18
!n :
i=9
while)li=i+1(<=20(}
print,fac)i(stirl)i(in
{
10 1.0000318
11 1.0000205
12 1.0000224
13 1.0000166
14 1.0000146
16 1.0000128
17 1.0000114
18 1.0000102
19 1.000092
20 1.000083