Вы находитесь на странице: 1из 880

Practical Guide to Linux Commands,

Editors, and Shell Programming

SECOND EDITION

Mark G. Sobell

..

PRENTICE
HALL

Upper Saddle River, NJ Boston Indianapolis San Francisco


New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City

2-
.

-
--

2011

32.973.2-018.2
004.451
54

54

.
Linux. . 2- . .:
, 2011. 880 .: .
ISBN 978-5-459-00450-2
, , UNIX/Linux,
Ubuntu, Fedora, openSUSE, Red Hat, Debian, Mandriva, Mint Mac OS X.
Linux: , , Linux, , , .
Linux-,
Linux: vim GNU-
emacs. bash tcsh.
,
Linux Mac OS: bash; Linux- Perl; sed
rsync. Linux,
.
: Linux Mac OS X,
,
, .

32.973.2-018.2
004.451

Prentice Hall, Inc. Upper Sadle River, New Jersey 07458.


.
.
, , ,
. , ,

, .

ISBN 978-0131367364 .
ISBN 978-5-459-00450-2

Prentice Hall, Inc. 2010


, 2011
, , 2011


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
I. Linux Mac OSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
II. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
III. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
IV. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
V. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13
14
14
16
16
17
18
19

1. Linux Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
UNIX GNULinux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Linux? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21
25
31
37
39
40

I. LINUX OS X
2. ! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
su/sudo: (root-) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42
45
46
50
52
60
63
64

3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
| (): . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

65
66
69
76
77
80
85
87
91
92
93
95

6
4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
(ACL-) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

97
103
106
114
121
126
132
134

5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

137
143
154
157
161
162
164

II.
6. vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. vim . . . . . . . . . . . . .
vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
, . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

168
169
177
184
188
189
193
199
200
202
203
208
211
214
218

7. emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

220
223
230
231
239
241
256
270
271
278


7
III.
8. Bourne Again Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bash: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

283
284
304
320
321
324
341
344
347
351
362
364

9. TC Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TC Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
, Bourne Again Shell TC Shell . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

368
369
371
377
378
384
397
406
411
412
413

IV.
10. Bourne Again Shell . . . . . . . . . . . . . . . . 416
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

417
449
452
465
480
488
499
501
502

11. Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504


Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

504
512
520
530
534
535
538

8
CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

544
546
550
550
551

12. AWK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

552
553
553
554
554
562
583
584
584

13. sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

585
585
585
586
590
600
601

14. rsync . . . . . . . . . . . . . . . . . . . . . . . 602


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

602
602
603
605
613
613

V.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
aspell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
at . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
bzip2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chgrp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chmod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
chown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

616
619
620
622
626
630
632
633
635
636
640
645
647


9
comm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
configure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cpio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
crontab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
df . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
diskutil (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ditto (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dmesg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dscl (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
du . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
finger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fmt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
fsck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ftp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GetFileInfo (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
kill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
killall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
launchctl (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ln . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
lpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
man . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mkdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mkfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mtools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
mv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nohup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
od . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
open (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
otool (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
plutil (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

649
651
653
657
662
665
667
669
672
674
680
683
685
686
689
691
694
697
699
706
708
710
715
722
723
727
729
734
737
738
740
741
744
748
750
753
761
767
771
772
775
778
780
782
782
786
788
790
792
798
799

10
ps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rcp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
renice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rlogin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rmdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rsh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
rsync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
scp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SetFile (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sleep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ssh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
stat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
stty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sysctl (OS X) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
telnet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
top . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tune2fs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
umask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
uniq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
w . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
wc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
which . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
who . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

801
805
807
809
810
811
812
814
814
816
817
818
820
829
830
838
839
840
845
846
849
850
852
855
859
861
864
864
867
868
870
872
873
875
877


Linux.
,
Linux. , .
.
Linux,
.
Linux

Mac OS X,
UNIX/Linux.
,
(GUI), Macintosh,
, (command-line
interface, CLI). OS X, . , Linux,
Mac OS X.
.
Mac OS X

(CLI),
, Linux
. ,
. (, emacs) , , ASCII-.
.
Linux ,
, Linux
. Linux,
, Linux
.
, Linux-,
.

Linux , .
Ubuntu,
Fedora, Red Hat, Mint, OpenSUSE, Mandriva, CentOS Debian.
, Linux, . , , ,
.

Linux

12

OS

X
( ). Mac
V
, Linux, ,
Mac OS X.
Perl (11).
rsync (14).
V , , Mac OS X.
A Practical Guide to Red Hat
Linux: Fedora and Red Hat Enterprise Linux A Practical Guide
to Ubuntu Linux,
.
, ( 3 ,
V), , Bourne Again Shell (bash) Perl.
, , ,
, vim emacs, TC Shell (tcsh), AWK
sed, rsync, V, 97
Linux Mac OS X.

.
,
. :
, , Linux
Mac OS X;
, Linux
Mac OS X ;
, Linux Mac OS X ;
Macintosh, , UNIX/Linux
,
;
Macintosh, ,
UNIX/Linux, Mac OS X;
UNIX, UNIX
Linux Mac OS X;
, Linux
Mac OS X , bash
Perl;
, ,
Linux Mac OS X;
, Linux Mac
OS X;
, Linux Mac OS X.

13

Linux. Linux Mac OS X


. ,
, .
, , Linux/OS X, .
Macintosh
.
- Macintosh. UNIX/
Linux , Mac OS X, Macintosh
,
Linux UNIX- .

, LINUX,
LINUX, MAC OS X
UNIX Linux, Mac OS X.
(GUI)
, (CLI)
.
Linux, Mac OS X. , Linux
Linux, Mac OS X. ,
, .



. , , ,
Linux.
Linux :
,
. V,
.
Linux.
-, : ,
.
Linux.
:
, ,
.
, , , ,
.
, ,
,
, - .

14
, -

.
- www.sobell.com, , , ,
- .
,
.
URL (-), , .
,
.
,
. -
www.sobell.com.
,
GNUs, gcc, GNU, make, gzip,
.
, - ,
.
,
Apple Mac OS X, diskutil, ditto,
dscl, GetFileInfo, launchctl, otool, plutil SetFile.

, , ,
Linux .
.
q 1, Linux Mac OS X.
Linux OS X.

Linux
,
OS
X

Mach

, , GNU Linux ,
Linux, .

I. Linux Mac OSX

,
I
UNIX/Linux , ,
.
, . 42,
,
. 52, ,
Linux, .

I. Linux Mac OSX 15


I

Linux
.
2, !.
, .
, ,
, Linux
.
3, .

30, .
, Linux, ,
.
:
yy grep, ;
yy unix2dos, Linux
Windows;
yy tar, , ;
yy bzip2 gzip, , ,
;
yy diff, .
4, .
Linux, ,
, , , ,
. , .

, .
5, .
, , . , , bash,
tcsh. :
yy
;
yy
, , ;
yy ;
yy ,
;
yy , , Linux ;
yy ,
.

16

II.

Linux
-, . vim,
vi, GNU- emacs
Linux.
, ,
, .
Linux

, , Linux .
6, vim.
vim, ,
,
, , ,
. .
7, emacs.
, emacs, ,
META, ALT ESCAPE. ,
, , , .
, ,
. ,
, emacs.
,
, emacs
C, C-. 7
emacs.

III.
III bash
TC Shell (tcsh).
8, Bourne Again Shell.
,
5, .
Bourne Again Shell bash, ,
. 8
:
yy , ;
yy
;

IV. 17

yy ,
;
yy ;
yy , ,
, ;
yy , ,
;
yy ;
yy , ,
.
9, TC Shell.
tcsh, bash tcsh
, , .
:
yy tcsh , tcsh;
yy , ,
;
yy
;
yy tcsh;
yy .

IV.
IV ,
Linux Mac OS,
.
10, Bourne Again Shell.
,

8, bash, .
:
yy , ifthenelse case;
yy , ;
yy () ;
yy , , exec,
trap getopts.
Linux
,
.
10 , , .

18

.
quiz, , ,
,
. (
V
, .)
11, Perl.
Perl

, . :
yy Perl, perldoc;
yy Perl ;
yy ;
yy ;
yy CPAN-.
Perl Linux-.
11 ,
. Perl.
12, AWK.
, ,
AWK, ,
. , , ,
, .
13, sed.
sed, ,

. sed
, .
14, rsync.
rsync, ,

, . , .

V.
Linux . 1214, V
, , C. ,
UNIX/Linux,
.
.

19

, 1214, V,
, , Linux (man-), .
, (, ls cp), ,
(sort, paste test),
Linux (ps, kill fsck) (ssh, scp ftp).
,
Mac OS X, ,
Linux.
, , .
, , ,
. , , AWK (
20 12), ftp ( V) sort
( V).


(www.sobell.com)
, ,
, Linux OS X,
, , ,
, .
,
. - , (mgs@sobell.com), . (www.sobell.com)
, .
-,
Linux.
(Mark G. Sobell)
-,

1. Linux
Mac OS X

, ,
, , , ,
. :
. , , ,
, . , , (),
( ), , ,
.
, - . ,
GNU, .
Linux
(Linus Torvalds), ,
. Linux
0.01 1991 .
.
, , BSD UNIX System V UNIX
(SVR4). Linux , Linus UNIX.
Linux,
, ( ). ,
. ,
. - ,
, , ,
( ,
-). GNU (www.
gnu.org/philosophy/free-sw.html) :
Linux


, . ,
, ( ) .

OS X Mach,
- (Carnegie Mellon University, CMU),
. CMU
1994, . Much
Mach

UNIX GNULinux 21

Mac OS X :
Mac OS X Mach FreeBSD, BSD
GNU, BSD,
Apple .

LINUX, OS X UNIX
Linux OS X UNIX.
Linux OS X. , Linux, OSX
Linux, , OS X Linux, .
Linux


, Linux, OS X.

UNIX GNULinux
, UNIX Linux,
GNU Linux. www.levenez.com/unix , UNIX.

UNIX Linux

UNIX
, , .
, , ,
.
UNIX

. 1975 UNIX
, Bell Labs
. ,
, ,
. UNIX
, .
,
.
,
UNIX
.
UNIX, (Computer Sys
tems Research Group, CSRG)
.
, UNIX,
Berkeley Software Distribution (BSD) UNIX system ( Berkeley
UNIX). UNIX System V (SVR4), , AT&T UNIX
BSD
(Berkeley)
UNIX

22 1. Linux Mac OS X
System Laboratories. Mac OS X
BSD .

1983
(Richard Stallman, www.stallman.org) 1
GNU
, , ,
, GNU Manifesto2,
:
GNU, Gnu UNIX (Gnus Not UNIX),
UNIX- , ,
, .
, ,
, :
.
, GNU.
, , GNU
. ,

.
, free ,
. ,

free
, . , ,
,
. , , .

:
GNU.
, ,
, , .
,
, .
.

. , ,
: ,
.
www.gnu.org/gnu/initial-announcement.html
www.gnu.org/gnu/manifesto.html

1
2

UNIX GNULinux 23

, , ,
. , MIT
( ) -
GNU.

, 1991
GNU .
GNU, .
:
90- (
GNU Hurd1, Mach2).
, ,
3.
,
, ,
UNIX-
.
, , GNU.
4 , ,
GNU 1984. UNIX GNU
GNU. GNU
GNU. Linux [GNU]
5.
GNU
FreeBSD (www.freebsd.org) NetBSD (www.netbsd.org),

Linux
, Hurd Darwin (developer.apple.com/opensource), .


, UNIX
,
. , UNIX

,
, . , www.gnu.org/software/hurd/hurd.html
www.gnu.org/software/hurd/gnumach.html
3
www.gnu.org/software/hurd/hurd-and-linux.html
4
www.gnu.org/philosophy/free-sw.html
5
www.gnu.org/gnu/linux-and-gnu.html
1
2

24 1. Linux Mac OS X
UNIX, .
UNIX ,
,
.
,
UNIX- : (Doug Comer) XINU,
(Andrew Tanenbaum) MINIX.
Linux, MINIX.
,
(
MINIX), , .
Linux .
Linux .
GNU,
. Free Software Foundation (www.fsf.org),
(GNU), , ,
Linux ( ,
Ubuntu, Red Hat, openSUSE), ,
.
MINIX

Linux GNU
GNU General Public License (GPL, www.gnu.org/
licenses/licenses.html). GPL , , .

, . , ,
GPL,

, GPL-.
copyright ( , ), copyleft. (
GPL, ,
.
GPL, .)
GPL

!
Linux ! (Have Fun!).
. UNIX, Linux
, . ,
(less) (more): GNU
, UNIX more,
less. PostScript ghostscript (
), vi elvis. -

Linux? 25

, , Intel, Intel Inside ( ), Linux-


Linux Inside. Linus Inside.

Linux?
Linux
UNIX- . UNIX. UNIX,
Linux UNIX : Linux
BSD System V, Linux
, Linux
UNIX ,
. , , GNU
Linux.
1985 POSIX (Portable Operating System Interface
for Computer Environments) , UNIX System V
Interface Definition (SVID) .
, ,
. 1988, POSIX
IEEE-,
API (application program interface), . UNIX-
, . , , , , UNIX, Linux
.

Linux , ,
, , , , , -
.
Linux , Linux,
GNU, KDE ,
Linux. , , Linux,
IBM (www.ibm.com/linux). Linux POSIX,
. , Linux

26 1. Linux Mac OS X
.
Linux

, .
Linux
. ,
, , ,
.

( ), , .
. Netscape, , Linux
Java , .
, , ,
Mozilla-Thunderbird-Firefox.

Linux Intel
( Apple): Power PC, Apple
(ppclinux), Compaq ( Digital Equipment Corpora
tion) Alpha, MIPS-,
Motorola 68K, 64-
IBM S/390. Linux :
2.0, (SMP).
O(1), SMP.

Linux ,
, . , Linux , DOS, Windows Macintosh. , Wine (www.winehq.com) Windows API,
X Window System UNIX/Linux.

(VM, )
. VM, (, ).
, ,
(VMM) .
, ,
. ,

Linux? 27

, Windows, Ubuntu 7.10, Ubuntu 9.04 Fedora 10.



.
( )
.
, :

, :
.
,

, .
, ,
.

, .
,

()
, ,
.
.
, ,
, .

. .
-

,
()
.
,

(), ,
.
-

,
,
.

28 1. Linux Mac OS X
Xen, ,
(VMM), .
Xen
.
Xen.
Xen

- www.cl.cam.ac.uk/research/srg/netos/xen, - wiki.
xensource.com/xenwiki.
Xen

VMware, Inc. (www.vmware.com) VMware Server,


, ,
Linux . VMware Server
,
, Windows Linux. VMware VMware player,
VMware Server.
VMware

, (Kernel-based
Virtual Machine, KVM; kvm.qumranet.com libvirt.org),
, Linux.
Intel VT (VMX)
AMD SVM.
KVM

Qemu (bellard.org/qemu),
(Fabrice Bellard), VMM ,
, - . ,
, , .
Qemu

VirtualBox (www.virtualbox.org) , Sun Microsystems.


VirtualBox

Linux ,
,
UNIX

Linux

. -,
,
. 70- -
, .
64- , , ,
.

Linux? 29

-, , ,
, (, DEC/Compaq
VMS).
,
.

(UNIX,
OS X, Windows) (Linux) . Linux , . ,

( ,
Microsoft Windows). ,
.

. , .
UNIX

, -
,
,

. Linux , :

.

Linux
,
. 95% Linux
C, ,
- . ( C C.)
Linux ,
() . , Linux , , , ,
.
.
, Linux
.

30 1. Linux Mac OS X

.


Linux
, . Linux
, ;
.
Linux
,
.
: ,
, , ,
.
.
,
.

C
(Ken Thompson) UNIX 1969
PDP

-7. -: , , ,
, .
UNIX (
).
UNIX , B, - ,
BCPL. (Dennis Ritchie) C, B, 1973 UNIX C. C
. .
C.
, .
C - .

, C

. .
C
( , ,
).
C .
. ,

Linux 31

, . C
, .
, .
80- (American
National Standards Institute, ANSI) C,
ANSI C C89 ( ).
C99, - GNU
( gcc). Kernighan
& Ritchie ( K&R) C, ( ),
C.

Linux

. 1.1. Linux

, Bell Labs, (Bjarne Strou


strup), - C++,

C
. - , C++
, C.
Objective-C,
-. -, GNU,
C, C++ Objective-C.

Linux
Linux .
,
. UNIX,
(. . 1.1) ,

.

32 1. Linux Mac OS X

Linux ,

Linux ,

, , , ,
, DVD -,
. , .
. , write() ,
.
write() . ,
.
(
).

Linux

Linux
, .
,
, ,
. , , .
,
, , .
.
, ,
, , .

Linux
Linux ,
.
, , , . ,

e 13

Linux 33

, ,
. X Window System
.
.

Linux

,
, , , .
, . Linux , ,
. .
, . .,
.
, .
, (.1.2).

,
, Linux Filesystem Standard
Wednesday, September 30, 2009 10:13 AM
(FSSTND),
Linux Filesystem Hierarchy Standard (FHS).
Linux .
, Linux,
.
Overview of Linux 13

home

tmp

etc

max

sam

hls

bin

report

notes

log

Figure.
1-2 1.2.
The
Linux filesystem
structureLinux


Links

A link allows a given file to be accessed by means of two or more names. The alternative names can be located in the same directory as the original file or in another
directory. Links can make the same file appear in several users directories, enabling
those users to share the file easily. Windows uses the term shortcut in place of link

34 1. Linux Mac OS X
. .
, . Windows
(shortcut).
Macintosh (alias). Linux
; , .

,
Linux .
, .
,
, ,
. Linux Access Control Lists (ACL), .

:


. ,
. Linux
. :
Bourne Again Shell (bash), Bourne Shell ( UNIX).
Debian Almquist Shell (dash), bash,
. dash bash.
TC Shell (tcsh), C Shell, , BSD UNIX.
Z Shell (zsh), ,
Korn Shell.
,

. Linux: ,
.
, , ,
.
,
( Linux ; Windows

Linux 35

).
,
, , , , .


, , , ,
. . , ,
:
,
.
. , mak* make-3.80.tar.gz.
,
.


Readline
, , , :
ESCAPE, ,
, ,
.


(, )
Linux- .
Linux
. .

,
, , , .
,
.
, cat . cat
, .


. , ,
.
.

36 1. Linux Mac OS X
, Bourne

Again

Shell

, , , . ,
.


,

.
. ,

, . ,
, , ,
. (
BSD UNIX, C Shell.)


Linux , , . ,
, , .
sort, , ( )
,
, , , , , ,
.. sort ,
Linux-. ,
, , , , ,
, , .
man ( manual ) info
Linux.


Linux
. .
, ,
. ,
. .

. ,
sort head (,
);
lpr, .

Linux 37

,
.


Linux
. .
,
.

( ), , ,
, , .

,
,
.

Linux
Linux BSD,
SystemV Solaris ( Sun Microsystems),
. , UNIX,
Linux,
.
, Linux.

GUI
X Window System ( X X11) (Massachusetts Institute of Technology, MIT)
Linux.
, X,
, ,
, ,
. X ,
,
, .
X11

Mac OS X , . Macintosh Aqua, Mac OS X. Aqua Aqua

38 1. Linux Mac OS X
Quartz
. Mac OS X X11,
Quartz.
X
: (desktop manager) (window manager). , ,
. Linux GNOME (www.gnome.org), KDE (www.kde.org) . Mac OS X Aqua, X11,
X11 .

,

, , . . Microsoft Windows
, X :
( ,
), ,
.
X Linux . Linux Metacity ( GNOME) kwin ( KDE).
, Sawfish WindowMaker.
Mac OS X
Quartz, Apple Aqua.
X11 quartz-wm,
Apple Aqua, X11
Mac ,
Mac OS X.


Linux ,
.
, ,
, , , .
, , ,
(local area network, LAN) (wide area network, WAN),
.

39

,
,
. , ,
.


Linux
. Linux
. C C++, ,
Linux, Ada, Fortran, Java, Lisp, Pascal, Perl Python. bison
(),
, ( ,
,
). flex (,
). make, ,
GNU (GNU Configure and Build System), .
(Source code management systems), CVS, . ,
ups gdb, . GNU
C- (gcc) - gprof,

. C
C-,
, .
Apple Xcode, OS X,
, .

Linux UNIX ( Windows),


( ).
UNIX Linux .
Linux UNIX-, , , GNU. Linux .

, ,
Linux .
,
Linux, , -

40 1. Linux Mac OS X
Linux , ,
,
, , ,
.

1. (free software)? .
2. Linux?
?
3. ? ?
4. (Free Software Foundation)
GNU? Linux? Linux ?
?
5. Linux? Linux?
6. ()?
7. ? ? ?
8. ?
9. Linux ?
10. ?
11. .
12. , Linux?
?
13. GNU General Public License.

I

Linux OS X

2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4. . . . . . . . . . . . . . . . . . . . . 97
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

2. !
, , , , ,

Linux
. , (GUI) .
,
(
command
-
line

interface
,
CLI
).
GUI
, , , ,
xterm, Konsole, GNOME Terminal, Terminal ( Mac OS X),
(. . 61).
,
, , .
, . ,
(root privileges),
, ,
, .
Linux.
, .
,
(root-).
,
: , , ,
, .

,
,
. .
Mac OS X Mac OS X, Mac OS X Server.
.
Mac OS X

Mac OS X 10.5 (Leopard).


, OS X
, .
- (www.sobell.com).

Mac OS X

, :

$ cat practice
,
.

, 43

, ,
.
($)
, Linux, , , .

Arial. emacs ls ls
( ls), ls a,
. ,
, ,
.

. , memo5,
letter.1283 reports.
, . Linux ext2, ext3
ext4, , memo5, MEMO5
Memo5 .

HFS
+,
Mac
OS

, , OS X memo5, MEMO5 Memo5


. . 100.

.
, . , passwd: Sorry,
passwords do not match1.


:
, 2, ESCAPE TAB.
, , ,
.
, CONTROL,
CONTROL+D. (, D , ,
SHIFT; CONTROL+D
CONTROL d.)

(
). . .
2

.
. RET, NEWLINE, ENTER,
RETURN .. ,
, .
1

44 2. !

, Linux .
($), (#), (%).
, .
.
, .
, . :

$ vim memo.1204

vim, vim memo.1204 .


( vim ESCAPE ZZ; vim
6.)
, .
, FOLDOC,
(Denis Howe), Free On-Line Dictionary of Computing
(foldoc.org), . ,
, , .

, , .
, , . ,
, , .
.

-, URL-, http://,
: ftp:// https://. ,
, , http://,
FTP- HTTP-
. , URL- ,
.
URL-
(-)

,
Linux-.

- .
.
, .

45



,
, , , (issue) (
/etc/issue). Linux,
, , .
:
Ubuntu 9.10 tiny tty1

. .
Max, tiny:
tiny login: max
Password:
Last login: Wed Mar 10 19:50:38 from dog
[max@tiny max]$

login:,
,
. login: ,
CONTROL+Q (
Xon).

, ?
, ,

: . ,
, - ,
. , , ,
,
.
, ,
.

Apple, PC, Linux-


, , ssh,
telnet , ,
IP- , .
, , . ,
, . , Linux
. Mac OS X
.

46 2. !

TELNET
telnet ,
,

. ssh , ,
, , telnet. ssh
, Linux.
ssh .

, ssh Linux:
$ ssh max@tiny
max@tiny's password:
Permission denied, please try again.
, ,
max@tiny's password:
Last login: Wed Mar 10 21:21:49 2005 from dog
[max@tiny max]$


Max
, , .
, ,

, , , at (@)
. Max ssh tiny.
( ), ,
.
, , motd,
/etc/motd.
($). , , ,
, ,
. $ ( ) (max), at (@), (tiny)
, Max.
8, bash, 9, tcsh.

TERM
TERM
. TERM
, .
, .


(GUI) UNIX,
Linux

( -

47

command-line interface, CLI).


, ,

ssh telnet.
()
. ,
. .
.
,
. -
, - ,
, .
, GUI - . , . , GUI ,
. ,
, , .
, , GUI. ,
, (. chmod
V).

,
, , , , , . , ,
.
, ,
dpkg-reconfigure, .
, ,
, , .

. :
5,
Bourne Again Shell, ,
TC Shell, 8, 9 TC Shell, 10
( )
Bourne Again Shell.

48 2. !

?
Bourne Again Shell (bash) TC Shell (tcsh).
, bash, , ,
tcsh - , Z Shell (zsh).
ps.
ps .
$ ps
PID TTY
2402 pts/5
7174 pts/5

TIME CMD
00:00:00 bash
00:00:00 ps

,
: bash ps. tcsh, bash ps
tcsh. , ps
.


,
, .
,
, .
: ,
. :
, ,
.


,
, .

, .
BACKSPACE.
, DELETE,
CONTROL+H. ,
stty1 (.
. 49):
$ stty ek

stty V.

stty set teletypewriter,


,
UNIX. stty .

49


, CONTROL+W.
,
. CONTROL+W,
(
) (
).

CONTROL+Z
,
( CONTROL+Z) ,
. , Stopped
(), ,
.
fg, ,
.
155.


,
.
, , .

CONTROL+U. , CONTROL+X.
, stty,
.48.


.
, , ,

, .
(CONTROL+C , , DELETE DEL). - ,
. 841 V.
,
( CONTROL+Z),
jobs, ,
kill, .
, (
[1]).
kill TERM, ,
(%1). TERM , kill .
$ bigjob
CONTROL+Z
[1]+ Stopped

bigjob

50 2. !
$ jobs
[1]+ Stopped
$ kill -TERM %1
$
[1]+ Killed

bigjob
bigjob

kill . ,
.
, .
, , ,
(KILL). ,
.
. 154 kill . 738.
Linux
.
, .

( ),
,
, .
Bourne Again Shell ,
. ,
, . ,
.
, ; . ,
.


, bash tcsh,
. . ,
, .
, .
.
.
.
bash 8 tcsh 9.

su/sudo:
(root-)
UNIX Linux root.
root ( root-),

su/sudo: (root-) 51

, .
, root-,
, . root-
, , .
root-,
. -, , ,
.
ROOT-


root
-, .
root-,
.

.
root- ,
Linux.


root
- . root.
root-, . , ,
su (substitute user ),
, root-, root-,
.
root
su root- ,

root
. , su .
$ ls -l /lost+found
ls: cannot open directory /lost+found: Permission denied
/lost+found:
$ su -c 'ls -l /lost+found'
Password:
root
total 0
$

, , root-, /lost+found: ls . su
c (command ) root-.
, . (ls , ), root-.
su root-.
root-
(#).
exit.
$ su
Password:
# ls -l /lost+found

root

52 2. !
total 0
# exit
exit
$

(, Ubuntu)
root

(
root
) sudo (www.sudo.ws),
root-. sudo
root- ( root). ,
root- /lost+found.
$ sudo ls -l /lost+found
[sudo] password for sam:
total 0
$

i sudo
root

-.
root
- (#).
exit.
$ sudo -i
[sudo] password for sam:
# ls -l /lost+found
total 0
# exit
logout
$


Linux .
Linux.
man (manual ) info, man info. ,
Linux,
. ,
Linux.

help
GNU- help,
. , GNU,
h help.
$ cat --help
Usage: cat [OPTION] [FILE]
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonblank output lines

53
-e
-E, --show-ends

equivalent to -vE
display $ at end of each line

, help, ,
less, :
$ ls --help | less

man:
man (. V) man-
. , ,
, . man- ,
Linux. , man-,
apropos. ,
, .

. 2.1. man,

- MAN
http://tldp.org/manpages/man.html
man-. , HTML-,
man-.

, man,
. .2.1 man,
; man man.

54 2. !
man
,
less, .
less ,
, , Manual page man(1) line 1,
, , .
h (help ), less. q
(quit ), .
. 66.

less

FHS (Filesystem Hierarchy Standard ), Linux


man- , :
1. User Commands ( ).
2. System Calls ( ).
3. Subroutines ().
4. Devices ().
5. File Formats ( ).
6. Games ().
7. Miscellaneous ().
8. System Administration ( ).
9. Kernel ().
10. New ().
UNIX.
, man , .
1, 6 7;
.

. , man-
passwd 1 :

$ man passwd

man- passwd 5,
:
$ man 5 passwd

man man-
5. , man-
passwd(5). man- ,
a (adjacent ),
, q . ,
, passwd,
man a passwd.

55


.
, . ,
, .
. . 138.

apropos:
, ,
apropos .
( ) man- ,
. man k
(keyword ) , apropos.
, apropos, whatis,

Linux
- , cron ( cron V , crontab).
, apropos
who. , , ,
man-. (who)
,
:
$ apropos who
at.allow (5)
at.deny (5)
from (1)
w (1)
w.procps (1)
who (1)
whoami (1)

determine who can submit jobs via at or


determine who can submit jobs via at or
print names of those who have sent mail
Show who is logged on and what they are
Show who is logged on and what they are
show who is logged on
print effective userid

batch
batch
doing.
doing.

. 2.2. , info coreutils

56 2. !

whatis
whatis apropos, ,
:
$ whatis who
who

(1)

- show who is logged on

info:
info, , , , GNU Linux. info Linux, , GNU.
info www.gnu.org/software/texinfo/manual/info. .2.2
, info info coreutils (coreutils
, Linux).

MAN INFO
info GNU-
, man. man-
, info, man-
info. man ,
, GNU. info ,
GNU, man-.

,
, .
info-
:
? info;
, ;
m , ;
m , ;
q CONTROL+C info.
info, ,
. C-h , CONTROL+H.
, M-x META ALT
x. , META+X,
ESCAPE, x. : . 231.

, INFO PINFO
pinfo info, ,
emacs. , info, . , pinfo
.
pinfo .

57

. 2.3. info coreutils, /sleep

info coreutils /sleep,


sleep. / ,
Search for string []:
, .
, .
sleep ,
sleep.
sleep (.2.3) / ( /sleep). , .
, .
info-,
.
, , ,
. menu
. ,
sleep, m sleep, .
m ( menu),
( , /)
Menu item:. sleep ,
.
.2.4 sleep.
, .
n,
p. ( sleep .)
man
info . -

58 2. !
PostScript-,
, man t. , man
t cat | lpr cat. , - www.tldp.org,
- .

. 2.4. info-, sleep

HOWTO: ,
HOWTO , -,

Linux,
.
Mini-HOWTO.
Linux

, , HOWTO, .
HOWTO mini-HOWTO -
Linux (LinuxDocumentation Project, LDP).
- www.tldp.org, HOWTOs,
HOWTO mini-HOWTO.
HOWTO ,
LDP .


,
Linux.

59

. 2.5. Google

,
/usr/share/doc /usr/src/linux/Documentation ( )
,
, man info.
,
, .
, .
, grep info.
, ,
zcat (. V),
less.
/usr/share/doc

$ less /usr/share/doc/grep/README
$ zcat /usr/share/doc/info/README.gz | less


, Linux.
.
, ,
, Google ( www.google.com
Linux www.google.com/linux).
,
. , ,
(.2.5).

60 2. !
GNU www.gnu.org/manual.
GNU (www.gnu.org) GNU-. GNU- .
GNU

. 2.6. Linux Documentation Project

Linux

Linux (Linux Documentation Project, www.


tldp.org, .2.6), -

Linux, ,
HOWTO, , man-
, Linux. , , , , .
. ,
, Linux ( Links
Search www.tldp.org/links).
, ,
, , ,
.


,
.

61


Linux
, . , 63 (
).
, .
,
CONTROL ALT, ,
, . , CONTROL+ALT+F5
.
,
CONTROL+ALT+F1, .
,
.
, ,
. ,
,
( ).

,
, ,
, , . , ,
.

,
.
:
. ,
,
, .
.
, . ,
, .
.
, .
.


CONTROL+D.

62 2. !

EOF
(
end
of

file

).

exit. ;
, . ,
, GNOME, GNOME.


- , ,
.
.

:
, , ,
(
). ,
.
,
, ,
, ( ) ..
,
, : ,
, ,
.

. , ;
. ,
-
: ,
-.

, passwd. passwd
() . , ,
- , .
.
PWGEN
pwgen (, , )
. , , ,
.

,
, .
, .
, , , :
Sorry, passwords do not match

, :
You must choose a longer password

63

, :
Bad: new password is too simple

, .
, , passwd.
.
, -, root-,
passwd, .
OS X passwd
,
Keychain
. Keychain

. ,
Keychain Access.

, ,
. :
( ).
- .
, , .
, ,
.
.
, .
. (, CONTROL+H),
.
.
.


Linux
, , . login:
, .
passwd.
, , ,
.
. .
- , .
-

64 2. !
. , root-, root su sudo.
root- .
root-, .
-, root-, .
, .
man
. ,
Linux,
,
. info ,
Linux.

1. ,
:
Login incorrect

, , , ,
. ?
2. .
? .
, .
3. fido ? .
4. , ?
5. dog. ?
. ?


1. tcsh, root-.
2. man- Devices () ?
(: Devices Special
Files.)
3. apropos: , man-
passwd 1 5 . , man, ,
.
4. , Linux ?

3.
Linux,
(GUI):
, (command-line interface, CLI).
. Linux , ,
, . ,
GUI-. GUI
, ,
.
(. 5, 8 10). ,
, .
: ls, cat, rm, less hostname. ,
, , , , ,
, ,
, .


, .
. 157.
,
, , . , ( emacs
), :
& ; | * ? ' " ' [ ] ( ) $ < > { } # / \ ! ~

, , TAB,
, .
, ,
. , .

, ,
,
( ). -

66 3.
. . (/) , .
, (\).
, (, **
\*\*).
, (\\).


: '**'. ,
: ' : >'.
, .
(CONTROL+H), (CONTROL+U) (
CONTROL+M) ,
CONTROL+V.
. :

$ echo 'xxxxxxCONTROL+U'
$ echo xxxxxxCONTROL+V CONTROL+U
, CONTROL+U, ,
, ,
. echo
od (octal display ), ,
CONTROL+U 25 (025):
$ echo xxxxxxCONTROL+V CONTROL+U | od c
0000000 x x x x x x 025 \n
0000010
\n , echo
.


Linux ,
, .
, , , , .
,
.
,
.

67


, ,
. , ,
GUI, .

,
, .
, Windows Mac OS X,
Linux GUI, . :
, , ( ).


.
: , ,
. 4.

ls:
, practice.
( vim 6,
emacs 7.) ,
, ls (list
). , 3.1, ls practice.
( ,
.) , 3.1, . .
V info coreutils
'ls invocation'.
3.1. ls, cat rm practice

$ ls
practice
$ cat practice
,

$ rm practice
$ cat practice
cat: practice: No such file or directiory

cat:
cat .
catenate, . ( 5, 5.4 cat .)

68 3.
cat,
. 3.1 cat,
practice. ls
cat: ls , cat .
V
info coreutils 'cat invocation'.

rm:
rm (remove ) . 3.1 rm,
practice. rm , ls cat
, practice . ( ls
, cat , .) rm
.
V info coreutils 'rm invocation'.


, ( ),
, rm.
rm i , rm , ,
, y (yes ).
, y, .
$ rm -i toollist
rm: remove regular file 'toollist'? y
. rm

i
(. 8,
) (. .103),
rm .

less more:


, , less, more.
, . , .
less more . ,
less END ,
, , q.
h ,
.
less practice more practice cat,

3.1, , . , less/usr/share/dict/words.
less more V
man-.

69

hostname:
hostname , . , , .

man-.
$ hostname
dog


, , , , , , .


( )
TAB

, Bourne

Again

Shell,
,
, .
, .
,
.
, , ,
. TAB ,
TAB (. , Bourne Again
Shell 8) CONTROL+D (. ,
TC Shell 9), .

cp:
cp (copy ) (3.2) .
, ( ). cp
.
, cp, ,
-:
cp _ _

_ , cp, _ , cp () .
3.2. cp

$ ls
memo
$ cp memo memo.copy
$ ls
memo memo.copy

cp,

3.2, memo memo.copy. ,


. ls , memo -

70 3.
. cp ls
: memo memo.copy.
.
30 (0130):
$ cp memo memo.0130

Linux ,
, . ,
.
. 99.
cp V info, info coreutils 'cp invocation'. ,
scp, ftp rsync, V.
CP

- , cp

. cp
- ( ) ,
, cp . cp
i (interactive )
- .

, orange.2 cp.
, y:
$ cp i orange orange.2
cp: overwrite 'orange.2'? y

mv:
mv (move ) .
, mv,
, , cp:
mv __ __

3.3 memo memo.0130.


ls , memo . mv memo.0130.
, 3.2
cp.
mv .
mv, cp:
. 110. V, ,
info coreutils 'mv invocation'.
MV

, cp, mv . , cp, mv
i (interactive). . cp .

71
3.3. mv

$ ls
memo
$ mv memo memo.0130
$ ls
memo.0130

lpr:
lpr (line printer )
. Linux ,

.

. (
, ),
lpstat p. lpr , ( ) ,
P.
report:
$ lpr report

,
, .
, .
, mailroom:
$ lpr -P mailroom report

, , lpstat o lpq:
$ lpq
lp is ready and printing
Rank Owner
Job Files
active max
86 (standard input)

Total Size
954061 bytes

Max , ,
.
,
( 86) lprm:
$ lprm 86

.
laser1:
$ lpr -P laser1 05.txt 108.txt 12.txt

lpr lpr 4
man-.

72 3.

grep:
grep1
. , , , .
grep 3.4 memo ,
credit, ,
. memo , discredit, creditor
accreditation, grep , ,
. w (
words
) grep .
, , .
3.4. grep

$ cat memo
Helen:
In our meeting on June 6 we
discussed the issue of credit.
Have you had any further thoughts
about it?
Max
$ grep 'credit' memo
discussed the issue of credit.
grep ,

.
V
man-.

head:
head ( ) .
, . , months,
12 , , head
Jan Oct (3.5).
3.5. head , tail

$ head months
Jan
Feb
Mar
Apr
grep , ed UNIX, Linux. g/re/p. g
global ( ), re ,
, p (print).

73
May
Jun
Jul
Aug
Sep
Oct
$ tail -5 months
Aug
Sep
Oct
Nov
Dec

head , , ..
, ,
. ,
months:
$ head -1 months
Jan

head
, .
V info coreutils 'head invocation'.

tail: ,
tail head,
. , , , ,

, .
tail 3.5 ( Aug Dec)
months.

logfile:
$ tail -f logfile

tail , ( CONTROL+C).
V info coreutils
'tail invocation'.
3.6. sort

$ cat days
Monday
Tuesday
Wednesday
Thursday
Friday

74 3.
3.6 ()
Sunday
$ sort days
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday

sort:

sort , . 3.6 cat, days, ,
.
sort .
sort . u ,
( ). n
. V info coreutils 'sort invocation'.

uniq:

uniq (
unique

) , , .
, , uniq
- (3.7).
uniq ,
. (, sort
, u.)
V info coreutils 'uniq invocation'.

diff:
diff (difference )
. .
,
.
diff u (
unified

output

format
) , ,
(+), (). 3.8
colors.1, colors.2.

75
3.7. uniq, -

$ cat dups
Cathy
Fred
Joe
John
Mary
Mary
Paula
$ uniq dups
Cathy
Fred
Joe
John
Mary
Paula

diff u , (hunk).
, at (@@). ,
, . 3.8 colors.1 ( )
. +1,5 ,
colors.2 .
diff u , .
, , .
, ,
. , ( , ),
.
V info-, diff.
3.8. diff,

$ diff -u colors.1 colors.2


--- colors.1
2009-07-29 16:41:11.000000000 -0700
+++ colors.2
2009-07-29 16:41:17.000000000 -0700
@@ -1,6 +1,5 @@
red
+blue
green
yellow
-pink
-purple
orange

file:
file
. file ,

76 3.
letter_e.bz2 , bzip2, :
$ file letter_e.bz2
letter_e.bz2: bzip2 compressed data, block size = 900k

file :
$ file memo zach.jpg
memo:
ASCII text
zach.jpg: JPEG image data, resolution (DPI), 72 x 72

file
V
man-.

| ():
Linux,
.
. 151.
Linux- (.
. 321).
UNIX Linux. (
[|] ) .
.
UNIX Linux,
, . (
. 143.) , ,
. , .
, . , head
, head,
.
months (. 3.5) head
:
$ sort months | head -4
Apr
Aug
Dec
Feb

, (, ).
wc (word count ) w (
words
) , :
$ ls | wc -w
14

77

, :
$ tail months | lpr


echo date Linux-. script ,
todos unix2dos ,
, Windows Mac OS X.

echo:
echo ,
echo. 3.9 .
,
(*), :
, .
echo Linux-.
, * . 158,
echo . 4, 8 10 echo ,
.
3.9. echo ( echo)

$ ls
memo memo.0714 practice
$ echo Hi
Hi
$ echo This is a sentence.
This is a sentence.
$ echo star: *
star: memo memo.0714 practice
$


V
info coreutils 'echo invocation'. echo, bash tcsh, man-.
echo , :
$ echo 'My new file.' > myfile
$ cat myfile
My new file.

, (>) echo

myfile
. . 146.

78 3.

date:
date :
$ date
Wed Mar 18 17:12:20 PDT 2009

, , ,
:
$ date +"%A %B %d"
Wednesday March 18

V info coreutils
'date invocation'.

script:
script , ,
. ,
, .
vim, , vim
, ,
, .
cat ,
vim, .
script
typescript. , script . , script,
, a.
, script:
$ script
Script started, file is typescript
$ whoami
sam
$ ls -l /bin | head -5
total 5024
-rwxr-xr-x 1 root root
2928 Sep
-rwxr-xr-x 1 root root
1054 Apr
-rwxr-xr-x 1 root root
7168 Sep
-rwxr-xr-x 1 root root 701008 Aug
$ exit
exit
Script done, file is typescript

21
26
21
27

21:42
15:37
19:18
02:41

archdetect
autopartition
autopartition-loop
bash

, script, exit. ,
cat, less, more . ,
script:
$ cat typescript
Script started on Thu Sep 24 20:54:59 2009

79
$ whoami
Sam
$ ls -l /bin | head -5
total 5024
-rwxr-xr-x 1 root root
2928 Sep
-rwxr-xr-x 1 root root
1054 Apr
-rwxr-xr-x 1 root root
7168 Sep
-rwxr-xr-x 1 root root 701008 Aug
$ exit
exit
Script done on Thu Sep 24 20:55:29

21
26
21
27

21:42
15:37
19:18
02:41

archdetect
autopartition
autopartition-loop
bash

2009

vim, emacs -
, fromdos dos2unix (
), ^M,
. man-,
script.

todos/unix2dos: Linux
Mac OS X Windows
, Linux-,
-, Windows Mac OS X,
, . todos
(to DOS DOS, tofrodos) unix2dos (UNIX
to DOS UNIX DOS, unix2dos)
Linux, , Windows OS X.
memo.txt ( ) DOS, :
$ todos memo.txt

$ unix2dos memo.txt

-
Windows

OS

X
. , todos . todos
, .bak,
b (backup ). unix2dos , (unix2dos
), n (new ).
Windows OS X ,
Linux, fromdos (from
DOS DOS, tofrodos) dos2unix (DOS to
UNIX DOS UNIX, dos2unix):

fromdos
dos2unix

$ fromdos memo.txt

$ dos2unix memo.txt

80 3.
man-, todos, fromdos, unix2dos dos2unix.
tr (translate , . V)
Windows OS X Linux. d
(delete ) tr (
\r) :
$ cat memo | tr -d '\r' > memo.txt

, (>) tr
memo.txt. 5, . ,
todos unix2dos, .


.
, -, DVD .
,
. , , .
, ,
, . ,
. ,
. , ,
,
.

bzip2:
bzip2
. -. -
. bzip2 ,
. ,
.
. 8000 letter_e 72 e ,
. .
$ ls -l
-rw-rw-r--

1 sam sam 584000 Mar

1 22:31 letter_e

l (long ) ls . , letter_e

81

584000. v (verbose ) bzip2


, . 99,99 :
$ bzip2 -v letter_e
letter_e: 11680.00:1, 0.001 bits/byte, 99.99% saved, 584000 in, 50 out.
$ ls -l
-rw-rw-r-- 1 sam sam 50 Mar 1 22:31 letter_e.bz2

50. bzip2
, .bz2. , ;
, .
bzip2 ,
.


.bz2

K
bzip

2 , bunzip

2,
. k (keep ) .

, zach.jpg :
$ ls -l
-rw-r--r--

1 sam sam 33287 Mar

1 22:40 zach.jpg

bzip2 28,
:
$ bzip2 -v zach.jpg
zach.jpg: 1.391:1, 5.749 bits/byte, 28.13% saved, 33287 in, 23922 out.
$ ls -l
-rw-r--r-- 1 sam sam 23922 Mar 1 22:40 zach.jpg.bz2

V man-, bzip2. - www.bzip.org


Bzip2 mini-HOWTO (, ,
HOWTO: , . 58).

bunzip2 bzcat:
, bzip2,
bunzip2:
$ bunzip2 letter_e.bz2
$ ls -l
-rw-rw-r-- 1 sam sam 584000 Mar
$ bunzip2 zach.jpg.bz2
$ ls -l
-rw-r--r-- 1 sam sam 33287 Mar

1 22:31 letter_e
1 22:40 zach.jpg

bzcat , bzip2.
cat .bz2, bzcat
. cat, bzcat

82 3.
. , , bzcat,
head, :
$ bzcat letter_e.bz2 | head -2
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

bzcat letter_e.bz2 ,
.
bzip2recover ,
. bzip2recover, ,
.

bzip2recover

gzip:
gzip (GNU zip) ,
bzip2, . ,
gzip, .gz. Linux gzip-. , , gzip-. gzip, gunzip zcat bzip2, bunzip2 bzcat .
V man-, gzip.

gunzip zcat

compress , , gzip. ,
.Z.

compress

GZIP ZIP

gzip
gunzip


zip
unzip

.
zip-, ,
, Windows


. zip- zip,
unzip. zip unzip PKZIP, Windows-,
.

tar:
tar . tape
archive . tar
.
( tar-, )
,
tar-. cpio (. V) .
ls g, b d.
tar c (create ), v (verbose
) f (file )
all.tar. ,
tar .

83

tar . all.tar 9700,


6000.
.
$ ls -l g b d
-rw-r--r-1 zach other 1178 Aug 20 14:16 b
-rw-r--r-1 zach zach 3783 Aug 20 14:17 d
-rw-r--r-1 zach zach 1302 Aug 20 14:16 g
$ tar -cvf all.tar g b d
g
b
d
$ ls -l all.tar
-rw-r--r-1 zach
zach
9728 Aug 20 14:17 all.tar
$ tar -tvf all.tar
-rw-r--r-- zach /zach
1302 2009-08-20 14:16 g
-rw-r--r-- zach /other
1178 2009-08-20 14:16 b
-rw-r--r-- zach /zach
3783 2009-08-20 14:17 d

t
. x t
tar-. , tar
1, -v.
tar-, , bzip2, compress gzip.
, , .
, tar bzip2,
.tar.bz2 .tbz. , tar gzip, .tar.gz, .tgz .gz, , tar compress, .tar.Z.
, tar gzip, . (
, bzip2, ,
gunzip bunzip2.) ,
GNU- make (ftp.gnu.org/pub/gnu/
make/make-3.81.tar.gz):

tar-

$ ls -l mak*
-rw-r--r-- 1 sam sam 1564560 Mar 26 18:25 make-3.81.tar.gz
$ gunzip mak*
$ ls -l mak*
-rw-r--r-- 1 sam sam 6072320 Mar 26 18:25 make-3.81.tar
$ tar -xvf mak*
make-3.81/
make-3.81/config/
UNIX- tar , , GNU- Linux- ,
. tar ,
.

84 3.
make-3.81/config/dospaths.m4
make-3.81/config/gettext.m4
make-3.81/config/iconv.m4

make-3.81/tests/run_make_tests
make-3.81/tests/run_make_tests.pl
make-3.81/tests/test_driver.pl

, tar gzip: make-3.81.tar.gz ( 1,2).


(*)
(. * . 158, ls ,
mak. .
,
. gunzip
make-3.81.tar ( .gz), 4,8. tar
make-3.81, .
$ ls -ld mak*
drwxr-xr-x 8 sam sam
4096 Mar 31
-rw-r--r-- 1 sam sam 6072320 Mar 26
$ ls -l make-3.81
total 2100
-rw-r--r-- 1 sam sam 53838 Mar 31
-rw-r--r-- 1 sam sam
2810 Mar 19
-rw-r--r-- 1 sam sam 18043 Dec 10
-rw-r--r-- 1 sam sam 106390 Mar 31

-rw-r--r-- 1 sam sam 16907 Feb 11


-rw-r--r-- 1 sam sam 17397 Feb 11
drwxr-xr-x 6 sam sam
4096 Mar 31

2006 make-3.81
18:25 make-3.81.tar
2006
2006
1996
2006

ABOUT-NLS
AUTHORS
COPYING
ChangeLog

2006 vmsjobs.c
2006 vpath.c
2006 w32

tar ,
, mak: make3.81.tar make3.81. d
(directory ) ls , .
ls , make-3.81. V
man-, tar.
X
TAR:
tar

- .
, , tar t
tar-. (.
mkdir

4), tar

-
.
, .
. , ,
tar

,
. t , tar
.

, 85
X
TAR:
x tar ,

, . ,
.

gunzip tar (|),


gunzip,
tar:
$ gunzip -c make-3.81.tar.gz | tar -xvf -

c gunzip
. () tar .
, , . 151, V ( gzip tar).
tar z. tar
gunzip ( gzip, )
:
$ tar -xvzf make-3.81.tar.gz

j bzip2 bunzip2.

,
whereis slocate , ,
, , .
, which ,
. slocate .

which whereis:
Linux - ,
. .
, , PATH:
, . 312.
, , .
.

which , ,
. ( 4 Linux.)
.
. which , . which
tar:
which

$ which tar
/bin/tar

86 3.
which , ,
. which,
,
, , . (

. 111.) , , tar , /bin/tar /usr/local/bin/tar, ,
.
whereis ,
whereis
, , .
, , , tar:
$ whereis tar
tar: /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

whereis ,
tar: tar, tar man-, tar.

WHICH WHEREIS
, which ()
, (. . 312, PATH:
, , .
, which
(, ).
whereis , .
whereis ()
, ;
whereis .

WHICH, WHEREIS

, which, whereis, , ,
, , (.
5, ). whereis, ,
echo ( ,
, ), :
$ whereis echo
echo: /bin/echo /usr/share/man/man1/echo.1.gz

whereis
echo

. which

:
$ which echo
/bin/echo
bash, type (. 10,
), ,
:
$ type echo
echo is a shell builtin (echo , )

slocate locate:
slocate (
secure
locate

), locate, :

87
$ slocate motd
/usr/share/app-install/icons/xmotd.xpm
/usr/share/app-install/desktop/motd-editor.desktop
/usr/share/app-install/desktop/xmotd.desktop
/usr/share/base-files/motd.md5sums
/usr/share/base-files/motd

, slocate locate, . slocate locate updatedb


slocate. cron ( crontab cron
V).

,

, ,
. ,
, .


, ,
,
.
, , ,
. who , , ,
, .
w finger ,
, .
, finger , . .3.1
, .

who: ,
who , .
3.10 who , Sam,
Max Zach. (Max .)
, . , .
.
( ) , .
, who, ,
, .
,

88 3.
write (. ).
who
,
(. ).
, who, ,
(|, . . 151),
less,
. grep, .
,
, who am i:
$ who am i
max
tty2

2009-07-25 16:42

3.10. who

$ who
sam
max
zach
max

tty4
tty2
tty1
pts/4

2009-07-25
2009-07-25
2009-07-25
2009-07-25

17:18
16:42
16:39
17:27 (coffee)

3.11. finger, : ,

$ finger
Login
max
max
sam
zach

Name
Max Wild
Max Wild
Sam the Great
Zach Brill

Tty
*tty2
pts/4
*tty4
*tty1

Idle
3
29
1:07

Login Time
Office
Jul 25 16:42
Jul 25 17:27 (coffee)
Jul 25 17:18
Jul 25 16:39

finger: ,
finger ,
. finger , ,
, , , - , ,
. ,
, , . ,
3.11 Max
coffee. (*), Tty, , ,
(. mesg: . 92).
FINGER

, ,

finger. ,
.
Mac OS X finger .

89

finger , .
3.12 finger
Max, (tty2). (pts/4) 3
7. finger ,
Max , Zach 1693.
3.12 finger
. ,
Plan:, Max. finger
.plan Max .
.plan
.project

3.12. finger, : ,

$ finger max
Login: max
Name: Max Wild
Directory: /home/max
Shell: /bin/tcsh
On since Fri Jul 25 16:42 (PDT) on tty2 (messages off)
On since Fri Jul 25 17:27 (PDT) on pts/4 from coffee
3 minutes 7 seconds idle
New mail received Sat Jul 25 17:16 2009 (PDT)
Unread since Sat Jul 25 16:44 2009 (PDT)
Plan:
I will be at a conference in Hawaii all next week.
If you need to see me, contact Zach Brill, x1693.
:
.
, . 1693.

( , , .plan, , ls, .)
, .plan,
,
, , . finger
.project .pgpkey,
. Max , finger
, ,
.
finger
. , ,
Helen Simpson, , Helen Simpson,
, hls. finger, ,
Helen, .
, Helen
Simpson:

90 3.
$ finger HELEN
Login: hls

$ finger simpson
Login: hls

Name: Helen Simpson.


Name: Helen Simpson.

finger V.

w: ,
w , .
w , , who: , w, ,
- .
3.13 , Max,
Zach Sam. , . , . ,
(, ,
).
, , ,
. , .
, w, , ,
( , ), ( ).
, ,
, ,
1, 5 15 . ,
uptime. w, who finger
.3.1.
3.13. w

$ w
17:47:35 up 1 day, 8:10,
USER
TTY
FROM
sam
tty4
max
tty2
zach
tty1
max
pts/4
coffee

4 users, load average: 0.34,


LOGIN@
IDLE
JCPU
17:18
29:14m 0.20s
16:42
0.00s
0.20s
16:39
1:07
0.05s
17:27
3:10m
0.24s

0.23, 0.26
PCPU WHAT
0.00s vi memo
0.07s w
0.00s run_bdgt
0.24s bash

3.1. w, who finger


who

finger

(
)

91

finger

( /etc/passwd)

who


, , , ,
.

write:
write , .
write ,
. write
(3.14).
write :
write _ []
3.14. write,

$ write max
Hi Max, are you there? o

, Ma, ? o

3.15. write,

$ write max
Hi Max, are you there? o
, Ma, ? o
Message from max@bravo.example.com on pts/0 at 16:23
Yes Zach, I'm here. o
, , .

_ , . ,
, . who,
w finger ,
, .
, ,
write, ,
. write
- ( 3.15). -

92 3.
, o ( over ),
, , oo
( over and out ),
. ,
CONTROL+D.
write ,
EOF (end of file)
. .
,
, CONTROL+ L CONTROL+ R,
.
,
, . ,
, .

mesg:
.
, :
$ mesg y

Max , Zach , Zach


:
$ write max
write: max has messages disabled

max

mesg n. mesg
y ( yes, , )
n ( no, , ).
write, , , ,
:
$ write max
write: write: you have write permission turned off.
write


,
, , .
.
write , , .

93

, .
.
Linux ,
mailx, , Mozilla/Thunderbird,
pine, Kmail , .
emacs.
sylpheed (sylpheed.sraoss.jp/en).
,
. procmail (www.
procmail.org) ,
, ; , ; ..
GNU Privacy Guard (GPG GNUpg; www.gnupg.org)
,
.
, ,
,
, ,
. ,
Max ,
.
, mgs@sobell.com.

, , ,
, Linux-.

, .
, .3.2, , , .
3.2.

cp

diff

file

grep

()

head

lpq

lpr

()

lprm

94 3.
3.2 ()

mv

()

sort

tail

uniq

, -

, ,
bzip2. ,
, , . , bzip2
bunzip2, () . .

3.3 , .
bzip2.
3.3.

bunzip2

, bzip2,

bzcat

, bzip2

bzip2

compress

gunzip

, gzip compress,

gzip

( , bzip2)

zcat

, gzip

, , . tar (.3.4) .
.tar.bz2, .tar.gz .tgz , ,
, tar, ,
.
3.4.

tar

, .3.5, , . ,
C++, .
3.5.

locate

whereis

, man-

which

95

.3.6 , . ,
, , , , .
3.6.

finger

hostname

who

, .

3.7, .
3.7.

mesg

, write

write

.3.8
3.8.

date

echo

1. , , ?
2. write
? ?
3. , , done ?
$ cp to_do done
$ mv to_do done

4. ?
Hi $2, I'm number * (guess)!

5. Ace Electronics phone,


?
? ? -?
6. , diff , ? (
gzip.) , , diff ,
, ASCII.

96 3.
7. .plan . finger
.plan?
8. , which , ,
?
9. , ? , ?
10. , file /usr/bin.
?
11.
status.report?
?


1. colors.1 colors.2, 3.8.
, diff u.
, ?
2. :
$ echo cat
$ cat echo

, .
3. 5, phone.gz, . ,
.
4. ,
) gzip 80;
) gzip 10;
) gzip.
ls l , . , , ?
5. .
, , gzip, , .
man-, uuencode, ASCII.
.
) ASCII, uuencode.
, ? . (
uuencode , ,
yum aptitude,
. sharutils.)
) uuencode ?
.

4.
,
.
, .
Linux ,
, .
,

. , ,
Access Control Lists (ACL),
( ) . ,
.
, df, fsck, mkfs tune2fs,
V
, .


.

: , , , , . (.4.1).

. 4.1.

98 4.
Linux
. . ,
. Linux-
, .
.

,
. ,
. ,
. , .4.2 , correspond (). : business
(), memos () personal ().
business , ,
. , ,
, ,
milk_co.
Linux
.

.


, ,
, , , . .4.2 4.3 , , ,
.
, . ,
, . , , ,
. , , , .
( .4.2 4.3 .)

, .
, , ( ,
) (
).
, .

.

79 Wednesday, September 30, 2009 10:13 AM

99
Directory Files and Ordinary Files 79

correspond

personal

memos

business

milk_co

letter_1

Figure 4-2

cheese_co

letter_2

4.2.

A.
secretarys
directories

downward from the root, with paths connecting the root to each of the other files. At

the end of each path is either an ordinary


file or a directory file. Special files, which
can also appear at the ends of paths, provide access to operating system features.
Ordinary files, or simply files, appear at the ends of paths that cannot support other

paths. Directory files, also referred


to as directories
or folders, are the points that
other paths can branch off from. (Figures 4-2 and 4-3 show some empty directories.) When you refer to the tree, up is toward the root and down is away from the


root. Directories
directly connected
by a path are
called parents (closer to the root)
and children (farther from the root). A pathname is a series of names that trace a
path along branches from one file to another. See page 83 for more information


about pathnames.

lenames

Every file has a filename. The maximum length of a filename varies with the type of
. 4.3.
filesystem; Linux supports several types of filesystems. Although most of todays
filesystems allow files with names up to 255 characters long, some filesystems

Directory .
. Linux

.
Directory
Directory
Directory

255,


.
, , , ,
Ordinary File :
Ordinary File
Directory

(AZ).
(az).
Directory
Ordinary File
(09).
(_).
Ordinary File
Ordinary File
(.).
(,).
Figure 4-3 Directories and ordinary files

100 4.
,
. (
, Linux

.) , , .
.
, hold1,
wombat junk, , foo foobar.
, ,
.
, :
correspond ();
january ();
davis ();
reports ();
2001;
acct_payable (__).
, . , DOS Windows, ,
, . UNIX-
14 , Macintosh 31-
. , ,
,
, .

.
stat (. V)
. , Namelen:,
, (/home).

$ stat -f /home | grep -i name


ID: ff1f5275f648468b Namelen: 255

Type: ext2/ext3

f stat
. i (
case
insensitive

) grep .
.
, .
. 69.

()
, : . ,
ext UFS

101

, JANUARY, January
january . FAT ( ) ,
. HFS+, OS X,
, , .


, .

, .

.
: joe.05.04.26, new_stuff.
, ,
, ,
.
my file .
$ lpr my\ file
$ lpr "my file"


,
. , .4.1,
. ,
C,
, . ,
. ,
, notes.4.10.01 files.tar.gz. Mac OS X
, .
4.1.

compute.c

compute.o

compute.c

compute

compute.c

memo.0410.txt

memo.pdf

PDF-; GUI
xpdf kpdf

memo.ps

PostScript
- GUI;

gs kpdf

memo.Z

, compress; uncompress
gunzip

memo.tgz memo.tar.gz

tar- , gzip

102 4.
4.1 ()

memo.gz

, gzip; zcat,
gunzip

memo.bz2

, bzip2; bzcat,
bunzip2

memo.html

, -, Firefox

photo.gif, photo.jpg,
photo.jpeg, photo.bmp,
photo.tif, or photo.tiff

, ,


, , ( ),
ls . ls a
, . (. . 103)
,
.
.plan (. 3, .plan .project) .

(. ..) (. . .. . 108).


Linux
,
.
: ( ) zach.
pwd (print working directory ) (. 5, ) .
pwd


.

pwd (. 4.1). Linux /home, Mac OS X
/Users.
ls .

, ls , .
( , ,
.)

103


, , . Mac OS X
.
, , stty (set terminal )
(. . 48)
(. . 49).
4.1.

login: max
Password:
Last login: Wed Oct 20 11:14:21 from bravo
$ pwd
/home/max

, , , .
, , .
,
, ls a.
. 284 (
bash) 9 ( tcsh).


,
.
(/ ) ,
. , , .
, .
,
.



.
(/), , .
(/),
. ,
. , , .
,
(/). ,
/ (
)

104 4.
, . , , ,
(, /home/zach/). ,
, ,
. .4.4
.
( ),
-
, ,
. ,
Sam
, , ,
/usr/bin:
$ pwd

fm Page 84 Wednesday,
September 30, 2009 10:13 AM
/home/sam

$ ls /usr/bin
7z
7za
822-date
Chapter 4 The Filesystem

kwin
kwin_killer_helper
kwin_rules_dialog

/
/home

/etc
home

tmp

etc
/home/hls

max

zach

hls

/home/hls/notes

/home/zach
bin

notes
/home/hls/bin/log

report

Figure 4-5

log

. 4.4.
Absolute pathnames

~ (Tilde)
in Pathnames

~ ()

In another form of absolute pathname, the shell expands the characters ~/ (a tilde
followed by a slash) at the start of a pathname into the pathname of your home
~/ (, ), ,
directory. Using this shortcut, you can display your .bashrc startup file (page 272)
. ,
with the following command, no matter which directory is the working directory:
.bashrc (.
. 286) $
( ,
less ~/.bashrc
):
A tilde quickly references paths that start with your or someone elses home directory.
$ less ~/.bashrc
The shell expands a tilde followed by a username at the beginning of a pathname into
the pathname of that users home directory. For example, assuming he has permission
to do so, Max can examine Sams .bashrc file with the following command:
$ less ~sam/.bashrc

105

,
- . , ,
. (, ), Max
.bashrc, Sam,
:
$ less ~sam/.bashrc

. 355.


.
, ( /)
(~), . , ,
, .
,
. .



. .
,
.
(GUI), .
,
. - ,
- , -
.
,


,
, ( ).
, ,
. pwd.
, vim,
, .
, .
,
goals, :
$ vim ~/goals

.4.5. , ,
. , ,
: ,

106 4.
.
.
, , , . ( . .. . . .. . 108.)
/
..
home

tmp

etc
=.

max

zach

hls
notes

.. /zach
bin

notes
bin/log

repor t

log

. 4.5.

k.fm Page 86 Wednesday,


September 30,
2009 10:13 AM

( mkdir), ( cd), ( rmdir),


,
.
,
Chapter 4 The Filesystem
Linux.
/

home

max

names

temp

literature

demo

promo

.4-7
4.6.
,developed


Figure
The file structure
in the examples

mkdir: Creates a Directory

107

mkdir:
mkdir . mkdir
. ,
.4.6.
.
4.2 pwd , Max
(/home/max), ls
: demo, names temp.
mkdir, Max literature .
( ), , literature . Max
: mkdir /home/max/literature
mkdir ~max/literature.
ls, 4.2, . F ls
( , ).
ls ,
. ls ,
literature .
4.2. mkdir

$ pwd
/home/max
$ ls
demo names temp
$ mkdir literature
$ ls
demo literature names temp
$ ls -F
demo literature/ names temp
$ ls literature
$

promo, literature.
, /home/max , :
$ pwd
/home/max
$ mkdir literature/promo

:
$ mkdir /home/max/literature/promo

, literature promo,
mkdir p (parents ):
$ pwd
/home/max

108 4.
$ ls
demo names temp
$ mkdir -p literature/promo

$ mkdir -p /home/max/literature/promo

cd:
cd (change directory ) ,
. 4.3
/home/max/literature, pwd.
Max cd , literature .
, .
pwd ,
Max
. cd ,
. cd, 4.3,
, Max

. , , , Max
literature (cd
literature) pwd.
4.3. cd

$ cd /home/max/literature
$ pwd
/home/max/literature
$ cd
$ pwd
/home/max
$ cd literature
$ pwd
/home/max/literature

.
, , , .
,
.
,
. , ,
.
cd .
Sam, .

. ..
mkdir :
(.) (..) . .4.5. (.) ,
(..) ,

109

. ,
.
literature (..) :
(/home/max), memoA
.
$ pwd
/home/max/literature
$ ls ..
demo literature names
$ cp memoA ..
$ ls ..
demo literature memoA

temp
names

temp

cd promo (
literature ) Max
vim .
$ cd promo
$ vim ../../names

, ,
,
. ls, vim, mkdir, rm Linux-.

rmdir:
rmdir (remove directory
). , , . .., . ,
, rm
, . ( )
. .., rmdir .
promo:
$ rmdir /home/max/literature/promo

rm r (rm r filename),
, .
RM R

,
rm

r
,
. ,
(*). ,
.


touch
, letter. touch (. V):

110 4.
$ cd
$ pwd
/home/max
$ touch letter

/home/max,
cp letter /home/
max/literature/promo. ( promo,
.) letter.0610:
$ cp letter literature/promo/letter.0610


Max
, vim :
$ vim literature/promo/letter.0610

Max ,
cd, vim promo
:
$ cd literature/promo
$ pwd
/home/max/literature/promo
$ vim letter.0610

.fm Page 90 Wednesday,


September
30, 2009
10:13 AM

(
/home/max/literature), Max

, ..:

$ cd ..
$ pwd
Chapter 4/home/max/literature
The Filesystem
/

home

zach

max

sam

names

temp

literature

names

temp

. 4.7.Figure

mvmove

4-10 Using
mv to
names and
temp names temp

mv, cp:
v, cp: Move
or
Copy Files
Chapter
3 discussed the
use of mv to rename
files.
mv works even
more
3

mv However,

.
generally:
You can
use this
utility to move
files from one directory
to another

mv

:

(change the pathname of a file) as well as to change a simple filename. When used
to move one or more files to a new directory, the mv command has this syntax:

111

(
), . mv ,
:
mv __

/home/max, Max
names temp literature
:
$ mv names temp literature

names temp /home/


max/names /home/max/temp /home/max/literature/names /home/max/literature/
temp (.4.7). Linux, mv
, .
Linux , , mkdir.
mv
.
cp , mv, ,
__ .

mv:
mv ,
. ,
,
:
mv __ _

_ , __ , mv _ (
mv ).
mv,
cp , r (recursive ).
V, tar cpio.


Linux . Linux,
, Linux-.
Linux, FSSTND (Linux Filesystem Standard)
1994. 1995 , UNIX- FHS
(Linux Filesystem Hierarchy Standard Linux;

112 4.
proton.pathname.com/fhs). FHS LSB (Linux Standard Base
Linux; www.linuxfoundation.org/en/LSB), FSG (Free

Group
91 Wednesday,Standards
September 30, 2009
10:13 AM

). , FSG
Open Source Development
Labs (OSDL), Linux Foundation (www.linuxfoundation.org).
.4.8
FHS.
Working with Directories 91
.
/
bin

sbin
mail

var
spool

dev

usr
bin

etc
sbin

tmp
max

home
zach

root
hls

. 4.8.

Linux

structure
Linux, FHS
Figure 4-11
A typical
FHS-based
filesystem

If new-directory
does not
the existing-directory-list
mustcontain
just
one

exist,

,
.4.8,

mv changes
new-directory
(mv renames the directory).
directory
name, which

FHS
.to

Linux

mv, you
cannot
copy,
their contents
with
Although
you
can rename
directories using

FHS
.
cp
unless you
use the
option.
Refer ,
to the explanations
of tar

r
(recursive)

.
/opt
(
(page 846)
and cpio (page
644) for other
ways to copy ,
and move directories.

/etc/
opt , /opt.

portant
Standard
Directories and Files
/
.

Originally files
on a Linux
Linux system
were
not
located
in
standard
places within
the

.
directory hierarchy. The scattered files made it difficult to document and maintain a
/bin

.
, package
Linux system
and just about
impossible
for someone
to
release a software

on
,

that
would
compile
and run
all Linux
systems.
The
first standard
for the
Linux

FSSTND

.
filesystem, the
(Linux Filesystem
Standard), was released early in 1994. In
early 1995 work
was started
on a broader
standard
covering
UNIX-like
sys/boot

many
,

tems:
FHS
(Linux
Filesystem
Hierarchy
Standard;
proton.pathname.com/fhs).
More
.
recently FHS has been incorporated in LSB (Linux Standard Base; www.linuxfoun/dev
a .

,Group).

dation.org/en/LSB),
workgroup of
FSG (Free
Standards
Finally, FSG
com,

,
.


bined with
Open
Source
Development
Labs (OSDL)
to form the
Linux
Foundation

udev
(www.linuxfoundation.org).
Figure
4-11 shows
the locations
of some important

and
,

/dev of

directories
files as specified
by FHS. The
significance
many of these
directo,

ries will become


clear as you .
continue reading.
The following
list describes the directories
in Figure.
4-11, some
of the direc/etc
shown

tories specified
by FHS, and some
other
directories.
Most
Linux
distributions
do
,

.

not
use
all
the
directories
specified
by
FHS.
Be
aware
that
you
cannot
always
deter /etc/passwd, , mine
the
function of a directory
name.
For
example,
although
/opt stores add

.by
its
Mac
OS X

/etc/passwd
Open
on
software,
/etc/opt
stores
configuration
files
for
the
software
in
/opt.
Directory.
/ Root The root
directory, present in
all Linux
filesystem
is the ancestor
of
/etc/opt

structures,

all
files
in
the
filesystem.
, /opt.
/bin Essential command binaries Holds the files needed to bring the system up and run it

when it first comes up in single-user or recovery mode.


/boot Static files of the boot loader Contains all files needed to boot the system.

113

X Window,
.

/etc/X11

. , , /home.
, , /home, Zach
/home/zach.
/home, , /inhouse
/clients. Mac OS X
/Users.
/home

/lib

/lib/modules

/mnt

() .

/opt

() .

/proc

,
.

/root

root.

. ,
/sbin /usr/sbin. /sbin
, , /usr/
sbin , .
Linux , (/etc, /usr/
bin, /usr/adm, /usr/include).
/sbin

/sys

/tmp

Mac OS X.
, ,
/Users. Linux /home.
/Users

. , , . /usr

.
/usr

.
Linux/OS X , , .
/usr/bin

/usr/games

/usr/include

, C.

114 4.
/usr/lib

. , . bin, games, include, lib,


sbin, share src.
/usr/local

, . . /sbin.

/usr/sbin

/usr/share
- . dict, doc, games, info, locale, man, misc, terminfo zoneinfo.
/usr/share/
doc

/usr/share/
info

GNU.

/usr/share/
man

/usr/src

. ,
, , /var.
,
, .
cache, lib, lock, log, mail, opt, run, spool, tmp yp.
Linux , /usr (/usr/adm, /usr/mail, /usr/spool, /usr/tmp).
/var

/var/log
. , , lastlog
( ), messages (
syslogd) wtmp (
).
/var/spool
(). anacron,
at, cron, lpd, mail, mqueue, samba . /var/spool/mail /var/mail.


Linux
: Linux Access Control List (ACL).
Linux. , , .
: (), , (),
( ).
: ,
.

115

ls l:

m Page 94

ls l
.

Wednesday, September 30, 2009 10:13 AM


. letter.0610 ,
check_spell , ,
:

e
am
en
Fi l

Da
t
of e an
mo d t
di f i m e
ica
ti o
n

S iz
e

p
Gr
ou

Typ

eo
f fi
le
Fi l
ea
pe c c
r m es
iss s
i on
s
AC
Lf
l
a
g
Lin
ks
Ow
ne
r

$ ls -l letter.0610 check_spell
1 max pubs 852 Jul 31 13:47 check_spell
Chapter 4 -rwxr-xr-x
The Filesystem
-rw-r--r-- 1 max pubs 3355 Jun 22 12:44 letter.0610

-rwxrwxr-x+..3.max.....pubs.........2048.Aug.12.13:15.memo

Figure 4-12

. 4.9. , ls l

The columns displayed by the ls l command

left

ls ldisplays
,

From
to right,
the lines
that an ls
l command
contain
the following

(.4.9):
information (refer to Figure 4-12):
( ).
The type of file (first character)
( ).
The files access permissions (the next nine characters)
ACL (, (. The
ACL flag
(present if
the file has
an ACL, page
99). 121).

(ACL-)
The number
of links
to the(.
file (page


104)
. 126).

The
name
of
the
owner
of
the
file
(usually
person who
created the file)

).
( , the
,

The
name of
the group
the file
is associated
with


().

The size
of the
file in characters
(bytes)

The
date and
time the
file
was created
or last modified

.
The name of the file
(first

(
is
letter.0610

of file
column)
for )
letter.0610
a hyphen
() because
it is an ordi
(),file.


.
see

on

nary
Directory
files
have a d in
this column;
Figure V-19
page 748
for a

.
that
,

, list of dother
characters
can appear
in this
position.

.
V.19.
Permissions The next three characters specify the access permissions for the owner of the file: r

Type of fileThe
type

(in the first position)


indicates
permission,
w (in the
second
position)

read

indicates

write permission,
andr (
x (in
the third
position)
indicates
execute
A
in
:

permission.
, w (

one of the
positions
indicates
the owner
does not
have the
permission associ)

that

x (
)

ated with that
position.
.

, (), , ,

the

.represent permissions for the group,


In a similar
manner
next
three
characters


and the final three characters represent permissions
for
other (everyone
else).
In the
,preceding

example, the owner of letter.0610 can read from and write to the file,

letter.0610

whereas
the group
and
others
can onlyread
from
the file
and no one is
allowed
to
,

execute it. Although execute permission can be allowed for any file, it does not
make sense to assign execute permission to a file that contains a document, such as
a letter. The check_spell file is an executable shell script, so execute permission is
appropriate for it. (The owner, group, and others have execute permission.) For

116 4.
.
,
, , , .
check_spell ,
. ( , .)
, ls . 757.

chmod:
Linux

, ,
() .
(
, , ).
(, ). (
, ,
, ). ( ).
ROOT-

, , .
, root-,
, ,
.

, . ,
chmod (change mode )
. chmod ()
() .

chmod
chmod, (+) (rw) (a) :
$ ls -l letter.0610
-rw------- 1 max pubs 3355 Jun 22 12:44 letter.0610
$ chmod a+rw letter.0610
$ ls -l letter.0610
-rw-rw-rw- 1 max pubs 3355 Jun 22 12:44 letter.0610

chmod
. , , (
) . chmod
() (r) (x) (o) . .

117
$ ls -l check_spell
-rwxr-xr-x 1 max pubs 852 Jul 31 13:47 check_spell
$ chmod o-rx check_spell
$ ls -l check_spell
-rwxr-x--- 1 max pubs 852 Jul 31 13:47 check_spell



, ,
( , ),
, , ,
, .
,
. () ,
.
, .

a (all ) o (ther ),
chmod g (group ) u (user
, ,
,
). , chmod a+x ( , ),
chmod gorwx , .

CHMOD: O , U

chmod
,
o

owner

, . o other ,
u owner (user) (). UGO (user-groupother, -- )
.

chmod
, chmod
. ,
, , .
(, , setuid


setgid
.) ,
, . 1 ( ) , 2 ,
4 . ,
, ,
(),
.
,
, .
chmod ,
,
, . 6

118 4.
(4) (2). 0
.
$ chmod 600 letter.0610
$ ls -l letter.0610
-rw------- 1 max pubs 3355 Jun 22 12:44 letter.0610

7 (4 + 2 + 1) ,
. 5 (4 + 1)
:
$ chmod 755 check_spell
$ ls -l check_spell
-rwxr-xr-x 1 max pubs 852 Jul 31 13:47 check_spell


.V.8.
chmod chmod: . 292, ,
, chmod,
V.

setuid setgid
,
setuid
- (
set

user

ID
), , ,
. , setuid-,
, ,
, .
, setgid

- (
set
group

ID

), , , ,
.
SETUID- SETGID-,
ROOT,
setuid

-,
root, root-,
root-.
, , root

- (
).
, setgid

-,
(root).
, setuid-, (root), setgid,
root
,
,
. - , ,
. setuid

-
passwd.

, root-
chmod,
setuid-, setgid-. ,
ls l (. ls l: . 115),

119

setuid
- , , s, setgid-
, :
$ ls -l myprog*
-rwxr-xr-x 1 root pubs
-rwxr-xr-x 1 root pubs
# chmod u+s myprog1
# chmod g+s myprog2
$ ls -l myprog*
-rwsr-xr-x 1 root pubs
-rwxr-sr-x 1 root pubs

19704 Jul 31 14:30 myprog1


19704 Jul 31 14:30 myprog2

19704 Jul 31 14:30 myprog1


19704 Jul 31 14:30 myprog2

chmod
.
, 1 , 2 setgid-,
4 setuid-:
$ ls -l myprog*
-rwxr-xr-x 1 root pubs
-rwxr-xr-x 1 root pubs
# chmod 4755 myprog1
# chmod 2755 myprog2
$ ls -l myprog*
-rwsr-xr-x 1 root pubs
-rwxr-sr-x 1 root pubs

19704 Jul 31 14:30 myprog1


19704 Jul 31 14:30 myprog2

19704 Jul 31 14:30 myprog1


19704 Jul 31 14:30 myprog2

SETUID-
setuid

-,
.


.
, , .
:
()
, .
.
,
ls , .
ls .
Zach ,
, info,
Max. , ls
d (directory ) l (long ):
$ who am i
zach
pts/7

Aug 21 10:02

120 4.
$ ls -ld /home/max/info
drwx-----x
2 max pubs 512 Aug 21 09:31 /home/max/info
$ ls -l /home/max/info
ls: /home/max/info: Permission denied

d , ls, , /home/max/info . Max ,


; , pubs,
, ,
x . Zach , ls l
.
Zach , ,
, , .
notes, cat
. financial, :
$ ls -l /home/max/info/financial /home/max/info/notes
-rw------1 max pubs 34 Aug 21 09:31 /home/max/info/financial
-rw-r--r-1 max pubs 30 Aug 21 09:32 /home/max/info/notes
$ cat /home/max/info/notes
notes.
$ cat /home/max/info/financial
cat: /home/max/info/financial: Permission denied

Max
info:
$ chmod o+r /home/max/info

Zach info, ,
. ls l info, Zach -
financial. ( ,
.) , Zach newfile,
touch (. V). Max info, Zach
:
$ ls -ld /home/max/info
drwx---r-x
2 max pubs 512 Aug 21 09:31 /home/max/info
$ ls -l /home/max/info
total 8
-rw------1 max pubs 34 Aug 21 09:31 financial
-rw-r--r-1 max pubs 30 Aug 21 09:32 notes
$ cat /home/max/info/financial
cat: financial: Permission denied

$ touch /home/max/info/newfile
touch: cannot touch '/home/max/info/newfile': Permission denied

(ACL-) 121

(ACL-)
(
ACL
-) Linux. ACL-
,
.
ACL
- , , ,
Linux. ,
, ACL-.
, ACL- , ,
.
ACL- . ,
, ACL. : . ( ACL- (access ACLs) ACL-
(default ACLs), ACL-
, ACL-.)

. ACL

- , (ACL-) ,
, ACL-.
ACL-

cp


p
(
preserve
)
a
(
archive

), ACL- . mv
ACL-

cp


p

a

ACL-, , mv
ACL-, :
$ mv report /tmp
mv: preserving permissions for '/tmp/report': Operation not supported
(mv: '/tmp/report': )
, tar, cpio dump, ACL-.
cp a , ACL-,
.

ACL
- ,
, , .

ACL-
ACL Linux.
ACL

-, acl.
Linux ACL- ext2,
ext3 ext4, ACL- -

122 4.
. ACL- ext
acl ( no_acl).
, , /home,
ACL- , /home,
acl /etc/fstab:
$ grep home /etc/fstab
LABEL=/home

/home

ext3

defaults,acl

1 2

fstab ACL-, /home.


- ,
( root-),
- /home. , /home mount remount:
# mount -v -o remount /home
/dev/hda3 on /home type ext3 (rw,acl)


setfacl ACL- , getfacl
. Linux.
getfacl , ACL-,
, ls l,
:
$ ls -l report
-rw-r--r-- 1 max max 9537 Jan 12 23:17 report
$ getfacl report
# file: report
# owner: max
# group: max
user::rwgroup::r-other::r--

, getfacl, . , , .
ls l: .115.
omit-header ( omit ) getfacl :
$ getfacl --omit-header report
user::rwgroup::r-other::r--

, user, (::)
, .

(ACL-) 123

group ,
, . , other,
: (other)
.
setfacl -- modify ( m)
ACL- ,
:
setfacl modify ugo:: _

ugo u, g o, ,
(), ; ,
; ; _
, .
(o), .
(rwx, rx ..),
.
chmod ( , ),
setfacl
, ugo
.
, chmod.
, ACL-,
Sam :
$ setfacl --modify u:sam:rw- report

$ setfacl --modify u:sam:6 report


$ getfacl report
# file: report
# owner: max
# group: max
user::rwuser:sam:rwgroup::r-mask::rwother::r--

, user:sam:rw, , Sam
(rw).
ls l: . 115. ,
mask, .
ACL-, ls l (+),
, ACL- :
$ ls -l report
-rw-rw-r--+ 1 max max 9537 Jan 12 23:17 report

124 4.

M
,
mask
, . , ACL- .
, . ,
Linux. , setfacl

, ACL-, ,
ACL-.
, mask ugo setfacl.
report:
$ setfacl -m mask::r-- report


mask
,
getfacl
, , (r- -). , Sam
, , - .
, .
$ getfacl report
# file: report
# owner: max
# group: max
user::rwuser:sam:rwgroup::r-mask::r-other::r--

#effective:r--

, setfacl ACL-
ACL-:
$ setfacl -m u:sam:r--,u:zach:rw- report
$ getfacl --omit-header report
user::rwuser:sam:r-user:zach:rwgroup::r-mask::rwother::r--

x ACL- .
,
. setfacl, ,
Sam :
$ setfacl -x u:sam report
$ getfacl --omit-header report
user::rwuser:zach:rwgroup::r-mask::rwother::r--

x . ugo
. b, ,
ACL- ACL- .

(ACL-) 125

setfacl getfacl .
man-, --help.


, dir ACL-.
setfacl d (default ),
ACL- dir.
, dir, ACL. pubs
admin , .
$ ls -ld dir
drwx------ 2 max max 4096 Feb 12 23:15 dir
$ getfacl dir
# file: dir
# owner: max
# group: max
user::rwx
group::--other::--$ setfacl -d -m g:pubs:r-x,g:admin:rwx dir

ls , dir ACL-,
+ . , getfacl, default:.

, , ,
.
Linux
ACL-.
pubs admin. .
$ ls -ld dir
drwx------+ 2 max max 4096 Feb 12 23:15 dir
$ getfacl dir
# file: dir
# owner: max
# group: max
user::rwx
group::--other::--default:user::rwx
default:group::--default:group:pubs:r-x
default:group:admin:rwx
default:mask::rwx
default:other::---

, , ACL- .
.
, ACL-
,

126 4.
. ACL-
.
touch new dir.
ls , ACL-.
umask (. man-, bash) , ,
, .
,
pubs , admin
. .
$ cd dir
$ touch new
$ ls -l new
-rw-rw----+ 1 max max 0 Feb 13 00:39 new
$ getfacl --omit new
user::rwgroup::--group:pubs:r-x
#effective:r-group:admin:rwx
#effective:rwmask::rwother::---

, , , ,
, ,
.
$ chmod 770 new
$ ls -l new
-rwxrwx---+ 1 max max 0 Feb 13 00:39 new
$ getfacl --omit new
user::rwx
group::--group:pubs:r-x
group:admin:rwx
mask::rwx
other::---

.
vim, touch, cp .
. ,
,
.

, -

127

.
, .
correspond

personal

memos

business

to_do

to_do

to_do

to_do

personal

memos

busines
businesss

. 4.10.

,
.
, , .
,
.
,
. , . , ,
.4.2, to_do , personal, memos business.
,
to_do.
, . , to_do memos memos to_do.
.4.10.
,
. ,
, ,
.


: () .
.
, , i-
, .

128 4.

, , , - , . , .
ln:

ln (link ) ( s --symbolic) , :
ln _ _

, Zach (.4.11)
/home/max/letter draft
Zach:
$ pwd
/home/zach
$ ln draft /home/max/letter

/home/max letter. Max , Zach


. , /home/max/letter ,
Max, Zach , .
ln ,
. , ,
, , ,
. , , Zach /home/zach/
draft, Max /home/max/letter.
/

home

max

zach

memo

/home/max/letter

planning

/home/zach /draft

. 4.11. : /home/max/letter /home/zach/draft


cp ln

, ln .
, ln ,
:
$ cat file_a
A.

129

$ ln file_a file_b
$ cat file_b
A.
$ vim file_b

$ cat file_b
B .
$ cat file_a
B .

ln cp
, . , :
$ cat file_c
C.
$ cp file_c file_d
$ cat file_d
C.
$ vim file_d

$ cat file_d
D .
$ cat file_c
C.

ls l, ,
, ,
,
. 2
( max) , file_a file_b ( ):

ls

$ ls -l file_a file_b
-rw-r--r-- 2 max pubs
-rw-r--r-- 2 max pubs
-rw-r--r-- 1 max pubs
-rw-r--r-- 1 max pubs

file_c file_d
33 May 24 10:52
33 May 24 10:52
16 May 24 10:55
33 May 24 10:57

file_a
file_b
file_c
file_d

, ,
ls .
, ,
ls i.
i- . I- .
(HFS+, , Mac OS X , i-,
, , i- .)
i-, ,
. ,
i-, ,
. , file_a file_b
i, file_c file_d i-:

ls
i-

$ ls -i file_a file_b file_c file_d


3534 file_a
3534 file_b
5800 file_c

7328 file_d

:
, . ,
- .
, , , , ,
, .

130 4.


Linux , (soft links symlinks).
( , i-),
( , ,
, ).
- ,
. ,
.
Linux
. (
i
- ) , , , , .
, ,
, , ( ) . , , .
. , ,
.
,
, .
. ,
, .o,
C make,
, .

.
, , .
, ,
, . .
,
. .

ln:
ln --symbolic ( s).
/tmp/s3 sum,

Max
. ls l, ls ,
. l (link ).
$ ln --symbolic /home/max/sum /tmp/s3
$ ls -l /home/max/sum /tmp/s3

131
-rw-rw-r-1 max max 38 Jun 12 09:51 /home/max/sum
lrwxrwxrwx
1 max max 14 Jun 12 09:52 /tmp/s3 -> /home/max/sum
$ cat /tmp/s3
sum.

.
, .


,
. , ,
, , ,
( , ).
sum /tmp.
, cat :
$ pwd
/home/max
$ ln --symbolic sum /tmp/s4
$ ls -l sum /tmp/s4
lrwxrwxrwx
1 max
max
3 Jun 12 10:13 /tmp/s4 -> sum
-rw-rw-r-1 max
max
38 Jun 12 09:51 sum
$ cat /tmp/s4
cat: /tmp/s4: No such file or directory
( )

cd
cd
, , , .
cd ,
(. 5, ) , pwd

. pwd (/bin/pwd) ,
, , , .
$ ln -s /home/max/grades /tmp/grades.old
$ pwd
/home/max
$ cd /tmp/grades.old
$ pwd
/tmp/grades.old
$ /bin/pwd
/home/max/grades

, ,
:
$ cd ..
$ pwd
/tmp
$ /bin/pwd
/tmp

Mac OS X /tmp /private/tmp.


OS X, cd .. ,
/private/tmp.

132 4.
ln .
-symbolic, ln , ,
, ,
.

rm:
.
, Linux, , rm.
, , , , . ,
.
, - . DOS Windows, Linux
.
,
.
. cat , , :
$ ls -l sum
-rw-r--r-- 1 max pubs 981 May 24 11:05 sum
$ ln -s sum total
$ rm sum
$ cat total
cat: total: No such file or directory
$ ls -l total
lrwxrwxrwx 1 max pubs 6 May 24 11:09 total -> sum

,
. ,
:
$ rm total

Linux ,
, .

. ,
, ,
. ,
(/).
Linux
255. , . .

133

.
cd
.

, .
, , , , ,
, , .
, , .
,
.
Linux , /usr/bin,
,
Linux, /dev, ,
. /etc/passwd,
,
.
, , . , .
: ,
.
: , . ls l.
.
, root

-, chmod .
, ,
, .
(
ACL
-)
Linux. ACL-

. ACL

- .
, ,
. , , , ,
. I- ,
,
. i-.
.
,
.

134 4.
, ,

,
. ,
.
.4.2 , .
4.2. , 4

cd

chmod

getfacl

ACL-

ln

mkdir

pwd

rmdir

setfacl

ACL-

1. , ?
1) milk_co
2) correspond/business/milk_co
3) /home/max
4) /home/max/literature/promo
5) ..
6) letter.0610
2. , :
1) .
2) .
3. , /home/max,
literature, , classics literature.
, classics .
4. df , . df h (human-readable
), :
1) Linux?
2) ?
3) , 4.1
, ,

135

5.

6.

7.

8.

9.

10.

. ?
?
, , ,
. , ?
/etc/passwd.
cat less, /etc/
passwd. , /etc/passwd,
.
1) /etc/passwd?
2) ?
3) ?
4) ? (: .)
5) /etc/passwd . x, ,
, - .
?
/home/zach/draft /home/max/letter
, , letter?
1) Max vim letter.
2) Zach vim draft.
3) Zach 31 2009 ,
vim.
4) Max 1 2009 ,
vim.
, ,
jobs_list, , ,
. , , ,
jobs_list -.
-, ?
- , ,
, ? - , ? .
, , .4.2,
:
d--x--x---

3 zach pubs 512 Mar 10 15:16 business

drwxr-xr-x

2 zach pubs 512 Mar 10 15:16 business/milk_co

, ,
? ,
correspond cheese_co.
1) cd correspond/business/milk_co
2) ls l correspond/business
3) cat correspond/business/cheese_co

136 4.


1. i-? i-
?
2. ..? (/) ?
3. i? ?
i?
4. , andor.
, ?
$ mv andor and\/or

, ?
5. ls i , i- (.
ls


i
- . 129). i

, i-, , . (:
.)
6. , , ? ?
7. ? , , ln? .
8. , :
$ ls -l
total 1
drwxrwxr-x
2 max pubs 1024 Mar 2 17:57 dirtmp
$ ls dirtmp
$ rmdir dirtmp
rmdir: dirtmp: Directory not empty

$ rm dirtmp/*
rm: No match.

5.

.
,
. , , .
, .
,
, Bourne Again (bash) TC (tcsh). : ,
, , ,
. , ,
8 (bash) 9 (tcsh).

10 bash.


, . , ls, () ls.
, , , . , , , .
,
, , .


. ( )
,
. , :
[1][2] [n]
. , 1n ,
, .
, . ( :
, ,

138 5.
.) ,
(, ).


Linux
. , ls
.

, , , .


. , , ,
, . ,
vim emacs , .
cp, temp
tempcopy:
$ cp temp tempcopy

, ,
. cp , temp ,
tempcopy . cp
(. V).
, , cp.
: , . , cp
. , cp,
.

, . , .
,
, () , .
, ,
, .
, .
, . GNU . , help ( )
.
5.1 , ls . ls
, .

139
r (reverse order ; GNU-,
reverse), ls
, - .
x ls .
5.1.

$ ls
hold
mark names
oldstuff temp zach
house max
office personal test
$ ls -r
zach temp
oldstuff names mark
hold
test personal office
max
house
$ ls -x
hold
house
mark max
names office
oldstuff personal temp test zach
$ ls -rx
zach
test temp personal oldstuff office
names max
mark house
hold


, ; .
, , .
.
5.1 , r x, ls.
, , . ls xr
, ls rx. ls x r .

, . , gcc o,
, ,
.
:

$ gcc -o prog prog.c

: H
, , .
, , ,
, .
h ( human-readable )
-, - . df h (disk
free ) ls lh.

, , .
l, :
$ ls -l

140 5.
, , , l.
.
, . ,
, -- ( ) (
). ,
:
$ ls -- -l

, , , ,
:
$ ls ./-l

, /home/max,
:
$ ls /home/max/-l

:
$ ls -l -- -l

,
(, find).
,
. , ,
Linux.
xargs (. V),
.


Linux tty ( Linux) , ,
- . CONTROL+H ( ) CONTROL+U ( ) .
( )
.
CONTROL+W ( ). ,
. , .
,
, ()
(.5.1). .
, ( ).

141

( ),
. ( )
, .
, ls :
$ ls
$ /bin/ls

--HELP
--help (
). , GNU (.
1), . :
$ bzip2 --help
bzip2, a block-sorting file compressor. Version 1.0.5, 10-Dec-2007.
usage: bunzip2 [flags and input files in any order]
-h --help
print this message
-d --decompress
force decompression
-z --compress
force compression
-k --keep
keep (don't delete) input files
-f --force
overwrite existing output files

If invoked as 'bzip2', default action is to compress.


as 'bunzip2', default action is to decompress.
as 'bzcat', default action is to decompress to stdout.

. 5.1.

142 5.

, .
:
$ >bb <aa cat

cat , aa,
,
bb
. (. . 146), ,
,
. , (,, ) .

, (
, ), ( /bin/ls /bin)
, .
, ,
, ,
. , , PATH. PATH 8 9 ( bash tcsh ), PATH:
, . 312. which whereis 3.
Bourne Again Shell (bash) ,
:

$ abc
bash: abc: command not found

, ,
PATH. bash
PATH (.):
$ PATH=$PATH:.

PATH
(. ). , (, - ,
), :
$ def
bash: ./def: Permission denied

lsl:
. 115.
chmod: . 116.

./
,
./. , myprog


, ,
, PATH:

$ ./myprog

143

. 5.2. ,


,
, . Linux (. 8). , .
, .
, (. $?:
. 458) . (), .

,
,
, ( )
.
.
.



. . 50, bash 8,
. 326 tcsh 9, . 381.



, , . , ,
(.5.2). ,
. ,
1, , .
,
, , .

144 5.
, . ,
, , . ,
, ,
( ).
, : .
8
(bash), . 288,
9 (tcsh).
, , , ( ),
, ,
. ,
, .
, ,
, . , ls ,
, .
, (a tty), ls isatty().
, ls
, . ,
. ls , ,
less.


4 , ,
. Linux OS X : .
Linux, /dev,
, , .
, who
. , who
pts/4, /dev/pts/4.
.
, ,
tty. ,
, . , , , .



,
, (.5.3).
, . -

145

,
,
, .
cat ,

cat
. . 5.3.
, cat
,
.


cat
, ( ) .
( cat ),
. , cat
.
, cat,
cat . , ,
. cat.
cat ,
, , cat () ( ).
5.2.

cat

5.2. utility

$ cat
.
.
Cat
Cat
, CONTROL+D
, CONTROL+D
.
.
CONTROL+D
$

cat , , CONTROL+D. EOF (end of file )


cat, ,
. cat
, .

CONTROL+D

EOF

146 5.

, .
.
, .
,
.


(>) , (.5.4). , , :
[] > _

. 5.4.


(, ), , _
, .
.5.5 cat.
.5.4,
. . 5.4 - .
, ,
cat sample.txt,
.
, .5.4, sample.txt
. cat sample.
txt . cat .

. 5.5.

5.3 , cat
.
,
. -

147

.
.
5.3. cat

$ cat > sample.txt


,
cat .
CONTROL+D, .
CONTROL+D
$
I

. ,
, .
II . 149.

5.4 cat
(catenate),
, , , cat. : stationery, tape
pens. cat .
cat . supply_orders.
cat , supply_orders
.
5.4. cat

$ cat stationery
2,000 sheets letterhead ordered:

10/7/08

$ cat tape
1 box masking tape ordered:
5 boxes filament tape ordered:

10/14/08
10/28/08

$ cat pens
12 doz. black pens ordered:

10/4/08

$ cat stationery tape pens > supply_orders


$ cat supply_orders
2,000 sheets letterhead ordered:
1 box masking tape ordered:
5 boxes filament tape ordered:
12 doz. black pens ordered:
$

10/7/08
10/14/08
10/28/08
10/4/08


, .
(<)
, , (. .5.5).
:
[] < _

148 5.

(, ), , _
, .
5.5 cat,
supply_orders, , 5.4, . ,
EOF.
cat
,
. cat Linux-,
. lpr,
sort, grep Perl. ,
. , .
, . ,
.
,

5.5. cat

$ cat < supply_orders


2,000 sheets letterhead ordered:
1 box masking tape ordered:
5 boxes filament tape ordered:
12 doz. black pens ordered:

10/7/08
10/14/08
10/28/08
10/4/08

noclobber:
noclobber, . bash
, noclobber set o
noclobber. +o noclobber.
tcsh set
noclobber unset noclobber. noclobber
,
. ,
touch, noclobber, echo ,
noclobber :
.
Bash

$ touch tmp

$ set -o noclobber
$ echo "hi there" > tmp
bash: tmp: cannot overwrite existing file

$ set +o noclobber
$ echo "hi there" > tmp
$

149
tcsh $ touch tmp

Tcsh

tcsh
tcsh
tmp:
tcsh
tcsh
$

$ set noclobber
$ echo "hi there" > tmp
File exists.
$ unset noclobber
$ echo "hi there" > tmp

II

, ,
, :
$ cat orange pear > orange
cat: orange: input file is output file
( )
cat

,
orange. orange
, pear, ,
(>),
orange.
,
cat

, mv


:
$ cat orange pear > temp
$ mv temp orange
, , .
apple a, b c,
grep a.output. , a output,
:
$ grep apple a b c > a output
grep: output: No such file or directory
( )
a, grep.
, -
.
, , , a
.

noclobber , ( tcsh ) (>|).


, date.
noclobber
. .
,
.
$ date > tmp2
$ set -o noclobber
$ date > tmp2
bash: tmp2: cannot overwrite existing file

$ date >| tmp2
$

150 5.
noclobber tcsh , . 395.


(>> )
, .
.
.
, :
$ cat orange
orange
$ cat pear >> orange
$ cat orange
orange
pear

orange.
pear orange. cat
.
NOCLOBBER

,

, , .
noclobber
, orange. noclobber
, .

noclobber
,
cp mv. i (interactive ),
.
.
cp . 70.

, ,
( date), ,
( who). 5.6
date whoson.
cat .
who whoson. cat , .
5.6.

$ date > whoson


$ cat whoson
Fri Mar 27 14:31:18 PST 2009
$ who >> whoson
$ cat whoson
Fri Mar 27 14:31:18 PST 2009
sam
console
Mar 27 05:00(:0)

151
max
max
zach

pts/4
pts/5
pts/7

Mar 27 12:23(:0.0)
Mar 27 12:33(:0.0)
Mar 26 08:45 (bravo.example.com)

/dev/null:
/dev/null ,
. /dev/null ,
, :
$ echo "hi there" > /dev/null
$

/dev/null .
cat messages ,
:
$ ls -l messages
-rw-r--r-1 max pubs 25315 Oct 24 10:55 messages
$ cat /dev/null > messages
$ ls -l messages
-rw-r--r-1 max pubs 0 Oct 24 11:02 messages


. ( )
,

.
. (|).
, , :
_a [] | _b []

,
:
_a []> temp
_b []< temp
rm temp


_ temp. _b ,
temp. , temp.
, , , , .
Linux, ,
. , . , tr
tr

152 5.
(translate , . V) . tr
tr 1 2

tr , 1. ,
1 , 2. (
1 2 . .) tr
.
tr abstract, a, b c,
A, B C:
$ cat abstract | tr abc ABC
$ tr abc ABC < abstract

tr ,
, ,
.
lpr (line printer , .V) , . lpt , .
, lpr
.
lpr. , 5.7, , ls lpr
(temp) , .
temp , .
(
temp ).
lpr

5.7.

$ ls > temp
$ lpr temp
$ rm temp

$ ls | lpr

, 5.8, who
temp, . sort (. sort:
. 74) ,
, , , .
. sort, 5.8,
, (<)
temp. , sort ,
, .

153
5.8.

$ who > temp


$ sort < temp
max
pts/4
max
pts/5
sam
console
zach
pts/7
$ rm temp

Mar
Mar
Mar
Mar

24
24
24
23

12:23
12:33
05:00
08:45

sort , ,
< 5.8 .
5.9 ,
temp.
who sort. sort
,
.
,
,
who grep (.
grep
: . 72), . grep ( ), ,
sam:
$ who | grep 'sam'
sam
console

Mar 24 05:00

, ,
, (, ), less more (. less more:
. 68).
$ ls | less

less . ,
. h,
q.
5.9.

$ who | sort
max
pts/4
max
pts/5
sam
console
zach
pts/7

Mar
Mar
Mar
Mar

24
24
24
23

12:23
12:33
05:00
08:45

, . , , ,
-
.

154 5.
.
.
sort ,
who
lpr.

who :
$ who | sort | lpr

Linux. : who, sort lpr, ,


. ,

.

tee:
tee (.

V
) , . (tee ):
. 5.10 who
tee. tee
who.out, .
tee
grep, , sam.
tee , ,
a (append ).
5.10. tee

$ who | tee who.out | grep sam


sam
console
Mar 24
$ cat who.out
sam
console
Mar 24
max
pts/4
Mar 24
max
pts/5
Mar 24
zach
pts/7
Mar 23

05:00
05:00
12:23
12:33
08:45


.
,
, , . , ,
.

155

,
.
, . ,
Linux.
, . , . ,
(GUI) .

,
, ,
(&). .
PID (process identification number),
, . , .
, . , , , ,
, ,
.
. ls lpr,
.

,
PID-

$ ls -l | lpr &
[1] 22092
$

[1], , , 1. 22092 PID-


. ( TC Shell PID- ,
.) ,
[1]+ Done

ls -l | lpr

( ls l, - ls color=always l.
, ls (.
) ls color=always . 341.)


( ), ,
CONTROL+Z.
.
, bg,
. ,

CONTROL+Z

156 5.
.
. ,
, . , fg .
, fg
(%), ,
. , ( promptme),
, :
bash $ fg 1
promptme

, ( ), , .
. 299. ,
| & , . 297.

kill:
( CONTROL+C) ; kill,
.V. kill PID , , ,
.
PID-, ps (process status , .V).
tail f outfile ( f (follow )
tail outfile,
), ps PID- , kill:

PID-


ps

$ tail -f outfile &


[1] 18228
$ ps | grep tail
18228 pts/4
00:00:00 tail
$ kill 18228
[1]+ Terminated
$

tail -f outfile

,
jobs.
, , , PID- . , , , ,
kill.


jobs

157
$ tail -f outfile &
[1] 18236
$ bigjob &
[2] 18237
$ jobs
[1]- Running
[2]+ Running
$ kill %1
$ RETURN
[1]- Terminated
$

tail -f outfile &


bigjob &
tail -f outfile


, , ,
, . -, .
,
, . , , , - . , , .
, .
, .
, , ,
, .

?
(?) , .
. lpr:
$ lpr memo?

memo? ,
, memo, . lpr.
, ,
. ,
,
(memo?), lpr , , (.
nullglob .8.13 . 349).

158 5.
ls,
, , ,
memo?:
$ ls
mem
memo12
memo memo5
$ ls memo?
memo5 memo9

memo9
memoa

memomax
memos

memoa

memos

newmemo5

memo? mem, memo, memo12,


memomax newmemo5.
:
$ ls
7may4report may4report
mayqreport may_report
may14report may4report.79 mayreport
may.report
$ ls may?report
may.report may4report may_report mayqreport

,
echo ls. echo , :
$ echo may?report
may.report may4report may_report mayqreport

,
, may?report.
echo,
. echo .
( ,
, .
.102). ,
,
.

*
(*) , , , ,
.
, , , memo, mo sam:
$ ls
amemo memo.0612
memosally
memsam
user.memo
mem
memoa
memosam.0620 sallymemo
memo
memorandum memosam.keep typescript
$ echo memo*
memo memo.0612 memoa memorandum memosally memosam.0620 memosam.keep
$ echo *mo
amemo memo sallymemo user.memo
$ echo *sam*
memosam.0620 memosam.keep memsam

159

memo* amemo, mem,


sallymemo user.memo. ?,
.
a ls .
echo * . ( ), .. ( , ), .aaa
.profile. echo .* :

$ ls
aaa
memo.sally sally.0612 thurs
memo.0612 report
saturday
$ ls -a
.
.aaa
aaa
memo.sally sally.0612 thurs
.. .profile memo.0612 report
saturday
$ echo *
aaa memo.0612 memo.sally report sally.0612 saturday thurs
$ echo .*
. .. .aaa .profile

.p* memo.0612,
private, reminder report. ls .* ls .private
.profile . ( ) .. (
).
echo ( ) ,
(.), .
$ ls -a
.
.private
memo.0612 reminder
..
.profile
private
report
$ echo .p*
.private .profile
$ ls .*
.private .profile
.:
memo.0612 private
reminder
report
..:
.
.
$ echo .*
. .. .private .profile

, . ,
.txt,
*.txt.
.
(&) lpr .
$ lpr *.txt &

160 5.

[ ]
[ ], , , . memo? memo, ,
memo[17a] :
memo1, memo7 memoa. , , . ( GNU
, GNU .)
, ,
,
.
.

. , .

, a, e, i, o u.
page2.txt, page4.txt, page6.txt page8.txt.
$ echo [aeiou]*

$ less page[2468].txt


. , [69] [6789],
[az] ,
[azAZ] ,
.
part0, part1, part2, part3 part5. lpr :
$ lpr part0 part1 part2 part3 part5
$ lpr part[01235]
$ lpr part[0-35]

.
,
.
, , part , .
0, 1, 2, 3 5.
, 03 5.
39, part0 part38:
$ lpr part[0-9] part[12][0-9] part3[0-8]

,
a m. , x, y z.

161
$ echo [a-m]*

$ echo *[x-z]

([) (!)
(^), , ,
, , .
[^tsq]* , t, s q.
, *[^ab] ,
a
b
, [^
b
-
d
]* ,

b, c d.
$ ls
aa ab ac ad ba bb bc bd cc dd
$ ls *[^ab]
ac ad bc bd cc dd
$ ls [^b-d]*
aa ab ac ad

() (]), .

, ls . ls ?old.
?old , , ls. , ,
(?) (.
. 65).
, ls. , , ,
, ?old (
).
$ ls ?old
hold
$ ls \?old
ls: ?old: No such file or directory

, , ls , .


, , ,
. (ls, cat, echo, lpr)
.
.


( ),
. -

162 5.
.
. , , .
, ,
, ,
.
echo, , , . , .
. 465,
, bash, .10.8 . 484,
tcsh .9.10 . 407.
, bash,
info bash Shell Builtin Commands.
Bourne Shell Builtins () Bash Builtins.
, info, 2. Info-, bash,
bash-doc, , ,
man- ( info).
bash GNU, info-
, man-. , man-,
man- builtins.

,


bash


tcsh

tcsh, man tcsh man-, tcsh,


: /Builtin commands$ (
).


Linux
. , , . ,
.. .
.
Linux
.
,
. ,
. , ,
. ,
, PATH,
.

163


, . , .

. ( )
.
, , ,
.
, , ,
. (&), . ,
.
jobs , , . .
, . (?) ,
(*) .
, , .
, - ( ),
,
.
, .
.
, . , , ,
.

,

.5.1 , .
5.1.

Tr

Tee

Bg

Fg

Jobs

164 5.

1. ? ,
?
2. sort , :
$ sort list > temp
$ lpr temp
$ rm temp

3. PID-? ?
PID- ?
4. , :
$ ls
intro
notesa

notesb
ref1

ref2
ref3

section1
section2

section3
section4a

section4b
sentrev

,
.
1) , section.
2) , section1, section2 section3.
3) intro.
4) section1, section3, ref1 ref3.
5. V man-, , :
1) , a A.
2) ,
() $(.
3) .
4) ,
.
6. :
1) sort phone_list. ,
numbers;
2) [ { (,
] } ) permdemos.c. (:
tr V.)
3) book, : part1 part2.
7. lpr sort , ,
.
1) , .
2) , .
8. , grep:
1) , ;
2) ;
3) ;
4) .
grep ?

165
9. .
ls?
$ ls
abc abd abe abf abg abh
$ rm abc ab*
rm: cannot remove 'abc': No such file or directory
rm: 'abc':


1. (>) (
) . .
2. , Max

PATH. , PATH. ,
, .
PATH ?
3. , ,
.
1) , , .
2) , ,
.
4. , ,
, CONTROL,
?
5. noclobber
cp mv?
6. , , ?
, ?
? ( ,
. ,
.)
7. answer :
$ > answers.0102 < answer cat
, .
?

II

6. vim . . . . . . . . . . . . . . . . . . . . . . . 168
7. emacs . . . . . . . . . . . . . . . . . . . . . 220

6. vim
vi,
, , - , ,
- .
vim ( vi ,
Linux),
.
vi . vim- vim .
vim-.

vi UNIX ed ( Linux-), ,
. ex1
ed. ex
ed
, , . ex
vi (Visual mode , ).
,
ex,
, ex,
vi. , vi. ,
ex, , vi
ex. ex, vi
, , , Q, ex. quit
ex.
Linux ()
vi. elvis (elvis.thelittle-red-haired-girl.org), nvi ( vi, www.bostic.com/vi), vile
(invisible-island.net/vile/vile.html) vim (www.vim.org). ,
vi,
.
, , vim.
Linux vim. , Red

vi

ex vi,
vim.

. vim 169

Hat /bin/vi, , vim,


, , /usr/bin/vim, vim.
vim vi ,
, . vim
vi, ,
Windows, Macintosh, OS/2, UNIX Linux.
-, vim (www.vim.org),
.
vim .
OpenOffice.org Writer,
. , vim ,
(C,HTML, Java ..),
, groff troff. , vim, fmt (.V).

vim

vim , . vim , .
vim
. , . , , , .
,
vim, .

. vim

, vim, ,
, , vim. vim
.

VIMTUTOR VIM HELP,



vimtutor ,
, vim-runtime vim-enhanced.

,
, vim, vimtutor. ,
.

vimtutor

170 6. vim
vim ,
, , (
) . , , .

vim
vim ,
practice:
$ vim practice

vim ,
.6.1.

. 6.1. vim

(~) , .
.
, . 6.1, , ,
.
vim ,
(terminfo), vim
ansi. .
vt100 vg100:
E558: Terminal entry not found in terminfo
terminfo
'vg100' not known. Available builtin terminals are:
vg100 . :
builtin_ansi
builtin_xterm
builtin_iris-ansi

. vim 171
builtin_dumb
defaulting to 'ansi'
'ansi'

VIM VI
Linux- vi vim ,
vi (. compatible . 177).

ESCAPE,
vim

:
:q!

(: ) vim .
q! vim ,
. ( vim , , ,
, .)
. ,
vim .

. 6.2. vim

vim ( ), vim , , ,
(.6.2).
practice , .
vim , , .

6.1 ( ) , ,
. vim

.

172 6. vim


vim (
) (.6.3). vim
. , vim. vim . vim ,
, . vim ,
ESCAPE. vim ,
: ,
INSERT (). vim INSERT,
CAPS LOCK.
vim , , :
:set number RETURN
(:)

Insert ,
Appe nd,
Open,
Replace,
Change

Last

Li ne

mode

ESCAPE

Input

mode

. 6.3. vim

(:), , vim . vim


.
vim
. , :set nonumber
.


, vim . i (
) a (
).
, vim , ESCAPE,
vim , ,
, ,
. i a , vim .
i a
( )

. vim 173

VIM
vim , .
,
vim

,
, .
CAPS LOCK (SHIFTLOCK). , ,
, , vim
.
,
vim
,
.

vim , , . , ,
.
, ,
.6.4, . ,
, vim ,
.
() ,
. vim .
, ,
(. . 180),
. ,
ESCAPE, .

. 6.4. vim


vim, vim-runtime.
vim :help [],
( ).
.
:help, vim vim (.6.5).
, , , .6.5,

174 6. vim
vim. vim-
, . .6.5
help.txt ( vim-). (practice)
( vim-).
, . , j
; ,
, CONTROL+D CONTROL+U.
, :q.

. 6.5. (Help) vim

,
:help insert (.6.6).

. 6.6. (insert)

. vim 175


, ,
vim. , ( CONTROL+H, CONTROL+U CONTROL+W). vim ,
.


, , .
vim , , .
,
, h, j, k l ( l )
, , .


x
(
)
dw
(
)
dd
(
)

, , ,
, x. ,
,
dw (
Delete

word
). , ,
dd.


, , , u (Undo
) , . vim
, .
u , vim , , .
.
compatible (, . compatible
. 177) vim .
u
()

, , (Redo):
CONTROL+R :redo (
). vim
. (Undo),
Redo . compatible (, . compatible) vim
.
:redo
(

176 6. vim


,
, . i (Insert ),
vim ,
ESCAPE .
, ,
a (Append ).
i
()
a
()


, .
o (Open ). vim ,
,
. , .
, ESCAPE, . O , o,
, .
o
O ()


dd, dw x,
, i, a, o O,
.
, pressing hitting (. .6.4)
, p
pressing, dw, pressing.
vim , i, hitting, ESCAPE. , vim
.
, dw, i, cw (Change
word ). cw vim .


CONTROL+L , ,
. ,
CONTROL+L . ^L , CONTROL+L
CONTROL+V.


vim , . ,
, .
vim ZZ ( Z ),
. ZZ vim

vim 177

. , :q!.
ZZ CONTROL+Z

vim ZZ ZZ,
CONTROL+Z ( ). CONTROL+Z
vim
, .
vim

, .
. 299.
vim, vim
(.6.7).

compatible
compatible () vim
vi. .
vi, .
compatible vim.
, compatible , (
Undo
) (.
(u U) . 189). : compatible ,
.
compatible , . compatible
:help compatible .
vim vi :help vi-diff .
help . 173.
compatible,
C, , N. compatible vim
vim . 203.

vim
, ,
, vim. ,
, vim,
version.


, vim .
, :help . ,
, |tutor|.
: -

178 6. vim
CONTROL+], .
, CONTROL+o (
o ).
. , |credits| :help
credits , credits.
, :q!.
insert (), delete () opening-window (
). ,
vim. :help doc-file-list.
vim , , ,
, ( :help iccf).

, .

, .

( ) .
.
.


vim ex :
ex.
ex.
vim.
vim.
vim.
, vim
, . , , . , vim
, , ,
. , (:),
vim .
, .
,
.
. ,
, .

vim 179

ex . , vim,
ex.
vim .
, (Insert) (Append), vim
. ESCAPE vim
.
(Change) (Replace) . (Change) ,
, vim , .
(Replace) () ,
( ). .6.3
.

CAPS LOCK
, , vim - .
, vim

,
, .
vim showmode (
, (.6.7), ,
. :set
laststatus=2 (. .6.7).
CAPS LOCK.
, .
- vim ,
.


, vim .


vim . ,
. vim , .


. vim
, @. , ,
, .
, , ,
. ,
ESCAPE, vim ,
CONTROL+L, () .

180 6. vim

(~)
, vim , (~) .
, vim
( ).


vim , .
CONTROL+W.


vim . vim .
,
,
. , , vim , .
vim
, .
.
, . ,
- (, ), .
vim,
, view:
$ view filename

view vim R (readonly


).
,
.
. mc (Midnight Commander), view
vim, mcview.


vim ,
( , ), .
.

vim 181

vim , ,
.
CONTROL+W, -
. , CONTROL+W s (
), . CONTROL+W n
. CONTROL+W w
, CONTROL+W q ( :q) (
). ,
:help windows.


vim
, . vim , , .6.7.
: ,
- (, ,
), , vim .

. 6.7.

, vim,
.
.

182 6. vim


: vim , , . vim, ZZ :wq (. . 176).
,
:
:q!

:q! . , vim , ,
.
, .
,
, vim . ,
vim, ZZ (No file name). vim
, Write(:w),
vim.
, _
( ):
:w _

(Write) :q ,
vim. (
q!), ,
- .
(Write) . 202.


, :w _
. ZZ,
, .
(Write), , .
, vim .
,
() . (, Max
:w /home/max/tempor :w ~/temp.)
vim (File exists),
:w! _, (
). . w! . 203.


vim , , .
,
. ,

vim 183

, , , . 6.7. - ,
.
Max r,
memo, :

$ vim -r
Swap files found:
:
In current directory:
:
1.
.party.swp
owned by: max
dated: Sat Jan 26 11:36:44 2008
file name: ~max/party
modified: YES
user name: max
host name: coffee
process ID: 18439
2.
.memo.swp
owned by: max
dated: Mon Mar 23 17:14:05 2009
file name: ~max/memo
modified: no
user name: max
host name: coffee
process ID: 27733 (still running)
In directory ~/tmp:
~/tmp:
-- none -In directory /var/tmp:
/var/tmp:
-- none -In directory /tmp:
/tmp:
-- none --

. 6.8.

r vim ( ). ,

, . . :w
_, ,
. ,
, . ,

184 6. vim
Max vim memo.
:
$ vim -r memo
Using swap file ".memo.swp"
".memo.swp"
Original file "~/memo"
"~/memo"
Recovery completed. You should check if everything is OK.
. , .
(You might want to write out this file under another name
(
and run diff with the original file to check for changes)
diff )
Delete the .swp file afterwards.
.swp
Hit ENTER or type command to continue
,
:w memo2
:q
$ rm .memo.swp

,

vim

,
. ,
r , .

:
vim , ,
.
.
, .
. .6.8,
, ,
,
. ,
(), (). ,
, , ,
.
, ,
, .
vim ( nowrap, . .6.7).
, ,
(, , , , ).
, , .
. 211.

: 185


l h
, . l ( l )
(.6.9) . , 7
7l .
.
h , l ,
.

. 6.9.


(Find) , , .
, a, :
f F

fa

, F.
a
:
Fa

(;) (Find).

fm Page 166 Wednesday, September 30, 2009 10:13 AM

w (word ) ,
(.6.10).
. ,
. 15w
Chapter 6 The vim Editor
.
w
W

belief,.really...It
. 6.10.
Figure
6-10
Moving the
cursor
by
words

oving the Cursor by Words


w/W

The w (word) key moves the cursor forward to the first letter of the next word

oving the Cursor by Words


The w (word) key moves the cursor forward to the first letter of the next word
(Figure
6-10).6.Groups
of vim
punctuation count as words. This command goes to the
186

next line if the next word is located there. The command 15w moves the cursor to
the first character of the fifteenth subsequent word.
W w, ,
The W key ,
is similar to
the w key but
moves the cursor
by
blank-delimited

(.words,

including
punctuation,
as
it
skips
forward.
(Refer
to
Blank-Delimited
Word
on
, . 212.)
page 194.)
b (back )
b
b/B
key moves the.
cursor
backwardBto
the first letter
of the
previous
word.
B The b (back)

,
e/E

The Be key moves


the
cursor
backward
by
blank-delimited
words.
Similarly
the e
. , e
E key moves the cursor to the end of the next word; E moves it to the end of the next
, E
blank-delimited
word. , .

w/W

oving
the Cursor
by Lines

j/k The RETURN key moves the cursor to the beginning of the next line; the j and DOWN ARROW
j k
, -

keys move the cursor down one line to the character just below the current character
j , , (Figure 6-11). If no character appears immediately below the current character, the
(.6.11).
cursor moves to the end of the next line. The cursor will not move past the last line
,
of text in the work buffer.
. .
keys
are similar
to the
j and DOWN ARROW
keys but
in the
oppoThe k andkUPARROW

j work

,
RETURN
key
but
works
in
the
opposite
site
direction.
The
minus
()
key
is
similar
to
the
. ()
direction.

, .

needed.as

with.their
RETURN

working..To
.
6.11.

by

Figure
6-11
Moving the
cursor
lines


) } (.6.12).
( { .
. 213.
) (
} {


H (home )
, M (middle )
, L (lower ) (.6.12).
H,
ML

: 187

L
}
. 6.12. , H, M L


, . , , , ,
, . ,
.
() , vim
- ,
CONTROL+D. () , CONTROL+U.
, vim
CONTROL+D CONTROL+U ( ).
scroll () .6.7.

CONTROL+D
CONTROL+U

CONTROL+F (forward ) CONTROL+B (backward


) ,
,
. CONTROL+F
CONTROL+B PAGE DOWN PAGE UP.

CONTROL+F
CONTROL+B

G (goto ), vim .
G , vim .
, , ,
. , vim ,
. 6.7 . 206.

(G)

188 6. vim


(Insert ), (Append), (Open), (Change)
(Replace) vim . vim , .
vim , ESCAPE. ,
vim , ,
. 6.7 ( ) . 207.

i (Insert ) vim . I
(.6.13). i I
.fm Page 168 Wednesday, September 30, 2009 10:13 AM
, , ,
, .
ESCAPE
vim . i I


8 Chapter
6 The vim
Editor
.

(i I)

This.is.a.line.of.text.
. 6.13.
I, i,Aa commands
A
Figure 6-13
The I,
i, a, and

ne numbers (G)
When you enter
a line number

followed by G (goto), vim positions the cursor on that


line in the Work buffer. If you press G without a number, vim positions the cursor on

a (Append


i,
the last line in
the Work
buffer. Line)
numbers are
implicit;
the file does
not need to

(.
.6.13).
have
actual
line
numbers
for
this
command
to
work.
Refer
to
Line
numbers
on
(a A)

.
page 187 if you want vim to display line numbers.

nput Mode

o (Open ) , ()
(o O)

vim

.

O
The Insert, Append,
Open,
Change,
and
Replace
commands
put vim
in Input
mode.
While
,
o

vim is
in this mode,
you can put
new
text.
into
the
Work buffer.
To return vim

mode
when
you


.
to Command
finish entering
text, press the
ESCAPE key. Refer to
Show mode on page 188 if you want vim to remind you when it is in Input mode
(it does by
default).

r R (Replace ) ()
. , (r R)
Insert (i/I) The i (Insert)
command
vim
in Input mode
and places the
text you.
enter before
puts

r,

the
current
character.
I command
places
text at the
beginning
of the
current
line

vimThe

(Figure
6-13).ESCAPE
Although

. the i and I commands sometimes overwrite text on the


screen, the characters in the Work buffer are not changed; only the display is
affected. The overwritten text is redisplayed when you press ESCAPE and vim returns

serting
Text

: 189

R ,
ESCAPE, vim .


, .
.

. , ,
, .6.7, . 206.


,
(Quote), CONTROL+V, , , vim . ,
CONTROL+L ( CONTROL+R), ;
, CONTROL+W,
; , CONTROL+M, ,
ESCAPE, .
, ,
CONTROL+V, .
CONTROL+V . ,
ESCAPE[2J , vim,
CONTROL+V ESCAPE[2J.
DEC VT-100 , .
, ( ESCAPE) , vim. 10.

CONTROL+V

:
,
. Undo () ,
.


u (Undo )
. , .
, , , ,
. compatible (. compatible . 177), vim
. U , , ,
.

(u U)

190 6. vim


x .
(. . 214),
, . X .

(x X)


d (Delete ) .
, d,
(. . 211). vim .

(d D)

DD
d : .
, dd

dd


(. . 214), .


. (;), dt;
( t (t T)
.193). , D
d$. .6.1 . ,
, dd,
.


,
xp. ,
ddp.
(Put)
(p P) . 200.

6.1.

dl

( x)

d0

d^

( )

Dw

d3w

Db

dW

dB

d7B

: 191

d)

d4)

d(

d}

d{

d7{

d/text

text

Dfc

c,

Dtc

d$

Dd

5dd

dL

dH

dG

d1G


c (Change )
, ,
. ,
. C
.
c , (. . 211), vim
. ESCAPE,
, , .
ESCAPE (
).
.6.2 . , .

(c C)

6.2.

cl

cw

C3w

cb

cW

192 6. vim
6.2 ()

cB

C7B

C$

C0

c)

C4)

C(

C}

C{

C7{

ctc

cc

5cc

DW CW
dw (). cw
, .


s S (Substitute ) (6.3). s vim .
, ESCAPE. S
, cc: . s
. s , ,
, s ( ,
C).

(s S)

6.3.

5s

, ,


(~) , . ,

193

. , 5~
, ( ),
, .


, , , . vim
.
, ,
, ,
.



f (Find ). . 185.

(f F)

.
t ,
. T , .
(;)
f, F, t T.
.
, d2fq
q .

(t T)


vim ,
, . (
) (/), ,
( ), .
vim . , .
vim . , vim
. (?), vim .
,
(\).

(/ ?)

194 6. vim


, ,
CONTROL+V (.
. 189).
( \ ).
.

N n
.
n , N , .
vim
, , . vim
.
, vim, , .
, , .6.7, . 207 . 206.

(n N)



vim ( ), (/)
(?), ,
. vim
.
vim , / ?.
, vim
,
. ,
, .
, vim .
vim incsearch.
, :set
incsearch, noincsearch.
compatible (. compatible . 177) vim .


, ,
. .
(^ $) ,
(\). nomagic. .6.7,
. 205.

195

( ),
. , /^the ,
the.
^

. , /!$ ,
, / $ , .

,
. , /l..e
line, followed, like, included, all memory
, l,
e. (\.).
.

. , /s\>
, s.
(\), ,
, \> ,
> .
\>

. ,
/\<The , The.
,
.
\<

,
,
. , /dis*m
di, s, m.
dim dism, dissm.
*

, ,
,
. , /dis[ck] disk disc.
.
(^), ,
, ,
, , . ,
, .
.6.4.
[]

6.4.

/and

and
: sand and standard slander andiron

196 6. vim
6.4 ()

/ \<and\>

and
: and

/^ The

, The
:
The
There

/^[0-9][0-9])

, ,
:
77)
01)
15)

/ \<[adr]

, a, d r
: apple drive road argument right

/^[A-Za-z]

,
:
, 7

, , ,


.
, , ,
/, ,
. ,
, .

:[g][]s/_/_[/]

, vim ,
, .


, .
,
.
, , , . , vim , ,
.6.7 . 206. , ,
, .
vim ,

197

. g (
), vim ,
. ( g ,
, ;
. . 197 ).
, ,
,
. ,
. .6.5 .
6.5.

77,100

77 100

1,.

.,$

1,$

/pine/

, pine

g/pine/

, pine

.,.+10

( 11)


s, , , . , s, _.
, , , , , ,
. _
.
_. , /, (.
. 194). (_ .) _
_.
_ ,
_, . _ - ,
, .
_, _. , (&)
_ , _.
_
. .6.6.

198 6. vim
6.6.

:s/bigger/biggest/

bigger
biggest
: biggerbiggest

:1,.s/Ch 1/Ch 2/g

Ch 1 Ch 2
:
Ch 1Ch 2
Ch 12Ch 22

:1,$s/ten/10/g

ten 10
:
ten10
oftenof10
tenant10ant

:g/chapter/s/ten/10/

ten 10 , chapter
:
chapter tenchapter 10
chapters will oftenchapters will of10

:%s/ \<ten\>/10/g

ten 10
: ten10

:.,.+10s/every/each/g

every
each
:
everyeach
everythingeachthing

:s/ \<short\>/ "&"/

short short
( )
: the shortest of the shortthe shortest of the short

_ , _.

, _, ,
_, g (global). c (check
), vim
, _. y
, q
, l (last ) , a (all )
, n (next ) .
_. ,
:/candle/s/wick/flame/

flame wick , candle.

199


:g/candle/s/wick/flame/


wick , candle,
:g/candle/s/wick/flame/g

wick
, candle.
_ , _
. , :/candle/s//lamp/
:/candle/s/candle/lamp/.


, -
.


J (Join ) , , , , , .
, vim .
() ,
, , .
Join
(J)


Status, CONTROL+G, ,
, ,
, ,
,
. :f. :

Status
(CONTROL+G)

"/usr/share/dict/words" [readonly] line 28501 of 98569 --28%-- col 1

.
. () ,
. , , d2w (
), . .
, . . .
( /),
( cw). n
, .
. , n,
.
.

200 6. vim

,
vim 26 , . ,
.

.
, .


vim , ,
( ) .
.


(y) (d),
, . vim . ,
-
, .
, . Y
.

(y Y)

, YY

D Y

, d , y .
, yy, dd.

D (. (d D) . 190 , Y.
D , Y ,
.


p P, .
, p
, P .
, P
, p .
.
, .

(p P)


(. :
. 189)
. , ,

, 201

,
.
,
dw, p.
vim .
dd, ,
, , P, vim
.

, .
26 . ,
. , vim
,
. vim
. ,
(, "kyy
k).
. -, ,
vim . -,
, vim
.
. ,

, .
, ,
, , :
ESCAPE
, . ( , .)
, "ayy ( "a2yy,
). ,
"ap,
.
, . ,
,
, .
, , National Standards
Institute, .


26 , vim 9 . .
vim ,
. "1, "2 ..
, vim-,
,

202 6. vim

, , "1p. , , : "1p .
, , (.),
.
: "1
("2). u (.) "2
"3 , .


, vim,
, . ZZ, ,
.
.

(ZZ)


.
,
, ( ,
).
0. :

(:r)

:[]r [_]

, , ,
. _
, ,
. _, vim .



. ,
, , .
, vim , .

. - ,
.
vim :q!, ,
.
:

(:w)

:[]w[!] [_]
:[]w>> _

203

.
, vim .
, , (.
. 196). , vim
. _
, . _, vim
.
, vim , w
(!).

( _) .
.
w!


(:f) ,
(CONTROL+G; . . 199). , _,
, :w _.


vim . ,
, , ,
.
. ,
vim.
~/.bash_profile ( bash), ~/.tcshrc ( tcsh),
vim ~/.vimrc.
, , , vim, .

vim
vim (:), set, (. . 205). ,
, .
:
:set incsearch

204 6. vim


bash ~/.bash_profile (.
8, .bash_profile . 285) ,
:

VIMINIT

export VIMINIT='set 1 2 '

1 2 ,
.6.7. VIMINIT , vim. vim
, , TC Shell
Linux 15
:
export VIMINIT='set ignorecase number shell=/bin/tcsh wrapmargin=15'

abbreviations,
:
export VIMINIT='set ic nu sh=/bin/tcsh wm=15'

tcsh ~/.tcshrc (. . 369) :


setenv VIMINIT 'set 1 2 '

1 2 .6.7.
, , ,
.

.vimrc
vim
~/.vimrc .
.vimrc vim
compatible (. compatible . 177). .vimrc
:
set 1 2

.vimrc, ,
VIMINIT:
$ cat ~/.vimrc
set ignorecase
set number
set shell=/bin/tcsh
set wrapmargin=15
$ cat ~/.vimrc
set ic nu sh=/bin/tcsh wm=15

, VIMINIT,
, .vimrc.

205

.6.7 vim.
:set , vim
. :set ,
, .
: , ( ), , no (, ). - :set [no]. ,
:set number ( :set nonumber).
, -
( ), :set shiftwidth=15.
, nu number, nonu
nonumber sw shiftwidth. .6.7
.
6.7.

. .194.
:

Magic

. [ ] *

nomagic
. magic .
^ $ ,

shiftwidth
.
autoindent, ai
. , autoindent, , noautoindent.
vim
, ( )
CONTROL+T,

, , CONTROL+D . CONTROL+T CONTROL+D

( :n

), vim
autowrite, aw
. autowrite vim
, :n, , .
, noautowrite noaw
- vim

ESCAPE
flash, fl
. flash ,
. noflash
.

206 6. vim
6.7 ()



ignorecase, ic

vim ,
. ignorecase
. noignorecase

incsearch, is

.
. 194. vim
. vim , incsearch. vim
, noincsearch

vim ^I $, list.
,
nolist

List


number, nu

vim , .
number. , nonumber.
, .
vim

, vim ,

. vim
Wrap
, wrap ( ). nowrap, vim

wrapmargin=nn,
wm=nn


report=nn

vim ,
, .
vim , ,
.
, , .
.
wrapmargin=nn nn , vim
. ,
.
.
( )
vim
, nn . , re
port 7 , vim 7 lines
deleted ( 7 ). vim . report 5

207

- ,

CONTROL+D CONTROL+U (.
scroll=nn,
CONTROL+D CONTROL+U . 187). scroll
scr=nn
, .
, . CONTROL+D
CONTROL+U, vim scroll . scroll , scroll=nn, nn CONTROL+D
CONTROL+U

shell=_
,
sh=_

shiftwidth=nn,
sw=nn

vim .
(
) . shell ,
vim. vim
shell , .
, shell=_, _

CONTROL+T
CONTROL+D (.
). shiftwidth=nn nn
( 8).
,


showmatch, sm

, ,
({ })
(Lisp, C, Tcl ..). showmatch ( )
, (
).
.
, , vim .
,
noshowmatch

showmode, smd

vim , showmode ( ).
vim , noshowmode


vi
compatible, cp

. compatible . 177. vim vi. vim vi, compatible. vim


vi, nocompatible

wrapscan, ws

, , vim
.
. nowrapscan
. ,
wrapscan

208 6. vim


,
vim.

vim .
mc, c . ( ,
, , .) vim .
, . , ,
. , t
mt. ,
, , ,
, , 't.
, , r:
d'r

, , (, ).
t (
, ), t. , r;
, , :
dr

. The THE m
( m ):
:'m,.s/The/THE/g

vim , :
:e[!] [_]

, ,
( :w). ,
vim , ,
,
. _, vim , .
:e! .
, , ,
, ,
. ,
, , . :e ,
, :e .

:e!

:e# ,
, ,
. , vim

:e#

209

. autowrite (. .6.7),
vim .
:e# . vim , :n , :e# :rew

. . vim , :n!.

:n :rew

vim . :map
, . CONTROL+X ,
(f[),
(df]) ,
(x), (2j) (0):

:map

:map ^X f[df]x2j0

, , ESCAPE- CONTROL-,
, ,
vim-. , :map
. , :map,
CONTROL+V (. CONTROL+V . 189).
:abbrev :map, ,
. ,
:abbrev, , vim
, . , . ZZ Sam the Great:

:abbrev

:abbrev ZZ Sam the Great

ZZ vim-, .
.

vim
vim . ,
:
:sh

:sh

vim shell (. .6.7) ,


( bash tcsh).
,
.
vim
( CONTROL+D
exit).

210 6. vim

:SH
, , :sh
. .
:sh, ,
. , :
sh

vim- shell . ,
vim :sh tcsh:
:set shell=/bin/tcsh
:sh ,
SHELL.



:sh ,

vim. ,

.
, vim .
,
vim. ,
vim, . 181.


vim , , ,
:

:!

:!

vim .
, .
vim .
- , :

:!!

!!

.
vim
, . vim
,
( , ).
vim,
.
.
, ,
, , ,
. ,
, !G. ,

211

b, !'b.
, vim
.
, q, mq.
:
!'qsort

. .
, , , , u.
V sort . 820.
!

,

(,
vim
).
.
(. (
u
U) . 189). :
e
! (.
. 208) ,
.
:
sh
, , ,
! .
. , ,
, shell.


vim-
. .
(. . 214). .

, , , .
:
a

>

TAB

, ,
: ,
, , .
, vim
(.6.8).

212 6. vim
6.8.

Pear

pear!

pear!)

pear!) The

pear!) The

11

This is a short, concise line (no frills)


( )

,
, , ,
. , ,
: , (.6.9).
6.9. ,

Pear

Pear!

Pear!)

Pear!) The

Pear!) The

This is a short, concise line (no frills)


( )

, .
() , (
) .
, , .

.
,
, . ,
,
, .
, fmt (. V,
fmt . 708).

213

.
, ,
(.6.10).
6.10.

That's it. This is one sentence


:
, .

: , - That's it. This is two sentence


.

:
,

What? Three sentences? One line!


? ? !

This sentence takes


up a total of
three lines

.
(.6.11).
6.11.

One paragraph

This may appear to be


more than one paragraph.

: ,

Even though in

,

.
Just because there are
two indentions does not mean
,
it qualifies as two paragraphs

English this is only


one sentence,

vim considers it to be
three paragraphs

vim ,

()
vim
.
. .6.5 .

214 6. vim


, (.
.211), . 5 5
5 ,
vim . ,
w , 5w
5, 250w 250. , vim , 1.
, .

, vim, , , . .6.12
vim .
6.12. vim

vim _

_,

vim +n _

_, n

vim + _

_,

vim +/ _

_, ,

vim r _

vim R _

_
( , view)


(.6.13), . , .
.
6.13.

, l ( l)

215

0 ()

( )

.6.14 ,
.
6.14.

CONTROL+D

CONTROL+U

CONTROL+F PAGE DOWN

CONTROL+B PAGE UP

nG

n ( n )

6.15 . , r, vim .
ESCAPE.
6.15.

( ESCAPE )

, ( ,
ESCAPE)

216 6. vim
. 6.16 , .
M , , n , c .
6.16.

Nx

, n,

nX

n , ,

dM

, M

Ndd

Dtc

n~

vim . ESCAPE
Ns

cM

, M

Ncc

Ctc

. 6.17 . rexp , .
6.17.

/rexp

rexp

?rexp

rexp

Fc

Fc

Tc

c ( )

Tc

c (
)

f, F, t T

:
:[]s/_/_[/g]

217

, .
(.) , $ , % .
. _ ,
. _
, . g (
).
. 6.18 vim- .
6.18.

(.)

:w _

_
( , _ )

:q

vim

ZZ


vim

:f CONTROL+G

, , ,
,

CONTROL+V

,
vim- ( )

. 6.19 , . M , , n .
, "x, x
( az).
6.19.

yM

, M

Nyy

. 6.20 vim-.
6.20.

Mx

x, x a z

'' (

218 6. vim
6.20 ()

'x

, x

'x

, x

:e _

_, , ( :w autowrite).
, , :e!
_. , , :e!

:n

, vim .
, ( :w
autowrite) . ,
,
:n!

:rew

, vim

.
,
( :w autowrite).
, , :rew!

:sh

. vim,

:!

!!

, ,

1. vim ? vim ?
2. ?
.
3. , , ,
(~):
The vim editor has a command, tilde (~),
that changes lowercase letters to (~),
uppercase, and vice versa.
The ~ command works with a Unit of Measure or
a Repeat Factor, so you can change
the case of more than one character at a time.

:
1) ?
2) Unit?
3) character letter?

vim

,
~
,

.

219
4. vim , x, p. , .
5. ?
1) i I
2) a A
3) o O
4) r R
5) u U
6.
, it?
7. this week
next week?
8. . vim
. , ,
.
, .
?
9. ?
10. vim letter_e, e (. bzip2: . 80). vim-.
vim- ?


1. ?
2. , ,
vim . (:
pr V.)
Command mode
Input mode
Last Line mode
Work buffer
General-Purpose buffer
Named buffer
Regular Expression
Search String
Replacement String
Startup File
Repeat Factor

3. ?
4. , vim
. , , ,
?
5. hither yon ? (
,
.)

7. emacs
1956 (MIT)
(John McCarthy) (List processing
).
() .
.
( ) ( , )
. ,
MIT, (Richard Stallman)
(Guy Steele). 1975 emacs, .
emacs 22, Free Software Foundation (GNU).
emacs www.gnu.org/software/emacs.

emacs ,
60- TECO (Text Editor and Corrector
). ,
Editor MACroS( ),
, ESCAPE META ALT
CONTROL SHIFT ( ), Emacs Makes All Computing Simple (Emacs
)
Eight Megabytes And Constantly Swapping (
).

emacs , 20
GNU-. emacs,
C,
XWindow System, .
TECO, ,
.

22 : UTF-8,
Unicode,
, 30 .


WYSIWYG (what you see is what you get , ),
.

221

emacs ,
.
UNIX
-, UNIX/Linux-. UNIX/Linux-,
-
, emacs
. (), emacs

.
~/.emacs.
X Window System
, emacs . emacs
, ,
,
, , .
emacs ,
.
, emacs .
X Window System emacs , - .
- ,
.
, , emacs ,
, nroff,
TeX, , C, ..
, , vi, vim
. emacs
, , .

emacs vim

en.wikipedia.org/wiki/Editor_war, emacs vim
.
vim, emacs :
. vim emacs ,
, :
CONTROL .
emacs ( ),

222 7. emacs
.
.
vim, emacs
. , emacs, ,
. ,
, .
C ,
.
vim, emacs ,
. ,
. ,
.
~/.emacs,
.
, ,
GNU- emacs.



, ,
. ,
.

, vim , emacs .
,
,
-. .emacs
, . emacs ,
emacs .
.emacs .emacs.

EMACS

EMACS X WINDOW SYSTEM


, .
emacs, .

19, GNU emacs X Window System.


emacs , ,
emacs

X
- (
GUI
).
, emacs nw,
. . emacs . 223.

emacs 223

emacs

Linux .
emacs
. , emacs,
emacs . 230.

emacs
emacs
. 35. ,

. , , emacs ,
.
.

EMACS
emacs .
, emacs CONTROL+H t.
emacs, CONTROL+X CONTROL+C.
emacs, . 239.

emacs
sample emacs, , :
$ emacs -nw -q sample

nw, emacs, X- (GUI). q


emacs ~/.emacs. ,
emacs
,
.emacs.
emacs, sample
. , emacs (New File)
(.7.1). , emacs
(.7.3). emacs
, , (.7.2).
emacs .
, , .

224 7. emacs
F10, META+ ( )
META-x tmm-menubar .
.

/
-

. 7.1. emacs

emacs
, (Mode Line).
, ,
, . ,
.
emacs . -
( ),
.

. 7.2. emacs

emacs 225

EMACS
emacs .
emacs CONTROL+H r.
, . m (
-), ( ),
. emacs ,
. ,
(. ).
. 239,
. 239.
, Minibuffer,
CONTROL+H r m minibuffer . CONTROLH r m min TAB .

- , emacs
. , emacs .
, , .
,
Minibuffer (. ).
, -. . emacs
. ,
, .

emacs CONTROL+X CONTROL+C.


( CONTROL+G).
emacs , , ,
.

, , CONTROL+G.
emacs Quit ()
.


( )
,
, .


emacs, . CONTROL+D ,

226 7. emacs
DELETE DEL. BACKSPACE .
, .


, , ,
emacs, .
:
$ info emacs
info, m deletion, ,
.
. info q.
emacs (
CONTROL-Hr; . . 239).

emacs . , .

.
. .7.3 .



, , .


emacs
, .
(.

6.8 6), , , , , .
( CONTROL+U, ), .
.

. 7.3.

emacs 227


() , CONTROL+F.
, . , CONTROL+U 7 CONTROL+F Wednesday,
September 30, 2009 10:13 AM
.

CONTROL+F

m Page 212

() , CONTROL+B.
, CONTROL+U 7 CONTROL+B .
Chapter 7
The emacs
Editor

CONTROL+B CONTROL+F (.7.4).

CONTROL+B

CONTROL-B

CONTROL-F

Figure
7-4
Moving the
cursor by
characters
. 7.4.

Moving the
Cursor
by
Characters

CONTROL-F

Pressing the

RIGHT ARROW

key or

CONTROL-F

moves the cursor forward (to the right) one

commands
,
character. If the
cursor
is at the end
of a line,these
wrap
it to the
beginning
META+f
META+f. -

of the next line. For example, the command CONTROL-U 7 CONTROL-F moves the cursor seven

forward.
f META ALT. characters
, ESCAPE,
CONTROL-B Pressing the LEFT ARROW key or CONTROL-B moves the cursor backward (to the left) one
, f.
character. For example, the command CONTROL-U 7 CONTROL-B moves the cursor seven
, , .
characters backward. The command CONTROL-B works in a manner similar to CONTROL-F
CONTROL-U 4 META-f ,
(Figure 7-4).
. : . 231.

Moving the Cursor by Words

one

META-f
Pressing META-f
moves
the cursor forward

,this

word.
To invoke
command,
hold

META+b

or ALT key
while you
press,
f. If
the keyboard
you are using
does not
down the META


,
ESCAPE, release
it, and
then
press
f. This
command
have
either
of these
keys,
press

META+b

leaves the
cursor on the
first character
that
is not part of the
word thecursor
started

.CONTROL-U
4
META

META-f moves
the +b
cursor
forward
one space
past theMETA+f
end
on. The command
(.7.5).
of the fourth word. For more information refer to Keys: Notation and Use on
META+b

page 216.

backward

META-b
Pressing META-b moves
the cursor
one

word, leaving the cursor on the first


letter
of
the
word
it
started
on.
If
the
cursor
was
on the
first letter
of a word,META-b
CONTROL+A

moves the cursor


to
the
first
letter
of
the
preceding
word.
The
command
META-b
CONTROL+A,
, ,

CONTROL+E,
works in a manner
similar
to META-f
(Figure
CONTROL+E

7-5).
.
CONTROL+P

CONTROL+P
CONTROL+N
Moving the
by
Lines
Cursor
,
, ,
CONTROL-A
Pressing
CONTROL-A
moves the cursor
to
the beginning
of the
line it
is on;
CONTROL-E

,

CONTROL-E
moves
it to the
end. Pressing
the UP
ARROW key or CONTROL-P
moves
the
cursorup
one

CONTROL+N

CONTROL-P
line to the
position directly
above
where
the cursor
started; pressing
the DOWN

ARROW
CONTROL-N
key or
CONTROL-N
moves it
down. As
with the other
cursor-movement
keys, you
can

CONTROL+P
CONTROL+N

and CONTROL-N
with
CONTROL-U and
a numeric
argument
move the
precede CONTROL-P

CONTROL+U

.

to
cursor up or down multiple lines. You can also use pairs of these commands to
move the cursor up to the beginning of the previous line, down to the end of the following line, and so on (Figure 7-6).

Pressing CONTROL-A moves the cursor to the beginning of the line it is on; CONTROL-E
moves it to the end. Pressing the UP ARROW key or CONTROL-P moves the cursor up one
CONTROL-P line to the position directly above where the cursor started; pressing the DOWN ARROW
CONTROL-N
key or CONTROL-N moves it down. As with the other cursor-movement keys, you can
228 7. emacs
precede CONTROL-P and CONTROL-N with CONTROL-U and a numeric argument to move the
cursor up or down multiple lines. You can also use pairs of these commands to
move
the
cursor
up to
the beginning
of the previous
down
to the end ,
of the fol

line,

lowing
line, and so
on (Figure
7-6).

. .
(.7.6).
CONTROL-A
CONTROL-E

META-b

META-f

213 Wednesday, September 30, 2009 10:13 AM

belief,.really...It
Figure
7-5
Moving the
cursorGetting
by
words
Tutorial:
Started with emacs 213
. 7.5.

CONTROL-P
CONTROL-P
CONTROL-A

needed.as

CONTROL-P
CONTROL-E

CONTROL-A

with.their

CONTROL-E

CONTROL-N
CONTROL-A

working.to

CONTROL-N
CONTROL-E

CONTROL-N

Figure .
7-6 7.6.
Moving
the cursor
by lines

Moving the
Cursor
by ,
Sentences, Paragraphs,

and Window
Position

META-a, META-e
Pressing META-a
moves
the cursor
to the beginning
of the
sentence the cursor
is on;

META+a

META-{, META-}
META+a,
META-e moves the cursor to the end. META-{ moves the cursor to the beginning of the

, , META+e

CONTROL-U
followed
by
a numeric
argument) META+}
to move
the cursor
bythat
tition count (
,

many
sentences
or paragraphs.

.
(
,
cursor
.)

META-r
Pressing
META-r moves the
to the
beginning
of the
middle
line of
the window.

(
CONTROL+U
,

),
You can precede this command with CONTROL-U and a line number (here CONTROL-U does

CONnot indicate
a repetition
count but rather
a screen
line number).
The command
.
TROL-U 0 META-r moves the cursor to the beginning of the top line (line zero) in the winCONTROL-U
(minus sign)
moves
the cursor
to the beginning
of the
dow. The command

META+r


last line of the
window
(Figure
7-7).
META+r
.

ONTROL-U
META-r

ETA-r

META+e,
to the
end. (Sentences
and paragraphs
are
paragraph the
cursor
is on; META-} moves

it

.

META+{
defined
starting
on
page
241.)
You
can
precede
any
of
these
commands
with
a
repe

META+{

META+}

CONTROL+U ( CONTROL+U , ). CONTROL+U 0 META+r

() .

META-{
CONTROL+U ( )
(.7.7).
META-a

emacs 229

CONTROL-U
0 META-r

ME TA- {

META-r

ME TA- a

CONTROL-U
META-r

ME TA- e

ME TA-}

. 7.7. ,


, ,
. emacs
. , , . ,
, emacs (\)
.
, .
, Linux
,
. , ,
, .
BACKSPACE . BACKSPACE
.

BACKSPACE.

CONTROL+D. , , ,
, .
, ,
. 226.


,
emacs, , . emacs (

230 7. emacs
emacs ), , emacs
, .

emacs
. , ( ) .

(~). .~n~, n
, . emacs version-control.
emacs
. 267.


CONTROL+X CONTROL+S. emacs
,

.
emacs ( emacs visiting a file),
emacs CONT
ROL+X CONTROL+F. emacs ,
.
, emacs, , emacs .

CONTROL+X CONTROL+F
CONTROL+X CONTROL+F, emacs
, , , .
, emacs ,
. ,
. (.
. 249).

emacs
emacs
ASCII
() . , ,
emacs . 19
. emacs , ,
.
, ,
, ,

231

.
.

EMACS

,
emacs .
emacs,
, .

, , emacs
nw , .
emacs (.7.8),
,
GNU. emacs
, .
, , ,
emacs .

. 7.8. emacs



emacs. emacs.

:
emacs ,
ASCII-,

232 7. emacs
. ASCII- SHIFT, , CONTROL. META ( , ALT),
. ASCII ,
. - emacs, , CONTROL META, .
, emacs, (.7.1).
C M,
CONTROL META ( ALT),
, RET ( RETURN), LFD (LINEFEED ), DEL (DELETE), ESC (ESCAPE), SPC
(SPACE) TAB.
emacs, .
7.1. emacs

emacs

( ) a

( ) SHIFT+a

CONTROL+a

C-a

CONTROL+A

C-a (SHIFT ), CONTROL+a

META+a

M-a

META+A

M-A (SHIFT ), M-a

CONTROL+META+a

C-M-a

META+CONTROL+a

M-C-a ( )

emacs . META, META.


, emacs - XON-XOFF, CONTROL+S
CONTROL+Q.
OS X
META ALT. META
, ESCAPE, META.
META ALT,
ESCAPE-, ESCAPE,
, META. , ESCAPE a META+a ESCAPE
CONTROL+A CONTROL+META+a.
, XON-XOFF
emacs. emacs
.

233

,
CONTROL

, META


. SHIFT
CONTROL- META-. META- (, META+A)
, -
, .


emacs
( ,
emacs) ,
.

.
, emacs, , ,

.
emacs . 266.

META+x:
( , emacs
; .
. 268), , . , META+x.
META+x emacs .
.
, META+x _.
emacs .
TAB,
emacs
. ,
, (?)
(Completion List), ,
.
, (. . 249).


emacs .
, .
.

234 7. emacs
,
. ,
:
META 09
(). , 10 z, META+1 META+0 z.
CONTROL+U ,
. , 20,
CONTROL+U 20 META+f.
CONTROL+U,
, 4.
, CONTROL+U r rrrr (4 * 1),
CONTROL+U CONTROL+U r rrrrrrrrrrrrrrrr (4 * 4 * 1).

CONTROL+U CONTROL+V
, CONTROL+U META+v , CONTROL+U
CONTROL+U CONTROL+V 16 CONTROL+U
CONTROL+U META+v 16.

CONTROL+U

, . ,
. ,
. , .
,
, ,
.

.



, ,
-, .
.
( ) CONTROL+V PAGE DOWN.
, . ( )
META+v PAGE UP. CONTROL+L
.
.

CONTROL+V,
META+v
CONTROL+L

235

CONTROL+V META+v
, CONTROL+U 10
CONTROL+V . CONTROL+L , , 0 , 1 ,
. CONTROL+P
CONTROL+N , .

META+<
META+>

META+<, , META+>
.


.
(delete) , (kill)
. ,
,
. , , (. : . 244).

META+d
. CONTROL+K
. , .
CONTROL+A, CONTROL+K,
,
. ,

CONTROL+K CONTROL+K ( CONTROL+U
2 CONTROL+K).

META+d
CONTROL+K


emacs :
.

(, ).
.
.
, .
,

236 7. emacs
.
.
. , ,
, .

CONTROL+S
CONTROL+R

.
CONTROL+S
, CONTROL+R

.
emacs
I-search:.
. , emacs , . , emacs
.
, ,
.
.
, .
,
. (, emacs,
, .
.)
, , .
, . , CONTROL+R CONTROL+S, .
,
Failing I-Search.
, :
yy , BACKSPACE, .
.
yy ,
, CONTROL+R
CONTROL+S.
yy ,
,
.
yy
, , CONTROL+G,
. CONTROL+G ,

237

, . ,
,
. ,
CONTROL+G
.



,
,
CONTROL+S CONTROL+R
. ,
emacs .
.

CONTROL+S

CONTROL+R


, emacs , . , .7.2.
7.2.

META+CONTROL+s

META+CONTROL+r

META+CONTROL+s

META+CONTROL+r



emacs (. .7.1). . emacs
(. : . 256). ,
C , ,
C.
, F10, META+ ( ) META+x tmm-menubar .
emacs ,

238 7. emacs
File (), Edit (), Options () ..,
, -. .7.9
,
- File ().

. 7.9.

,
:
, CONTROL+G ESCAPE
ESCAPE ESCAPE. , .
-. .
,
, . .
PAGE UP META+v
.
. ,
. -
ESCAPE ESCAPE ESCAPE.
emacs
.
. , emacs .
Menu Bar (.
emacs . 225).

239


emacs .
CONTROL+H.
. ,
(?) CONTROL+H, ,
emacs . ,
, CONTROL+G
.
, . ,
.
CONTROL+V META+v (. . 234).
CONTROL+X o ( o
). . 253.

CONTROL+H


, ,
,
CONTROL
+
X
1 ().
(CONTROL+X o [ o ])
CONTROL+X 0 () .
, ,
CONTROL+H n ( emacs) CONTROL+H t ( emacs),
CONTROL+X k
CONTROL+X b ( . 252).

BACKSPACE , CONTROL+H. ,
emacs, , .
,
, .
, CONTROL+G
. (.
. 268). .7.3.
7.3.

CONTROL+H a

CONTROL+H b

240 7. emacs
7.3 ()

, _.
_
.
,
,
.
(F1, F2 , ), ESCAPE [ SHIFT

CONTROL+H c

CONTROL+H f

-
. -,

CONTROL+H i

info (. info

: . 56),
emacs

CONTROL+H k

, _
_- . (. CONTROL+Hc)

CONTROL+H l
100 .
( l .
)
,
CONTROL+H m


(
Text
, C

, Fundamental

..; . . 257)

CONTROL+H n

emacs, , emacs,

CONTROL+H r

emacs

CONTROL+H t

emacs

CONTROL+H v

CONTROL+H w

.
. (.
CONTROL+H c)

,
- emacs. , ,
, , ,
-. emacs,
CONTROL+H a ( ) META+x apropos (
, ).
Backward
Beginning
Buffer
Case

Dir
Down
End
File

insert
Kill
Line
List

previous
region
register
screen

View
What
Window
Word

241

Change
Char

Fill
Find

mark
mode

search
sentence

Defun

Forward

next

set

Delete

Goto

page

sexp

Describe

Indent

paragraph

up

Yank


emacs ,
. emacs.


emacs. .
,
. ,
.
(. .

7.1) , .
: ( ), ** () %% (
).
emacs ( )
( 20000). ,
, . (. . 252),
.
, , ,
. CONTROL+_ ( , ASR-33 TTY ) CONTROL+X u.
CONTROL+_ emacs
, ,
. CONTROL+_ , . . CONTROL+_,
, , , **, .
, , ,
.
.

242 7. emacs
, , ( , , CONTROL+F) . .7.4
.
7.4.

CONTROL+_

CONTROL+_ CONTROL+F CONTROL+_

CONTROL+_ CONTROL+_

CONTROL+_ CONTROL+_ CONTROL+F


CONTROL+_ CONTROL+_

CONTROL+_ CONTROL+_ CONTROL+F


CONTROL+_

, ,
CONTROL+_ . , ,
CONTROL+F CONTROL+_, .
, CONTROL+_
, .
,
META+x revert-buffer,
. emacs
.

,
. .
, .
( ) .
,
.


, .
-
. .
CONTROL+@ ( CONTROL+SPACE) , ( ).
CONTROL+@ CONTROL+Q.
, ,
.
( )
CONTROL+X CONTROL+X.

CONTROL+@,
CONTROL+
SPACE
CONTROL+X
CONTROL+X

243

( )
, CONTROL+@ ,

( ) . ,
, CONTROL+X
CONTROL+X. ,
CONTROL+X CONTROL+X, .
, CONTROL+X
CONTROL+X ,
. ,
.


.7.5 , .
CONTROL+H a region.
7.5.

META+w

CONTROL+W

META+x print-region

META+x append-to-buffer

META+x append-to-file

META+x capitalize-region

CONTROL+X CONTROL+L


. FIFO (
), 16 .
.
,
. , ,
, ,
.
,
( ) CONT
ROL+U CONTROL+@. emacs
:
( ) .

.

CONTROL+U
CONTROL+@

244 7. emacs
( ) -

.
CONTROL+U CONTROL+@ emacs
,
.
, .
,
, .
CONTROL+U CONTROL+@ ,
.
, .


. ,
.
, META+> .
CONTROL+U CONTROL+@. .
, , ,
: Mark Set.

:
, , . 30 ,
.
(yanking). emacs
, vim: yanking
.
, emacs . .7.6
.
7.6.

META+d

META+D

CONTROL+K

CONTROL+U 1 CONTROL+K

CONTROL+U 0 CONTROL+K

META+w

245

CONTROL+W

META+z char

char

CONTROL+Y


;
, ;
CONTROL+Y

META+y

,

( CONTROL+Y META+y)

,
, CONTROL+U 2 CONTROL+K,
.
CONTROL+Y.

CONTROL+U 2 CONTROL+K CONTROL+Y, ,
. CONTROL+Y.
, , CONTROL+W CONTROL+Y,
.
CONTROL+Y. META+w .
FIFO- ,

. , , . ,
. ,
, ,
, .
,
CONTROL+Y. ( )
. , ,
, META+y.
. , , META+y,
. . ,
. META+y,
, . , .
, ,
CONTROL+Y, CONTROL+Y META+y.
META+y ,

246 7. emacs
, ,
CONTROL+Y.

( ).
, .
, CONTROL+Y META+y , ,
, CONTROL+Y
, ,
META+y.
META+y .
.


, emacs ,
. , emacs,
emacs : CONTROL+Q.
:
CONTROL+Q , , , .
CONTROL+Q ,
.

CONTROL+Q
, CONTROL

+
Q

. ,
CONTROL
+
Q
, , ,
. quoted+insert
(. . 268).


vim
. emacs
. .
,
META+<, .

,
, .7.7,
, .
META+x occur
*Occur*, -

247

. *Occur*
, ( CONTROL+X o,
o ),
CONTROL+C CONTROL+C.
, , ,
.
7.7. ,

META+x occur

, , *Occur*

META+x delete-matching-lines

META+x delete-non-matching-lines

,
.


, .7.8, , . .

.
7.8.

META+x replace-string

_ _.
, , ,
CONTROL+U CONTROL+@

META+x replace-regexp

_ _
_ _. , , ,
CONTROL+U CONTROL+@

META+%
META+x query-replace

, . _,
, , _.
,
, ,
CONTROL+U CONTROL+@

248 7. emacs
7.8 ()
META+x query-replaceregexp

_ _,
_
, , _. ,
, ,
CONTROL+U CONTROL+@

emacs
,
. .7.9
.
7.9.

DELETE

, ()

, .
, , DELETE , ,

. ()

! (


( emacs- ),
emacs (. . 252),
, , ,
. .
emacs ,
( , ,
, ),
.
. ,
, META+x pwd,
META+x cd. , emacs
.

META+x pwd
META+x cd


emacs , ,
. ,

249

, , emacs . .7.10
, .
, . ,
. New File,
emacs.
, CONTROL+X CONTROL+V
.
7.10.

CONTROL+X CONTROL+F

.
. .

CONTROL+X CONTROL+V


,
.

CONTROL+X 4 CONTROL+F

.
.
. , , ,
,


- ,
.
, ,
bash (. . 249).
- TAB, emacs,
, . ,
. emacs .
, ,
. emacs ,
. ,
(/) , , ,
TAB, emacs
, , [Complete, but not unique].
TAB , emacs
, (.7.10).
, (?).

250 7. emacs
:
q , CONTROL+G ESCAPE ESCAPE ESCAPE.
, ;
q - .
, emacs
;
q -,
, TAB;
q META+v PAGE UP,
.
. , , .
CONTROL+G ESCAPE ESCAPE ESCAPE,
-.

. 7.10.

, emacs
,
, -.
-
.
Completion Completion Commands Minibuffer emacs (. emacs . 225).


,
. .7.11 .

251


(META+~)

. META+~
.

, ?


CONTROL
+
X

s
, ,

, , emacs
.
emacs
-
, (y). ,
CONTROL+Xs, n , .
:
CONTROL+X
CONTROL+W .
, CONTROL+_ ,
**.
, ,
META+x revert-buffer.
. , emacs
.
META+~ () **.
CONTROL+X s, ,
.

7.11.

CONTROL+X CONTROL+S

, .
, emacs ,
No changes need to be saved

CONTROL+X s


y
n.
emacs , , .

META+x set-visited-filename

CONTROL+X CONTROL+W

,
. META+x set-visited-filename, CONTROL+X CONTROL+S

META+~ ()

.
META+~ , , emacs
, ** ,
. ,
,
DELETE

252 7. emacs

emacs
, . , .
,
. ,
. ,
: , , , , , . ,
, .7.12.
7.12.

CONTROL+X b

.
,

CONTROL+X 4 b

.
,

CONTROL+X CONTROL+B

*Buffer List* .
,
. . *Buffer List*
, , ,
() . ,
, (% ), (*)
, , (.)

META+x rename-buffer

CONTROL+X CONTROL+Q


%% .

META+x append-to-buffer

META+x prepend-to-buffer

META+x copy-to-buffer

META+x insert-buffer

CONTROL+X k

.
, , emacs

META+x kill-some-buffers

.
CONTROL+X k, emacs
,

253

emacs . emacs ,
. .
emacs . 222.
. CONTROL+X b _ ,
.
,
. ,
. ,
.

CONTROL+X b
_

. 7.11.


. CONTROL+X 2 ,
.
. CONTROL+X 3 ,
(.7.11). , . , CONTROL+U CONTROL+X 2
, CONTROL+U 4
, (
).
,
. ( )
,
.

254 7. emacs



CONTROL+Xo ( o ). , CONTROL+X o
,
. META+CONTROL+V . , , CONTROL+X o. , , ,
CONTROL+V .

CONTROL+X o
META+
CONTROL+V


emacs
, ,
CONTROL+X 4. , CONTROL+X 4b .
,
. CONT
ROL+X 4f ,
. , .

CONTROL+X
4b
CONTROL+X
4f


, . ,
, , ,
. CONTROL+X 0 () ,
CONTROL+X 1 .

CONTROL+X 0
CONTROL+
X1

.
, META+x shrinkwindow . ,
CONTROL+X ^, , CONT
ROL+X }, CONTROL+X {.

, .
emacs , , . , .

META+x
shrink-window,
CONTROL+X ^,
CONTROL+X }
CONTROL+
X{

,
emacs (,
emacs; . . 223)

255

,
(.7.13).
vim, , (. :!! . 210).
, vim, .
7.13.

META+!

(
)
CONTROL+U META+!

(
)
META+| (

)
CONTROL+U META+|

( )

,
,
,
,
,
, ,

emacs , .
. 265.

,
emacs , , emacs, .

.
.
*compilation*. ,
,
.

, .
,
META+x compile, . *compilation* .
, *compilation*
. , , META+>.
, CONTROL+X 0 (),

META+x
compile

256 7. emacs
, , , CONTROL+X 1 . *compilation*,
CONTROL+X b.
, META+x killcompilation. emacs ,
.
*compilation* , , .
, , CONTROL+X ( ).
*compilation*
. ,
CONTROL+U CONTROL+X.

:

emacs ,
. .
.
. , , . ,
,
(..7.1).
,
:
(, text, nroff, TeX).
, (, C,
Fortran, Lisp).
(, shell, mail, dired, ftp).
, Fundamental,
. :
, , ,
. ,
.
, , , , ,
.. ,
, , , , .

: 257


emacs , . ,
.
META+x _.
, emacs .
, - * _ *, ,
, ,
.

META+x _


,
, , .
, .
.
,
, TeX, LaTeX nroff/troff. ,
, ,
,
.

,
, , , : CONTROL+F, CONTROL+B, CONTROL+D, DELETE CONTROL+T.
, CONTROL+F CONTROL+B
, META+f META+b .
, ,
,
. ,
, .

META+f
META+b

, CONTROL+D DELETE
,
META+d META+DELETE .
, META+f META+b, , . .

META+d
META+
DELETE

META+t

META+t
.

258 7. emacs

, , , , :
CONTROL+A, CONTROL+E CONTROL+K. META+a
, META+e . ,
CONTROL+X DELETE ,
META+k .
emacs , , sentence-end. ,
emacs ., ? !,
, , .
, CONTROL+H v sentenceend .
META+a META+e
. , , ,
.
META+k CONTROL+X DELETE ,
, CONTROL+K .
, META+a META+e,
, . .
CONTROL+X DELETE .

META+a,
META+e,
CONTROL+X
DELETE
META+k

META+{ , META+}
. META+h ,
, ( , ), ,
.
META+} META+{ ,
. , , ,
, .
, ,
, ,
, .
, paragraph-separate paragraphstart . ,
. ,
.

META+{,
META+}
META+h

: 259

emacs , , . , . emacs

.
META+x auto-fill-mode
(Auto Fill). , emacs ,
.
.

META+x autofill-mode

, .
,
,
. , META+q ,
META+x fill-region (
).
,
70, fill-column.
fill-column ,
CONTROL+X f, fill-column nnn,
0 , CONTROL+U nnn
CONTROL+X f.

META+q
META+x
fill-region


emacs , (
, ) (.7.14).
7.14.

META+l ( l , , -

META+u

, ,

META+c

, ,

CONTROL+X CONTROL+L

CONTROL+X CONTROL+U

260 7. emacs
, , ( ,
META+f),
META+l, META+u META+c
, META+f.
,
.

, . .
.7.15 .
7.15.

HELLOMETA+META+l ( l )

Hello

helloMETA+ META+u

HELLO

helloMETA+ META+c

Hello

() ,
, .


, ,
, ,
, .
,
, TAB
(.).
META+x text-mode.

META+x textmode

TAB tab-totab-stop. , TAB .


META+x edit-tab-stops, *Tab Stops*.
, . , CONTROL+C CONTROL+C.
(CONTROL+X k) (CONTROL+X b),
TAB.
, META+x edit-tab-stops,
, .
emacs , .
, . , ,
, .

META+x edittab-stops

: 261

C
, , , .
(, ),
:
, ,
.
.
.
emacs
C
, . , . -
,
. , , . ,
, , , .
C.

emacs
. (, ,
) , . , C

. C

C1.
.7.16 emacs, , , . CONTROL META .
,
.
7.16.

CONTROL+META+f

.
, (
, ).
yy (, ), .
yy ,


emacs sexp
S-expression. ,
(expression), .

262 7. emacs
7.16 ()

CONTROL+META+b

CONTROL+META+k

, . , CONTROL+META+f,
,

CONTROL+META+@

, CONTROL+META+f, . , CONTROL+X
CONTROL+X


emacs ,
, defun,
. , .
C ,
, {. .7.17
.



emacs

. ,
. ,
- ,
.

7.17.

CONTROL+META+a

CONTROL+META+e

CONTROL+META+h

(
, ).
, ,

emacs C
.
, C (.7.18).

: 263
7.18.

TAB

. TAB
,
. , ;
.
TAB ; TAB
.
, META+i
CONTROL+Q TAB

LINEFEED (

, TAB.
LINEFEED ,


CONTROL+META+q


. CONTROL+META+q ,
, .
,
TAB, .
,
, ,
TAB

CONTROL+META+\

.
, . ,
, , TAB


C, emacs . emacs
19 C,
C++, Objective-C Java. emacs

, (
50), , else.
emacs coffsetsalist,
.
. ,
.
c-style-alist (GNU,
K&R [Kernighan and Ritchie], BSD ..).
cstylealist:
("gnu"
(c-basic-offset . 2)

264 7. emacs
(c-comment-only-line-offset . (0 . 0))
(c-offsets-alist . ((statement-block-intro . +)
(knr-argdecl-intro . 5)
(substatement-open . +)
(label . 0)
(statement-case-open . +)
(statement-cont . +)
(arglist-intro . c-lineup-arglist-intro-after-paren)
(arglist-close . c-lineup-arglist)
))
)

.
, info- emacs , Customizing C Indentation ( C).
.emacs, ,
, .c.

comment-column,
CONTROL+H v comment-column .
.7.19 , .
7.19.

META+;

. .
, META+; ,
commentcolumn.
, , ,
commentcolumn, META+;

, commentcolumn, META+;

. ,
.

( ) ,
CONTROL+X ;

commentcolumn .
0

CONTROL+U
CONTROL+X ;

.
comment+column , , META+;,

CONTROL+U
CONTROL+X ;

comment-column , META+;

: 265


emacs , , .
.
:
Rmail: , ..
Dired: ls l .
VIP: vi.
VC: ( RCS, CVS
Subversion) emacs.
GUD (Grand Unified Debugger ): C ( )
emacs.
Tramp: ,
ftp scp.
Shell: emacs.
Shell mode.


,
. 254. emacs
.
,
. ,
.
, ,
.
emacs .
*shell* META+x shell .
*shell* , emacs . ,
, :
- explicit-shell-file-name.
ESHELL.
SHELL.
, META+xrena
mebuffer ,
META+x shell .
, .

META+x shell

266 7. emacs
(.7.20).
, CONTROL+C.
, Linux,
CONTROL+C.
7.20.

, emacs ,
, ()
. -
, , ,
, (. shell-prompt-pattern), .
,
,

CONTROL+C CONTROL+D

CONTROL+D

CONTROL+C CONTROL+C

CONTROL+C

CONTROL+C CONTROL+\

quit,

CONTROL+C CONTROL+U

CONTROL+C CONTROL+R

,
,

CONTROL+C CONTROL+O

emacs
emacs -,
C
. .
, ,
emacs.
, , , ,
.
.
, ,
. , .
, , vi vim,
DELETE CONTROL+H, CONTROL+H , vim, DELETE ( vim)
. DELETE ,
CONTROL+H .
, , , .
, CONTROL+N,

: 267

, CONTROL+X CONTROL+F. .
, ,
, . ,
. , CONTROL+W, vi vim
, emacs
CONTROL+W (kill-region)
(delete-word-backward).
. .
-, , . , - ,
- . ,
- ,
.
emacs , . , .

.emacs
emacs ~/.emacs, -.
emacs . .emacs
. q
.emacs, emacs .
.emacs. u (user )
~user/.emacs ( .emacs ).
.emacs
. . .
, , , , .
.emacs setq, .
, ,
.
c-indent-level 8:

(setq c-indent-level 8)

emacs CONTROL+H v .
, emacs .

, setq-default .
,
aset. ,
, . . .7.21
.

268 7. emacs

7.21. , .emacs

C, CONTROLMETA-: \C-s CONTROL+S, \M+s


META+s, \M-\C-s CONTROL+META+s

C ?,
(, ?a, ?\C-i, ?\033)

1 0 t (true) nil (false)

- ,


emacs
. ASCII , 128- ,
.
META-,
. META- ESCAPE, ,
.
:
: .
: .
: ; , .
,
. , emacs,
,
,
.
, , , ,
.
.
:
. , , emacs .
,
.
global-map.

: 269

:
, . , ,
.
, , .
,

.
, . , , , ,
self-insert-command.
(.7.22).
7.22.

ctl-x-map

, CONTROL+X

ctl-x-4-map

, CONTROL+X 4

esc-map

, ESCAPE ( META-)

help-map

, CONTROL+H

mode-specific-map

, CONTROL+C

, CONTROL+H b.
: ,
. .
CONTROL+H f _
CONTROL+H k _.

-
. .emacs,
define-key. :
.
, .
, .
, backward-kill-word CONTROL+W,

(define-key global-map "\C-w" 'backward-kill-word)

\ C-w CONTROL+W,
( \^w).
. .
kill-region CONTROL+X CONTROL+K

(define-key ctl-x-map "\C-k" 'kill-region)

.emacs
~/.emacs ,
vi vim. - ,

270 7. emacs


(;).
;;; Preference Variables
(setq make-backup-files nil)
;Do not make backup files
(setq backup-by-copying t)
;If you do, at least do not destroy links
(setq delete-auto-save-files t)
;Delete autosave files when writing orig
(setq blink-matching-paren nil)
;Do not blink opening delim
(setq require-final-newline 'ask)
;Ask about missing final newline
;; Reverse mappings for C-h and DEL.
;; Sometimes useful to get DEL character from the Backspace key,
;; and online help from the Delete key.
;; NB: F1 is always bound to online help.
(keyboard-translate ?\C-h ?\177)
(keyboard-translate ?\177 ?\C-h)
;; Some vi sugar: emulate the CR command
;; that positions us to first non-blank on next line.
(defun forward-line-1-skipws ()
"Position to first nonwhitespace character on next line.
(interactive)
(if (= (forward-line) 0)
;if moved OK to start of next line
(skip-chars-forward " \t")))
;skip over horizontal whitespace
;; Bind this to M-n. ("enhanced next-line")
;; C-M-n is arguably more "correct" but (1) it takes three fingers
;; and (2) C-M-n is already bound to forward-list.
(define-key esc-map "n" 'forward-line-1-skipws)
;; C mode customization: set vanilla (8-space bsd) indention style
(require 'cc-mode)
;kiss: be sure it's here
(setq c-default-style
'(
(java-mode . "java")
(awk-mode . "awk")
(c-mode . "bsd")
(other . "gnu")
))
;; See also CC Mode in online help for more style setup examples.
;; end of c mode style setup


emacs ,
. info-
emacs (. ).
-, GNU emacs www.gnu.org/software/
emacs. emacs , , comp.emacs gnu.emacs.help.

emacs
emacs Linux.
emacs apt-get,
, , ,

271

, yum.
- www.gnu.org.

:
:
Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor
Boston, MA 02110-1301, USA
: gnu@gnu.org
: +1 617-542-5942
: +1 617 542 2652
: www.gnu.org

, ,
, . emacs ,
CONTROL+U.
.7.23 .
7.23.

CONTROL+F

CONTROL+B

META+f

META+b

META+e

META+a

META+}

META+{

META+>

META+<

CONTROL+ESCAPE

CONTROL+A

CONTROL+N

()

CONTROL+P

()

CONTROL+V

()

META+v

()

CONTROL+L

META+r

CONTROL+U num META+r

num (0 = , = )

272 7. emacs
.7.24 , .
7.24.

CONTROL+DELETE

DELETE

META+d

META+DELETE

META+k

CONTROL+X DELETE

CONTROL+K

, , ;
,

CONTROL+U 1 CONTROL+K

CONTROL+U 0 CONTROL+K

META+z char

char,

META+w

(
)

CONTROL+W

( )

CONTROL+Y


; ,

META+y

,
(
CONTROL+Y META+y)

.7.25 ,
.
7.25.

CONTROL+S

CONTROL+S

CONTROL+R

CONTROL+R

META+CONTROL+S

META+ CONTROL+S

META+CONTROL+R

META+ CONTROL+R

273

.7.26 , .
7.26.

CONTROL+H a

CONTROL+H b

CONTROL+H c _

, _

CONTROL+H k _

, _

CONTROL+H f

CONTROL+H i ( i

info (. info:
. 56)

CONTROL+H l ( l

100

)
CONTROL+H m

CONTROL+H n

emacs

CONTROL+H t

emacs

CONTROL+H v

CONTROL+H w

, ,

.7.27 , .
7.27.

META+W

CONTROL+W

()

META+x print-region

META+x append-to-buffer

META+x append-to-file

CONTROL+X CONTROL+U

CONTROL+X CONTROL+L

274 7. emacs
.7.28 , .
7.28.

META+x occur

, ,
*Occur*

META+x delete-matchinglines

META+x delete-non-match
ing-lines

.7.29 , ,
.
7.29. ,

META+x replace-string


;
,

META+% META+x
query-replace

, , ( . .7.30)

META+x replace-regexp

,
, ; ,

META+x query-replaceregexp

, , ( . .7.30)

.7.30 .
7.30.

( )

DELETE

, ()

. ()

! ( )

275

.7.31 .
7.31.

CONTROL+X b

CONTROL+X 2

CONTROL+X 3

CONTROL+X o ( o

META+CONTROL+V

CONTROL+X 4b

CONTROL+X 4f

CONTROL+X 0 ()

CONTROL+X 1 ()

META+x shrink-window

CONTROL+X ^

CONTROL+X }

CONTROL+X {

.7.32 .
7.32.

CONTROL+X CONTROL+F

CONTROL+X CONTROL+V


( )

CONTROL+X 4 CONTROL+F


; .
. ,

CONTROL+X CONTROL+S

CONTROL+X s

(y/n)

META+x set-visited-filename

CONTROL+X CONTROL+W

META+~ ()

276 7. emacs
. 7.33 .
7.33.

CONTROL+X CONTROL+S

CONTROL+X CONTROL+F

()

CONTROL+X b

.
,

CONTROL+X 4b

.
,

CONTROL+X CONTROL+B

*Buffer List*, .
,
. . *Buffer List*
, , ,
()

META+x rename-buffer

CONTROL+X CONTROL+Q


%%

META+x append-to-buffer

META+x prepend-to-buffer

META+x copy-to-buffer

META+x insert-buffer

CONTROL+X k

META+x kill-some-buffers

.7.34 ,
. .
7.34. ,

META+! (

CONTROL+U META+! (-

,
,

META+| (

,
,

277

CONTROL+U META+| (-

,
, ,

.7.35 ,
.
7.35. ,

META+x compile



*compilation*

META+x kill-compilation

.7.35 ,
.
7.36.

META+l ( l , , -

META+u

, ,

META+c

, ,

CONTROL+X CONTROL+L

CONTROL+X CONTROL+U

.7.37 C.
7.37. C

CONTROL+META+f

CONTROL+META+b

CONTROL+META+k

CONTROL+META+@

,
CONTROL+META+f,

CONTROL+META+a

CONTROL+META+e

CONTROL+META+h

, ( ,
)

278 7. emacs
*shell*
META+x shell. . 7.38 ,
.
7.38.

CONTROL+C CONTROL+D

CONTROL+D

CONTROL+C CONTROL+C

CONTROL+C

CONTROL+C CONTROL+\

quit

CONTROL+C CONTROL+U

CONTROL+C CONTROL+R

,
,

CONTROL+C CONTROL+O

( O
)

1. a , :
1) his hers?
2) ?
3)
?
4) His ?
2. ? ?
3. , .
1) ,
?
2) , ? ?
4. , ,
, .

?
5. ,
? ?
6. ?
7. ?
8. , , - ,
. ?

279
, ?
> ?
9. emacs . ,
. X

, .
.
:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Full fathom five thy father lies;


Of his bones are coral made;
Those are pearls that were his eyes:
Nothing of him that doth fade,
But doth suffer a sea-change
Into something rich and strange.
Sea-nymphs hourly ring his knell:
Ding-dong.
Hark! now I hear them-Ding-dong, bell!

:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Full fathiom five tyy father lies;


These are pearls that were his eyes:
Of his bones are coral made;
Nothin of him that doth fade,
But doth susffer a sea-change
Into something rich and strange.
Sea-nymphs hourly ring his knell:
Ding=dong.
Hard! now I hear them-Ding-dong, bell!

:
1)
?
2) ,
?
3)
? ?
4) 2 3 .
?


, C,
C, while,
:
/*
* Copy string s2 to s1. s1 must be large enough
* return s1
*/
char *strcpy(char *s1, char *s2)

280 7. emacs
{
char *os1;
os1 = s1;
while (*s1++ = *s2++)
;
return os1;
}
/*
* Copy source into dest, stopping after '\0'
* return a pointer to the '\0' at the end of
* can catenate to the dest * string without
*/
char *stpcpy (char *dest, char *source)
{
while ((*dest++ = *source++) != '\0')
; /* void loop body */
return (dest - 1);
}

is copied, and
dest. Then our caller
another strlen call.

1) strcpy?
? ?
2) , while.
?
?
[] ( ) {} ( )?
vim- %?
3) Berkeley, GNU.
, ?
?
4) , strcpy . strcpy?
5) emacs?

III

8. Bourne Again Shell . . . . . . . . . . 282


9. TC Shell . . . . . . . . . . . . . . . . . . . 367

8. Bourne Again Shell


5 Bourne Again Shell
(bash). , tcsh
bash, , , , . 10
,
Bourne Again
Shell (bash). bash www.gnu.org/software/bash.
Info bash Bourne Again
Shell.
Bourne Again Shell TC Shell (tcsh) .
, . ,
, , .
, (, for if).
,
.
, ,
cp, ,
, , . .
, , ,
, , , ,
,
. , . 10
.
bash ( dash, . ). - , ,
, ,
.
, 5, ,
, .
,
, .
9 TC Shell,
, 10 .

283

Bourne Again Shell Bourne Shell,


UNIX ( ,
Bourne Shell), (Steve Bourne),
Bell Laboratories AT&T. Bourne Shell , ,
UNIX.

Bourne Shell , UNIX-. Linux Bourne Again Shell. Bourne Again
Shell ,
Bourne Shell, bash
Bourne Shell, bash.
UNIX- Bourne Shell sh.
Linux- sh bash, , Bourne Shell.
sh bash
Bourne Shell.
sh

bash 800, . dash (Debian Almquist) 100,


Bourne Shell,
( ),
, bash.
sh, , Ubuntu, dash.
.

dash

System V UNIX Korn Shell (ksh),


(David Korn). , Bourne Shell,
. Bourne Again
Shell, ,
Korn Shell.

Korn Shell

POSIX (Portable Operating System Interface


) ,
PASC ( IEEE Portable Application Standards Committee, www.pasc.org).
(FAQ) POSIX,
, www.opengroup.org/austin/papers/posix_faq.html.
POSIX

- 1003.2 . Bourne Again Shell ,


POSIX

284 8. Bourne Again Shell


. ,
Bourne Again Shell POSIX.
bash posix, Bourne Again Shell
POSIX.

INFO- BOURNE AGAIN SHELL


BASH-DOC
Man- bash bash-doc.
info- bash. info
info: . 56.


, , , ,
.

CHSH:
, , ,
GUI.
Linux bash .
, .
, (bash, tcsh ),
. .
, exit

.
(
), .
, exit ,
. .
, ,
chsh. chsh.
, (/bin/bash,
/bin/tcsh - ).
, (.

2,
), GUI,
,
.


.
, :
, ,
, (,
bash), (
). ,
. Linux
. bash. tcsh
. 369.

285


, , , , ,
bash login.
.
/etc/
profile

, /etc/
profile. ,

(root-), , bash, ,
.
~/.bash_profile, ~/.bash_login ~/.profile
(~/ )
, ,
. , /etc/profile. , , ,
.

.bash_profile,
.bash_login
.profile

bash ,
~/.bash_logout. , , .
.bash_
logout

,

, .
, , .
bash,
~/.bashrc /etc/bashrc. root-
bash.
/etc/
bashrc

, , ~/.bashrc.
, , .bash_profile,
, , .bashrc, , .
.bashrc



, , . , ,
.

286 8. Bourne Again Shell


BASH_
ENV ( ENV, sh) , , .

BASH_ENV


,
.bash_profile .bashrc, . , , .bash_profile, .bashrc ,
( .bashrc ). ,
.bashrc, , .
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

[ f ~/.bashrc ] .bashrc . test


[ ] 10 V. . () () source: . 287.

PATH,
.BASH_PROFILE
.bashrc ,
, ,
, .bash_profile. ,
bin, home (.
, . 305),
.bash_profile:
PATH=$PATH:$HOME/bin
.bash_profile, .bashrc,
PATH , .
.bash_profile , ,
. .bashrc
, .

.bash_profile .bashrc. ,
, .
, .
. 454.
$ cat ~/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=$PATH:.
export PS1='[\h \W \!]\$ '

# ,
# PATH
#

.bash_profile .bashrc, .
PATH (. PATH: ,
. 312). PATH -

287

/etc/profile,
.
PS1 (. PS

1: () . 314), .
.bashrc. , /etc/bashrc, .
LANG (.8.5) VIMINIT ( vim) .
.
$ cat ~/.bashrc
if [ -f /etc/bashrc ]; then
source /etc/bashrc
# ,
fi
set -o noclobber
unset MAILCHECK
export LANG=C
export VIMINIT='set ai aw'
alias df='df -h'
alias rm='rm -i'
alias lt='ls -ltrh | tail'
alias h='history | tail'
alias ch='chmod 755 '

#
#
#
#
#
#


"you have new mail"
LANG
vim

rm

function switch()
{
local tmp=$$switch
mv "$1" $tmp
mv "$2" "$1"
mv $tmp "$2"
}

. () source:

, .bashrc,
, .
. () source (
bash , tcsh
source, . .9.10). ,
. () . . () source ,
, . , . () source ,
, ,
, . , . () source,
, ,
, , ,

288 8. Bourne Again Shell


. . () source
, ,
( , ).
10, . 454.
.bashrc , PS1, , . . () .
$ cat ~/.bashrc
export TERM=vt100
export PS1="$(hostname -f): "
export CDPATH=:$HOME
stty kill '^u'
$ . ~/.bashrc
bravo.example.com:

#
#
#
#



HOME CDPATH
kill control-u

,
Bourne Again Shell (, ), [, ] $ .
, .8.1
.
.
8.1. ,

()

(. ( ) , . 298)

$( )

(. , . 358)

(( ))

; let (
, , . . 480)

$(( ))

( ,
, . )

[]

test (. test

. 417 [] test . 420, V)

[[ ]]

; [ ], (. ( ) . 482


5 , . , ,
.
, , .
, ,
. -

289

, , ,
, , .
, Bourne Again Shell

. TC Shell,
. 377.
() , .
Linux : 0(
), 1 ( ) 2 ( ).
(>,. . 146) 1>, .
< (. . 147)
0< , .
2> .
. 499.
,
.
cat

. ,
.

$ cat y
This is y.
$ cat x
cat: x: No such file or directory
$ cat x y
cat: x: No such file or directory
This is y.

y


y


, - .
$ cat x y > hold
cat: x: No such file or directory
$ cat hold
This is y.

, ,
. cat tr, . (
tr V.) , cat , ,
, .
$ cat x y | tr "[a-z]" "[A-Z]"
cat: x: No such file or directory
THIS IS Y.

290 8. Bourne Again Shell


. , 2>, ,
( 2). , 1>,
, ( 1). 1>
>.
$ cat x y 1> hold1 2> hold2
$ cat hold1
This is y.
$ cat hold2

cat: x: No such file or directory

&>
. >&
tcsh (.
. 377).

$ cat x y &> hold


$ cat hold
cat: x: No such file or directory
This is y.
y

1>
hold, 2>&1
2 1.
, hold.

$ cat x y 1> hold 2>&1


$ cat hold
cat: x: No such file or directory
This is y.
y

1> hold 2>&1. ,


, hold.
hold .
2 1, 1 tr.
$ cat x y 2>&1 | tr "[a-z]" "[A-Z]"
CAT: X: NO SUCH FILE OR DIRECTORY
THIS IS Y.


1>&2.
echo
. echo :

$ cat message_demo
echo . 1>&2
echo .

291

message_demo,
( , echo) ,
.
,
. lnks (. lnks . 425).
,

exec (. exec:
. 469).

Bourne

Again

Shell
, .8.2.
8.2.

< _

_.

> _

_,

noclobber (. noclobber:
. 148). noclobber ,
( ) _

>| _

_,
noclobber (. noclobber:
. 148)

>> _

_,
noclobber (. noclobber:
. 148). noclobber ,
_,

&> _

<&m

m (.
. 450)

[n] >&m

n, , m (. . 450)

[n]<&

n, (.
. 450)

[n] >&

n,



, , . ,
. , Linux,
. , ,

292 8. Bourne Again Shell


.
.
, ,

( ). , , .
(
bash) . 417, 9 ( tcsh). , .
,
.

chmod:
, ,
, (. 4, ). ,
. ,
, ()
(, ).
,
.
whoson, :
$ cat whoson
date
echo "Users Currently Logged In" ,
who
$ ./whoson
bash: ./whoson: Permission denied

whoson, , .
whoson ,
Permission denied ( ). (. , ,
command not found, .) bash (bash whoson), bash
.
bash , whoson ,
bash , whoson .
.
tcsh.
chmod , . .8.1
ls l, ,
whoson , chmod
. ls

293

(), , .
: u+x
chmod (+) (x) (u). ( u
user
, , .)
. ls Shell
x, Basics 279
, .

279 Wednesday,
September
30, 2009 10:13
AM

chmod

$ ls -l whoson
-rw-rw-r-1 max group 40 May 24 11:30 whoson
$ chmod u+x whoson
$ ls -l whoson
-rwxrw-r-1 max group 40 May 24 11:30 whoson
$ ./whoson
Fri May 22 11:40:49 PDT 2009
Users Currently Logged In
zach
pts/7
May 21 18:17
hls
pts/1
May 22 09:59
sam
pts/12
May 22 06:29 (bravo.example.com)
max
pts/4
May 22 09:08

. 8.1.


Figure
8-1
Using chmodchmod
to make
a shell script
executable

chmod utility changes the access privileges associated with a file. Figure 8-1
The
shows ls with the l option displaying the access privileges of whoson before and
COMMAND NOT FOUND?

./,
:

after chmod gives execute permission to the files owner.


$ whoson

bash:first
whoson:
command anot
found ()
(
)
ls displays
hyphen
as the
fourth
character, indicating that the owner
The

,the
file.



gives the
owner
execute perdoes
not have permission
to execute
Next chmod
u+x

:
chmod
to add
(+) execute
permission
(x) for the owner (u). (The u
mission:
causes
$ ./whoson
stands
for user, although it means the owner of the file.) The second argument is the


second

,


ls shows
an
x in
the fourth
position, indicating
that the
name
of ./
the
file. The
. ,
owner has execute permission.
, , PATH.

Command
found?
not

script

()
including

.
If you give
the name
of a shell
as a command
without
the leading ./,
the shell typtip

,
ically displays
the following
error message:

. , ,
$ whoson
bash: whoson:
command
not
found

,
.

This
message indicates
the shell
is not set (
up to search for executable)
files in the working
directory.
Give

.
this
command
instead:

, .

8.1, , $ ./whoson
, .
The ./ tells
the shell
explicitly
to look for
an executable
file in the
working
directory.
To,
change the

4,

,


V (
environment so the shell searches the working directory automatically, see the section about PATH
ls chmod).
on page 297.

If other users will execute the file, you must also change group and/or public access
permissions for the file. Any user must have execute access to use the files name as

294 8. Bourne Again Shell

#!
, , (
) .
,
exec, .
#!,
, .
, . , bash:
$ cat bash_script
#!/bin/bash
echo "This is a Bourne Again Shell script."
Bourne Again Shell

#! , , , , .
, tcsh:
$ cat tcsh_script
#!/bin/tcsh
echo "This is a tcsh script."
set person = zach
echo "person is $person"

tcsh

#! , tcsh , .
ps f ,
. , ps
, , bash, , tcsh, , ps:
$ cat tcsh_script2
#!/bin/tcsh
ps -f
$ ./tcsh_script2
UID
PID PPID
max
3031 3030
max
9358 3031
max
9375 9358

C
0
0
0

STIME
Nov16
21:13
21:13

TTY
pts/4
pts/4
pts/4

TIME
00:00:00
00:00:00
00:00:00

CMD
-bash
/bin/tcsh ./tcsh_script2
ps -f

#! , , , . #! . #!
, ,
tcsh bash, bash,
, .
sed, #!,
. 597.

295

#

. , Bourne Again Shell TC Shell,
. (#)
,
(!), - ,
. ,
( ).


, ,
,
( ). . fork, exec
( ).
, C,
exec , . ,
exec . exec, ,
, . , , ,
, .
, ,
, , bash exec ,
. bash ,
whoson:
C
fork
exec

$ bash whoson

bash , ,
whoson. ( .) , bash , whoson,
, .
bash , , , ,

.
, .
,
( , ,
). , bash
, ,
, ,
bash tcsh.

296 8. Bourne Again Shell


, ,
, .
, Bourne Again Shell TC Shell,
, 5,
.

(;) (8 )
,
.
, , .
(;) ,
.

(;).
:
$ x ; y ; z

x, y z ,
, . ,
(x, y z),
,
z:
$ x
$ y
$ z


, .
.

\

, ,
(\).
, ,
. ,
,
( tcsh; . prompt2 .392).

( tcsh).
(),
.

297

bash
. ( tcsh, prompt2 . 392).
( ) :
$ echo "Please enter the three values
> required to complete the transaction."
Please enter the three values required to complete the transaction.

, , bash
, , . (>) (.
PS2: () . 315), ,
.
(),
.
$ echo "Please enter the three values \
> required to complete the transaction."
Please enter the three values required to complete the transaction.

:
$ echo 'Please enter the three values \
> required to complete the transaction.'
Please enter the three values \
required to complete the transaction.

| & ,
( | ) (&)
. ,
.
.
, .
, :
$ x | y | z
$ ls -l | grep tmp | less

x y y,
z. ,
, z:, z , y, ,
, , x.
x ls l, y grep tmp, z
, less. ( )
, tmp ,
less. d e ,
f :
$ d & e & f
[1] 14271
[2] 14272

298 8. Bourne Again Shell


, PID , .
f , .
, d e.
, - .
Done (), ,
, .

+, .
.
:
[1][2]+

Done
Done

d
e

,
:
$ d
[1]
[2]
[3]

& e & f &


14290
14291
14292


, (&) . . , ,
. , ,
(|) .
Bourne Again Shell ,
( ):
$ d | e | f &
[1] 14295


TC

Shell
( 1):
tcsh $ d | e | f &
[1] 14302 14304 14306

( )
.
, . ,
. (
. 321.) ()
, , ,
.
a b ,
c.
.

299

$ (a ; b) & c &
[1] 15520
[2] 15521

d & e & f & , a b


, .
, a b c d, .
, a b, , cd,
. .
$ (a ; b) & (c ; d) &
[1] 15528
[2] 15529

.
, .
tar tar,
cd. , tar cd
, cd .
$1 $2
(. $1$n: . 460).
, , cpdir . cd
( , cd).
.
$ cat cpdir
(cd $1 ; tar -cf - . ) | (cd $2 ; tar -xvf - )
$ ./cpdir /home/max/sources /home/max/memo/biblio

cpdir , /home/max/sources,
/home/max/memo/biblio. ,
cp r. cp tar V.


.
. ,
, . , :
$ find . -print | sort | lpr & grep -l max /tmp/* > maxfiles &
[1] 18839
[2] 18876

, &, , , : find (. V),


sort (. sort:
.74 V) lpr (.
lpr
: . 71
V
). , grep.
& , bash
, .
,
( ), .

300 8. Bourne Again Shell

jobs:
jobs ,
. sleep
, jobs:
$ sleep 60 &
[1] 7809
$ jobs
[1] + Running

sleep 60 &

fg:
, , . , , ,
, PID-:
$ xclock &
[1] 1246
$ date &
[2] 1247
$ Tue Dec 2 11:44:40 PST 2008
[2]+ Done
date
$ find /usr -name ace -print > findout &
[2] 1269
$ jobs
[1]- Running
xclock &
[2]+ Running
find /usr -name ace -print > findout &

, , .
, ,
.
jobs , xclock,
1. date ,
, jobs. date
find, find 2.
fg, .
(%), .
2 .
, , .
$ fg 2
find /usr -name ace -print > findout

$ %2
find /usr -name ace -print > findout

, ,
,
.

301

fg %find, fg %f,
2.
, . fg %?ace 2 .
, , , , , jobs
(+). fg
.


( CONTROL+Z) ,
, ( ),
, Stopped ().
CONTROL+Z
[2]+ Stopped

find /usr -name ace -print > findout

. 155.

bg:
, ,
(. ). bg
.
$ bg
[2]+ find /usr -name ace -print > findout &

- ,
, ,
. ,
.
$ (sleep 5; cat > mytext) &
[1] 1343
$ date
Tue Dec 2 11:58:20 PST 2008
[1]+ Stopped
$ fg
( sleep 5; cat >mytext )
Remember to let the cat out!
CONTROL+D
$

( sleep 5; cat >mytext )


cat!

PID-
, , .
, , date, .
( date)
1. fg,

302 8. Bourne Again Shell

, , . CONTROL+D,
EOF (end of file .
.
, , gain Shell , (, -
). ,
home
, , .
, ,
sam
, ,
.
demo
names
literature
notify (.8.13),

.
promo

,
8.2.
,
Figure 8-2 .
The directory
structure
in the examples

.
and

xit from a shell while jobs are stopped, the shell
issues
a warning
jobs

of
,
to review
the list
jobs or
you
immew you to exit.
If
you
then use jobs

shell
.


exit from
the
shell
again, the
allows you
to exit.
If huponexit
(.

8.13) jobs

not set (the
default), huponexit
stopped and
background
keep ,
running in the , shell

the

f it is set, the
terminates
jobs. .
, .
2009 10:13 AM

Directory Stack

ne Again and
the TC
Shells
allow you
to store a list of directories you
with, enabling
you
to
move
easily
among
them.This
list is
referred to ,
, Bourne Again Shell,
TC Shell,
s analogous
to
a
stack
of
dinner
plates:
You
typically
add
plates
to and
,
.
from the top
of
the
stack,
so
this
type
of
stack
is
named
a
last
in,
first
.

ack.
: .
LIFO (last in,
out).
lays thefirst
Stack

n displays the contents of the directory stack. If you call dirs when the

k is empty,dirs:
it displays
the name of the
working directory:
dirs . dirs
, :

ture

$ dirs
~/literature
names

dirs (~). 21 pushd , .8.2.


,
demo
12 pushd
literature

ays the Stack

displays the contents of the directory stack. If you call dirs when the
000.book.fm Page 289 Wednesday, September 30, 2009 10:13 AM
is empty, it displays the name of the working directory:
303

re

Shell Basic
names
pushd

21 pushd

pushd

names

demo

names

demo

names

demo

literature

literature

literature

demo
12 pushd
literature

Figure
Creating
a directory stack
.8-3
8.3.

Figure 8-4.
Using
pushd to change
working
directories
8.4.

pushd

The dirs builtin uses a tilde (~) to represent the name of a users home directo
examples
in the next
several sections assume that you are referring to the di
pushd:

structure shown in Figure 8-2.
pushd (
push
directory

) , , , ,
pushd:
a Directory
on the Stack

Pushes

.
When you supply the pushd (push directory) builtin with one argument, it
.8.3.
the directory specified by the argument on the stack, changes directories
$ pushd ../demo
~/demo ~/literature specified directory, and displays the stack. The following example is illustr
Figure 8-3:
$ pwd
/home/sam/demo
$ pushd ../demo
$ pushd ../names
~/demo ~/literature
~/names ~/demo ~/literature
$ pwd
$ pwd
/home/sam/demo
$ pushd ../names
/home/sam/names

~/names ~/demo ~/literature


pushd
$ pwd
,
,
/home/sam/names
( ),
When you use pushd without an argument, it swaps the top two directories
(.8.4):
stack, makes the new top directory (which was the second directory) the new
$ pushd
ing directory, and displays the stack (Figure 8-4):
~/demo ~/names ~/literature
$ pushd
$ pwd
~/demo ~/names ~/literature
/home/sam/demo
$ pwd


pushd /home/sam/demo
. ,
in this
way, you
can easilymove
back and forth
between
Using
, pushd

- two dire
You
can
also
use
cd

to
change
to
the
previous
directory,
whether
cd . , or not yo
explicitly
a directory
stack.
To access another
directory
in the stack, ca

pushd created

,

.
with
a
numeric
argument
preceded
by
a
plus
sign.
The
directories
, 0. in the st
starting
with the top directory,
which ,
is number
0. The following
numbered
pushd

command
continues
with
the
previous
example,
changing
the
working direc
literature literature :
literature and moving literature to the top of the stack:
$ pushd +2
$ pushd +2
~/literature ~/demo ~/names
~/literature ~/demo ~/names
$ pwd
$ pwd
/home/sam/literature
/home/sam/literature

000.book.fm Page 290 Wednesday, September 30, 2009 10:13 AM

304 8. Bourne Again Shell


290 Chapter 8 The Bourne Again Shell

popd:


popd (pop directory ).
.8.5, popd

, :
$ dirs
~/literature ~/demo ~/names
$ popd
~/demo ~/names
$ pwd
/home/sam/demo

literature
popd
demo
names

Figure 8-5 Using popd


to 8.5.
remove
a directory fro


popd ,

- popd
popd: Pops
a Directory
Off
the
Stack


+.

To remove a directory from the stack, use the popd (pop direc
1, demo. ,
and

lowing
example
Figure 8-5 show, without an argumen
,
.
directory from the stack and changes the working directory t

$ dirs
~/literature ~/demo ~/names
$ popd +1
~/literature ~/names

$ dirs
~/literature ~/demo ~/names
$ popd
~/demo ~/names
$ pwd
/home/sam/demo

To remove a directory other than the top one from the


numeric
argument
preceded by
a plus sign.
Thefollowing
, tory
.

number 1, demo.
Removing
a directory
other than d
. ,not
change
the

,
working
directory.
, $ dirs
. ,
~/literature ~/demo ~/names
. A76, MY_CAT ___X___
,
$ popd
+1
69TH_STREET ( ) ~/literature
MY-NAME (
~/names )
.
,


, .
Variables
,



.
Within
a shell,
a shell parameter
is associated
with a value
user.

-Parameters
There
are several
kinds of shell
parameters.
(
)
are

letters, digits,
and underscores
often
referred to as sh
, variables.
,

A variable
name muststart
with a letter or under
. ,

and

ber. Thus
A76,
MY_CAT,
___X___
are valid va

,

69TH_STREET
(starts with
a digit)
and
MY-NAME (conta

.
User-created Shell variables that you name and assign values to are user

ofuser-created

variables can change
the values
variables at any time,
. 454. Bourne
Again
Shell

- You can
readonly so that their values cannot
be changed.
:
variables global. A global variable (also called an environme

Parameters and Variables

to all shells and other programs you fork from the original
vention is to use only uppercase letters for global variables a

305

(=) .
:
$ myvar=abc

TC Shell
set, :
$ set myvar = abc


Bourne
Again

Shell

.
,
, .
my_script TEMPDIR.
my_script TEMPDIR, /home/sam/temp. echo ,
TEMPDIR my_script.
TEMPDIR , my_script .
$ cat my_script
echo $TEMPDIR
$ TEMPDIR=/home/sam/temp ./my_script
/home/sam/temp
$ echo $TEMPDIR
$

, (
, )
, . ( , )
,
. HOME, , PATH, ,
.
, . ,
- .
, ,
.
,
/etc/profile /etc/csh.cshrc. -
bash tcsh, ,
( bash . . 284
, tcsh . . 393). ,
, ,

.
.
,

306 8. Bourne Again Shell



. (, 1,? #) (
) ($1, $? $#).

.
(. . 459).
, .
set (. set: . 461)
.
, ,
,
(. . 457).
.


person ,
max ( tcsh
setperson=max):
$ person=max
$ echo person
person
$ echo $person
max

echo , . , person max.


person person. , ($).
echo$person person, $person, $person echo .
$ $person , echo. , , ,
.

, $.
, (\).

$ echo $person
max

307
$ echo "$person"
max
$ echo '$person'
$person
$ echo \$person
$person

,
, , . ,
,
. , .

$ person="max and zach"


$ echo $person
max and zach
$ person=max and zach
bash: and: command not found

and:

,
, , . , ,
, :
$ person="max
and
$ echo $person
max and zach
$ echo "$person"
max
and
zach

zach"


. , * ? ,

.
memo max*. Bourne Again Shell ,
bash (.
. 157), .
.
, bash ( tcsh) , . , echo,
(*) $memo bash
memo
echo:
$ memo=max*
$ echo "$memo"
max*

308 8. Bourne Again Shell


, ,
.
memo
, :
$ ls
max.report
max.summary
$ echo $memo
max.report max.summary

$memo max*, max* max.report


max.summary echo.
$
${}, ${}. -

. , :
$
$
$
$

PREF=counter
WAY=$PREFclockwise
FAKE=$PREFfeit
echo $WAY $FAKE

, .
. , PREFclockwise PREFfeit
, .
(bash) (tcsh).
, PREF :
$ PREF=counter
$ WAY=${PREF}clockwise
$ FAKE=${PREF}feit
$ echo $WAY $FAKE
counterclockwise counterfeit

Bourne Again Shell ,


$1, $2, $3 , $9.
, , : ${10}.
$0 (. $0: . 459).

unset:
, ,
, . , ( tcsh
set person = ):
$ person=
$ echo $person
$

unset.
person:
$ unset person

309


, .

readonly:

, readonly ( tcsh).
person .
,
. , , :
$ person=zach
$ echo $person
zach
$ readonly person
$ person=helen
bash: person: readonly variable

person:

readonly ,
, .
, , , ,
, ,
. . ( readonly
declare r . ???).

declare typeset:
declare () typeset ( ) (
, tcsh)
. .8.3
.
8.3. ( typeset declare)

(.
.???)

(. . 363)

(. . 311)

, . readonly

( ), .
export (. export . 454)

. person1

310 8. Bourne Again Shell


max. , declare,

.
$
$
$
$

declare
declare
declare
declare

person1=max
-r person2=zach
-rx person3=helen
-x person4

readonly export declarer


declare x . , ,
person4.
person4 (
). ,
.
declare ( ).

person3:
$ declare -x -r person3=helen

, +. .
, person3 ,
- .
$ declare +x person3

declare typeset.
declare ,
. set (. set:
. 461). declare , ,
, . ,
declare r ,
. ,
readonly, . :

$ declare -r
declare -ar BASH_VERSINFO='([0]="3" [1]="2" [2]="39" [3]="1" )'
declare -ir EUID="500"
declare -ir PPID="936"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:"
declare -ir UID="500"
declare -r person2="zach"
declare -rx person3="helen"

, ,
.
(i). a ,
BASH_VERSINFO ; .

311

.
, , . integer .
integer :

$ declare -i COUNT

,
, , ,
.
. ,
,
, . , ,
, export (bash; . export . 454)
setenv (tcsh; . . 384).

HOME:
,
. ,
Linux /etc/passwd. Mac OS X
Open Directory.
$ grep sam /etc/passwd
sam:x:501:501:Sam S. x301:/home/sam:/bin/bash

HOME. cd
, HOME:
$ pwd
/home/max/laptop
$ echo $HOME
/home/max
$ cd
$ pwd
/home/max

HOME cd. cd, ,


, HOME,
.
HOME
, (~) (. ~ () . 105).
echo, ,
(~)

312 8. Bourne Again Shell


Max laptop,
, ls:
$ echo ~
/home/max
$ ls ~/laptop
tester count lineup

PATH: ,

,
,
. , , . ,

, .
, ( ) , .
, ( ) ( ).
PATH.
PATH bash tcsh. ,
. , ,
. /bin /usr/bin, , .
, ,
, ,
PATH, .
PATH ,
. .
PATH, /usr/local/bin. ,
/bin, /usr/bin. , ~/bin, . ,
. PATH :

$ export PATH=/usr/local/bin:/bin:/usr/bin:~/bin:

.
( - )
.
( -

313

, ), ( ) .
, (.).
tcsh path PATH . 391.
Linux bin (binary
), ~/bin. (
) PATH,
, , PATH .
PATH
PATH
. root-, PATH
.
PATH .
, ./, . /myprog.

PATH

.

ls
.
ls PATH ,
, ls , ls
ls, ,
.

PATH - ,

( ,
). /usr/local/bin
PATH bin,
(~/bin), :
$ PATH=/usr/local/bin:$PATH:~/bin

MAIL:
MAIL ( tcsh mail) ,
( , /var/mail/,
). MAIL
MAILPATH (. ),
, MAIL.
MAIL,
( ,
).
Mac OS X
.
. ,
MAIL , ,
- .
MAILPATH ( tcsh)
, . ,

314 8. Bourne Again Shell


(, ). ,
, (?),
. ,
, .
MAILCHECK ( tcsh) .
, 60

. , .

PS1: ()
Bourne Again Shell
($). bash root-
(#).
PS1 ( tcsh prompt; . prompt . 392)
, , ,
. PS1
prompt .
, PS1, . ,
,
$ PS1="[\u@\h \W \!]$ "

:
[@ ]$

,
, ,
(. . 325) .
, . ,
, ( ).
(. . 358) , PS1:
$ PS1="$(hostname): "
bravo.example.com: echo test
test
bravo.example.com:

tcsh :
tcsh $ set prompt = "'hostname': "


, (, root-,
). ,
.
(
):
$ PS1='\h \$ '

315
bravo $
$ PS1='\@ \u $ '
09:44 PM max $
$ PS1='\$ '
$

.8.4 ,
PS1.
tcsh . 9.4. ,
, man-,
bash, PROMPTING (
/PROMPTING, n).
8.4. , PS1

\$

#, root-, $

\w

\W

\!

() (. !n . 330)

\d

, ,

\h

\H

\u

\@

12- AM/PM

\T

12- ::

\A

24- :

\t

24- ::

PS2: ()
PS2 . ( tcsh
prompt2; . prompt2 . 392.) echo .
, , , (>). , .
, , .
:
$ echo "
> 2"

2
$ PS2=" : "
$ echo "
: 2"

2

316 8. Bourne Again Shell


:,
. echo.

PS3:
PS3 ( tcsh prompt3;
. prompt3 . 393) (. PS3
. 496).

PS4:
PS4 bash (. . 428; tcsh ).

IFS: ( )
IFS (Internal Field Separator ),
tcsh, , .
, ,
(SPACE TAB NEWLINE). IFS,

, , . IFS
, ,
. .
IFS

IFS ,

. , IFS
.
. ,
IFS bash;
, ,
IFS .

, IFS
:
$ a=w:x:y:z
$ cat $a
cat: w:x:y:z: No such file or directory
$ IFS=":"
$ cat $a
cat: w: No
cat: x: No
cat: y: No
cat: z: No

such
such
such
such

file
file
file
file

or
or
or
or

directory
directory
directory
directory

317

cat a, w:x:y:z , cat.


cat w:x:y:z ,
. IFS (:), , ,
cat. cat
, : w, x, y z. ,
(:), .

, IFS. , . :
$ IFS="p"
$ export VAR

IFS p, p export
, export .

export:
$ IFS="p"
$ aa=export
$ echo $aa
ex ort

, p export ( echo).
aa VAR $aa VAR exortVAR.
ex : ort VAR.
$ $aa VAR
2 files to edit
"ort" [New File]


"ort" [ ]

Ex. visual, .
:q
E173: 1 more file to edit

:q
$
IFS ,

-

,
- .

CDPATH: cd
CDPATH ( tcsh cdpath)
cd , .

318 8. Bourne Again Shell


, ,
cd
, .
CDPATH cdpath cd , cd
, . , cd .
CDPATH cdpath , cd
, CDPATH. , . CDPATH cdpath
cd
, CDPATH cdpath.
CDPATH cdpath ,
, ( PATH).
~/.bash_profile (bash) ~/.tcshrc
(tcsh) , :
export CDPATH=$HOME:$HOME/literature

tcsh :
setenv cdpath $HOME\:$HOME/literature

cd cd
, literature, .
CDPATH cdpath , cd ,
,
CDPATH cdpath. , cd , CDPATH
, (::):
export CDPATH=::$HOME:$HOME/literature

cd ,
, (/), CDPATH cdpath.

, :
.8.5 bash, .
tcsh
. 390.
8.5. bash,

BASH_ENV

( . 285)

CDPATH

cd ( CDPATH

: cd . 317)

COLUMNS

, select (
select . 445)

319

FCEDIT

, fc (
fc: ,
. 327)

HISTFILE

, (
~/.bash_history; . . 324)

HISTFILESIZE

, , HISTFILE
( 500; . ,
.324)

HISTSIZE

,
( 500; . . 324)

HOME

( HOME:
. 311);
cd ( ~ ()
. 104)

IFS

( IFS:
( ) . 316); (
. 359)

INPUTRC

Readline (
~/.inputrc; . .inputrc:
Readline . 338)

LANG

,
LC_*

LC_*

, ,
LC_COLLATE, LC_CTYPE, LC_MESSAGES LC_NUMERIC;
locale

LINES

, select (
select . 445)

MAIL

,
( MAIL:
. 313)

MAILCHECK

bash,
( MAIL:
. 313)

MAILPATH

, bash ( MAIL:
. 313)

PATH

, bash ( PATH:
, . 312)

PROMPT_COMMAND

, bash

PS1

1; ( PS1:
() . 314)

PS2

2; ( >; .
PS2: () . 315)

PS3

, select ( select . 445)

320 8. Bourne Again Shell


8.5 ()

PS4

bash ( . 428)

REPLY

, read
( REPLY . 467); select
( select . 445)


.8.6 ,
bash tcsh.
8.6.

NEWLINE (- ( (;)

() . 296)

( (;) ()
. 296)

()

( . 298)
(
. 369)

(( ))

(
. 356)

&

(
, | & , . 297)


(; | & ,
. 297)

>

( . 146)

>>

(
. 150)

<

(
. 147)

<<

Here document ( Here document


.442)


( * . 158)

( ? . 157)

( . 66)

(
. 66)

,
( . 65
$ . 306)

321

(
. 358)

[]

(
[ ] . 160)

( . 304)

. (

( . () source:
. 287)

(# . 295)

{}

( . 363)

: (
null)

true ( : ( )
. 464)

&& ( )


( ; . . 486)

|| (

( ; . 486)

! ( )

$() ( tcsh)

( ; .
. 358)

[]

(
. 356)

Linux. ,
, ,
. Linux

, . , ,
, .
, , ,
. ,
cd, .


, , , . , , , . (
(fork) , , , . , ,
, .
(spawn); .) , ,
, fork().

fork

322 8. Bourne Again Shell


Linux,
init, , , PID-1. ,
: , .
, init getty mingetty,
login:.
, getty login, .
login .


Linux PID-
(process identification ). , PID-. .
, , PID- () . - PID-, .
,
( ), , ps. f ps .
ps, CMD bash , ,
. , PID, PID-.
, PPID, PID- .
PID PPID , ,
(PID 21341),
, sleep (PID 22789).
PID- sleep , PID- (21341).
PID-

$ sleep 10 &
[1] 22789
$ ps -f
UID
PID PPID C STIME TTY
max
21341 21340 0 10:42 pts/16
max
22789 21341 0 17:30 pts/16
max
22790 21341 0 17:30 pts/16

TIME CMD
00:00:00 bash
00:00:00 sleep 10
00:00:00 ps -f

ps ,
f, V. sleep ps f ,
- ,
sleep:
$ sleep 10 &
[1] 22791
$ ps -f

323
UID
max
max
max

PID PPID C STIME TTY


21341 21340 0 10:42 pts/16
22791 21341 0 17:31 pts/16
22792 21341 0 17:31 pts/16

TIME CMD
00:00:00 bash
00:00:00 sleep 10
00:00:00 ps -f

, pstree ( ps

for
est e). pstree
p, PID-:
$ pstree -p
init(1)-+-acpid(1395)
|-atd(1758)
|-crond(1702)

|-kdeinit(2223)-+-firefox(8914)---run-mozilla.sh(8920)---firefox-bin(8925)
|
|-gaim(2306)
|
|-gqview(14062)
|
|-kdeinit(2228)
|
|-kdeinit(2294)
|
|-kdeinit(2314)-+-bash(2329)---ssh(2561)
|
|
|-bash(2339)
|
|
'-bash(15821)---bash(16778)
|
|-kdeinit(16448)
|
|-kdeinit(20888)
|
|-oclock(2317)
|
'-pam-panel-icon(2305)---pam_timestamp_c(2307)

|-login(1823)---bash(20986)-+-pstree(21028)
|
'-sleep(21026)

.
, kdeinit, , , , , firefox, gaim oclock.
, login, , , , sleep
pstree. , PID-, 10,
$$: PID- . 457.


, , ,
(, fork()) ,
. , ( sleep()). , ().
,
( ),
fork
sleep

324 8. Bourne Again Shell


. ( )

.
(&) ,
, ,
. , , PID-
.
, .


, .
.
. 161.

, ,
, , , .
. ,
. () export bash
(. export . 454) setenv
tcsh (. . 384).

,
C
Shell

, , ,
, .

.
, .
. ,
, , ,
.
history ( , bash, tcsh) . , ,
, .

,
, TC

Shell

, , bash, .
. 372.

325

HISTSIZE ,
.
100 1000.

, HISTFILE (
~/.bash_history).
. HISTFILESIZE , , HISTFILE (. 8.7).


(
), , ,
. ,
.

8.7.

HISTSIZE

500

HISTFILE

~/.bash_history

HISTFILESIZE

500

Bourne Again Shell


.
bash, PS1 \! (.
PS1: () . 314).
,
.
~/.bash_
profile, ,
, 100 :

$ HISTSIZE=100

bash 100
:
$ HISTFILESIZE=100

HISTFILESIZE
, 100
.
, history.
. tcsh
.
,
. history,
.

326 8. Bourne Again Shell


32 $ history | tail
23 PS1="\! bash$ "
24 ls -l
25 cat temp
26 rm temp
27 vim memo
28 lpr memo
29 vim memo
30 lpr memo
31 rm memo
32 history | tail


history .
less
history 10 ( history | tail)
.

HISTORY
.
h 10 .
hg ,
string
. ,
~/.bashrc.
. 363.
$ alias 'h=history | tail'
$ alias 'hg=history | grep'


.
, .
, , .
,
: fc ( ),
(.
(!) . 329)
Readline
, , vi emacs (. Readline).

?
vi emacs C Shell TC
Shell, fc Readline. C Shell
TC Shell, .
, ,
Readline, Linux,
.

327

fc: ,

fc (fix command ) ( tcsh )
,
. ,
.

fc l .
, fc l 16

, :
$ fc -l
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

cd
view calendar
vim letter.adams01
aspell -c letter.adams01
vim letter.adams01
lpr letter.adams01
cd ../memos
ls
rm *0405
fc -l
cd
whereis aspell
man aspell
cd /usr/share/doc/*aspell*
pwd
ls
ls man-html

fc l , . , :
fc l [ []]

fc , , . ,
,
n- . fc .
, fc , , ,
.
1030
1035:
$ fc -l 1030 1035
1030
cd ../memos
1031
ls
1032
rm *0405
1033
fc -l

328 8. Bourne Again Shell


1034
1035

cd
whereis aspell

,
view,
, whereis:
$ fc -l view whereis
1025
view calendar
1026
vim letter.adams01
1027
aspell -c letter.adams01
1028
vim letter.adams01
1029
lpr letter.adams01
1030
cd ../memos
1031
ls
1032
rm *0405
1033
fc -l
1034
cd
1035
whereis aspell

, .
1027:
$ fc -l 1027 1027
1027
aspell -c letter.adams01


fc .
fc [e ] [ []]

fc e, , fc
, ,
. fc nano.
. vim
:
$ fc -e vi

fc vim.
FCEDIT, e
. FCEDIT /usr/bin/emacs fc
,
emacs ( emacs,
):
$ export FCEDIT=/usr/bin/emacs
$ fc

, fc
. 1029
. :
$ fc 1029

329

, . ,
vim, 1030:
$ fc vim 1030
FC

fc , ,
, . ,
.


.
fc s,
. 1029:
$ fc -s 1029
lpr letter.adams01

:
$ fc s

fc
. 1029 adams john, :
$ fc -s adams=john 1029
lpr letter.john01

(!)

C

Shell

. , bash tcsh,
, fc,
. , !! ,
!$ .
, ,
.
, , (!).

.
.
, ,
,
(\) .

.
.8.8.

330 8. Bourne Again Shell


!!

!! . 45
44:

44 $ ls -l text
-rw-rw-r-1 max group 45 Apr 30 14:53 text
45 $ !!
ls -l text
-rw-rw-r-1 max group 45 Apr 30 14:53 text

!! , . ,

.
, , .
, .
. , , . , !3
.
,
, ( 3 4).
44:
!n

51 $ !44
ls -l text
-rw-rw-r-52 $ !-8
ls -l text
-rw-rw-r--

1 max group 45 Apr 30 14:53 text


1 max group 45 Apr 30 14:53 text

,
,
. , , . , .
!

68 $ history 10
59 ls -l text*
60 tail text5
61 cat text1 text5 > letter
62 vim letter
63 cat letter
64 cat memo
65 lpr memo
66 pine zach
67 ls -l
68 history
69 $ !l
ls -l

331

70 $ !lpr
lpr memo
71 $ !?letter?
cat letter

8.8.

, , , = (

!!

!n

!n

n-

!? [?]

, . ?

!#

( , )

!{}

.
. , !{3}3
, 3


() . ( .8.9.) ( ),
1 ( , ), n ( ).
,
(, !14) . , !14:3
14. ,
( 1), (^),
($). , .
72 $ echo apple grape orange pear
apple grape orange pear
73 $ echo !72:2
echo grape
grape
74 $ echo !72:^
echo apple
apple
75 $ !72:0 !72:$
echo pear
pear
76 $ echo !72:2-4
echo grape orange pear
grape orange pear
77 $ !72:0-$
echo apple grape orange pear
apple grape orange pear

332 8. Bourne Again Shell

, !$
. , , cat :
$ cat report.718

$ vim !$
vim report.718

, .
,
.

78 , , ;
5.
78 $ !72 ; echo helen zach barbara
echo apple grape orange pear ; echo helen zach barbara
apple grape orange pear
helen zach barbara
79 $ echo !78:7
echo helen
helen
80 $ echo !78:4-7
echo pear ; echo helen
pear
helen
8.9.

n- . 0

( )

mn

m n; m, 0 (0n

n*

. , 1*

, ??

- .
, ,
.
,
, .
(:).

$ car /home/zach/memo.0507 /home/max/letter.0507


bash: car: command not found

$ !!:s/car/cat
cat /home/zach/memo.0507 /home/max/letter.0507

333

:
[g]s///

( ), , .
. s g ( gs///),
. /,
, .
, . vim, (&)
. (s///) , ??.
. ,
.
(^). ,

$ ^^^

,
$ !!:s///

cat car
$ ^car^cat
cat /home/zach/memo.0507 /home/max/letter.0507

,
. ,
, .
( )
, . ,
(:).

ls,
( p ),
( h), :
$ ls /var/log/messages
/var/log/messages
$ !!:p
ls /var/log/messages
$ !!:h:p
ls /var/log

.8.10 , .
8.10.

e (extension )

h (head )

p (print-not )
q (quote )

334 8. Bourne Again Shell

8.10 ()

r (root )

t (tail )

, q,

Readline

Bourne
Again

Shell

Readline, ,
C

. , Readline

, , , bash. ,
Readline ( bash), ~/.inputrc (.
.inputrc: Readline . 338) . bash noediting.
bash Readline
: emacs vi(m). , emacs vim.
, .
.
, Readline (. Readline . 336).
emacs, vi:
vi

$ set -o vi

emacs


emacs:

$ set -o emacs

vi
, vi.
bash vi, (. . 172).
, ESCAPE
vim.
vim.
vim. vim , .
k
. j

335
, .
k j

; ,
.


vim , vim-
.
, , . ESCAPE .


(?), ,
, , . ,
(/).
vim, .
(^), , . vim,
n
.
. (
ESCAPE), , G
.
/, ? G
, :
. ,
, , vim, x ( ), r ( ), ~ ( ) . ( ). , (i, I), (a, A),
(R) (c, C).
; , . vim- . 214.

emacs
vim emacs .
, emacs (.
. 231), emacs
. vim, emacs
, . emacs emacs.
emacs ,
CONTROL, ESCAPE. -

336 8. Bourne Again Shell


, CONTROL+B.
CONTROL+F. vim,
. ,
ESCAPE; .
vim, emacs . , ESCAPE b ESCAPE
f. , ,
ESCAPE, , escape.
CONTROL+A;
CONTROL+E. c,
CONTROL+X CONTROL+F, c.
, , , .
, ,
(. . 48) , .

CONTROL+D
, CONTROL+D.
CONTROL+D , .

,
(. 2, ). ,
.
, CONTROL+K.
emacs 7.

Readline
, , TAB. , ,
, vi, emacs, , tcsh. .
, ,
TAB.

( ), TAB , bash
, . ,
, bash .
, bash . ,
vi bash , emacs
. TAB bash ,

337

,
.
bz TAB.
( emacs), ,
bz .
TAB, , bz,
:
$ bz TAB ()TAB
bzcat
bzdiff
bzcmp
bzgrep
$ bz

bzip2
bzip2recover

bzless
bzmore

c TAB.
, bzc. a,
TAB. , bzca
.
$ bzcTAB () TAB
bzcat bzcmp
$ bzca TAB t


, TAB, bash .
, bash
. , bash
, , .
, , TAB,
( emacs, vi
). , .
$ cat films/dar TAB () cat films/dark_

films, dar,
k_, bash ,
.
_. TAB.
bash , ,
_.
$ cat films/dark_ TAB () TAB
dark_passage dark_victory
$ cat films/dark_

,
, TAB, bash .
_ p, TAB, :
$ cat films/dark_p TAB assage

, ,

338 8. Bourne Again Shell


. , bash
(/).

TAB, bash
. ,
, TAB
:
$ echo $HO TAB TAB
$HOME
$HOSTNAME $HOSTTYPE
$ echo $HOM TAB E


,
.

.inputrc: Readline
Bourne Again Shell
, Readline

, , INPUTRC. ,
~/.inputrc.
, (#), .

Readline .inputrc
, :
set

.8.11 ,
. man-
info-, bash.
8.11. Readline

editing-mode

vi Readline vi.
emacs Readline emacs ( ). set o vi set o emacs (.
Readline . 334)

horizontal-scroll-mode

on , .

.
,
. off,

339

mark-directories

off Readline (/) . o

mark-modified-lines

on Readline

. off


,
Readline, ,
. emacs, Readline
, . , , , :
: _
"_": _

. , CONTROL+U
control-u.
.
, ,
.
escape- emacs-:
CONTROL (\C), META (\M) ESCAPE (\e). : \\. ,
: \" \'.
kill-whole-line , emacs,
. ( ) CONTROL+R, .inputrc
:
control-r: kill-whole-line

Readline,
bind Ps. ,
. , vi-, vi. , vi-next-word viprev-word
. , vi, , , emacs-.
, ,
bind q:
bind

$ bind -q kill-whole-line
kill-whole-line can be invoked via "\C-r".
kill-whole-line "\C-r".

, ( emacs):
"QQ": "The Linux Operating System"

bash QQ The Linux Operating System.

340 8. Bourne Again Shell



$if, .inputrc
. :
$if [=]

[$else
]
$endif

mode, term bash. (


, )
. ( ,
),
, , , .
$if .
1. .
$if mode=vi

, Readline vi, .
vi emacs.
2. .
$if term=xterm

, TERM
xterm. TERM .
3. .
$if bash

, bash,
, Readline.
.
Readline
, . Readline
bash .
.inputrc , CONTROL+Y
, ,
bash, vi emacs:
$ cat ~/.inputrc
set editing-mode vi
$if mode=vi
"\C-y": vi-next-word
$else
"\C-y": forward-word
$endif

bash , .inputrc .
set CONTROL+Y.

341

Readline , man, bash, /^READLINE, READLINE


.

READLINE ,

Bourne Again Shell ~/.inputrc .
, ,
.

(alias) ( ),
( ) . . ~/.bashrc (bash) ~/.tcshrc (tcsh),
.
bash alias :
alias [[=]]

tcsh :
alias [[ ]]

bash .
, .
tcsh, bash . ,
bash (. . 363).
,
, , :
$ alias ls='ls -F'

. ( ) . , alias.
, alias .
unalias.
alias ,
:
$ alias
alias ll='ls -l'
alias l='ls -ltr'
alias ls='ls -F'
alias zap='rm -i'

Linux
. ,
alias. .

342 8. Bourne Again Shell


,
. ,
, , . ,
, .
.
PWD .
Max .
dirA ,
. alias dirA
dirA , :
$ echo $PWD
/home/max
$ alias dirA="echo $PWD"
$ alias dirA
alias dirA='echo /home/max'

Max dirB, ,
$PWD. alias dirB ,
dirB - $PWD:
$ alias dirB='echo $PWD'
$ alias dirB
alias dirB='echo $PWD'

dirA dirB Max cd,


cars . ,
,
, ( ).
dirB :
$ cd cars
$ dirA
/home/max
$ dirB
/home/max/cars


, , ,
. ,
, . , ,
, ,
./.


r
r abc , abc:
$ alias r='fc -s'

343

ls ltr, , ls ltr, l:
$ alias l='ls
$ l
total 41
-rw-r--r-- 1
-rw-r----- 1
-rw-r--r-- 1
-rw-r--r-- 1
drwxrwxr-x 2
drwxrwxr-x 2
-rwxr-xr-x 1
drwxrwxr-x 2

-ltr'
max
max
max
max
max
max
max
max

group
group
group
group
group
group
group
group

30015
3089
641
484
1024
1024
485
1024

Mar
Feb
Apr
Apr
Aug
Sep
Oct
Oct

1
11
1
9
9
10
21
27

2008 flute.ps
2009 XTerm.ad
2009 fixtax.icn
2009 maptax.icn
17:41 Tiger
11:32 testdir
08:03 floor
20:19 Test_Emacs


.
rm zap:
$ alias zap='rm -i'
$ zap f*
rm: remove 'fixtax.icn'? n
rm: remove 'flute.ps'? n
rm: remove 'floor'? n

i rm , . rm
rm i: alias rm='rm i'.
ls l
ll ls F ls:
$ alias ls='ls -F'
$ alias ll='ls -l'
$ ll
total 41
drwxrwxr-x 2 max
drwxrwxr-x 2 max
-rw-r----- 1 max
-rw-r--r-- 1 max
-rw-r--r-- 1 max
-rwxr-xr-x 1 max
-rw-r--r-- 1 max
drwxrwxr-x 2 max

group
group
group
group
group
group
group
group

1024 Oct 27 20:19 Test_Emacs/


1024 Aug 9 17:41 Tiger/
3089 Feb 11 2009 XTerm.ad
641 Apr 1 2009 fixtax.icn
30015 Mar 1 2008 flute.ps
485 Oct 21 08:03 floor*
484 Apr 9 2009 maptax.icn
1024 Sep 10 11:32 testdir/

F ls (/) (*) . ,
ll (ls l), (ls). , ,
. ls,
,
ls F l. ( , ls , ,
.)
= ,
alias .

344 8. Bourne Again Shell


- , alias
:
$ alias ll l ls zap wx
alias ll='ls -l'
alias l='ls -ltr'
alias ls='ls -F'
alias zap='rm -i'
bash: alias: wx: not found

, -
(\):
$ \ls
Test_Emacs XTerm.ad
Tiger
fixtax.icn

flute.ps
floor

maptax.icn
testdir


, -
:
$ ll f*
-rw-r--r--rw-r--r--rwxr-xr-x

1 max
1 max
1 max

group
group
group

641 Apr 1 2009 fixtax.icn


30015 Mar 1 2008 flute.ps
485 Oct 21 08:03 floor*

unalias.
zap alias, :
$ unalias zap
$ alias
alias ll='ls -l'
alias l='ls -ltr'
alias ls='ls -F'
$ zap maptax.icn
bash: zap: command not found

bash ( tcsh )
, . , , ,
, , .
( )
, . , , . ,
( ) .
~/.bash_profile, ,
, .
unset.
.

345


,
unset . unset
, .

:
[function] _ ()
{

function , _
, , , . ,
, .
({) . ,
. , break (. break continue . 438).
,
.
process , , .process.out:
start_process() {
process > .process.out 2>&1 &
}

, ,
, . , whoson, chmod:
. 292. . , (>)
(PS2), .
$ function whoson ()
> {
> date
> echo "Users Currently Logged On"
> who
> }
$ whoson
Sun Aug 9 15:44:58 PDT 2009
Users Currently Logged On
,
hls
console
Aug 8 08:59 (:0)
max
pts/4
Aug 8 09:33 (0.0)
zach
pts/7
Aug 8 09:23 (bravo.example.com)

, whoson ,
~/.bash_profile.
.bash_profile, . (),
:

346 8. Bourne Again Shell


$ cat ~/.bash_profile
export TERM=vt100
stty kill '^u'
whoson ()
{
date
echo "Users Currently Logged On"
who
}
$ . ~/.bash_profile

.
(.
. 459). arg1, :
$ arg1 ( ) {
> echo "$1"
> }
$ arg1 first_arg
first_arg

switch(),
. 286. . 455.
,
tcsh. env
setenv:
$ cat .bash_profile

# setenv tcsh
function setenv()
{
if [ $# -eq 2 ]
then
eval $1=$2
export $1
else
echo "Usage: setenv NAME VALUE" 1>&2
fi
}
$ . ~/.bash_profile
$ setenv TCL_LIBRARY /usr/local/lib/tcl
$ env | grep TCL_LIBRARY
TCL_LIBRARY=/usr/local/lib/tcl

$# (. $#:
. 459) , . , bash $1=$2,
eval. $1=$2 ($), . TCL_LIBRARY=/usr/local/lib/tcl,
. eval ,
.

eval

bash: 347

bash:
, bash,
set shopt.


: .
, ; ,
. , bash, . .8.12
.
8.12.

Help ()

help

No edit
( )

- noediting
Readline (. Readline . 334)

No profile
(. noprofile
( ) . 285): /etc/profile, ~/.bash_profile, ~/.bash_login
~/.profile
No rc
( rc)

~/.bashrc (. norc
.bashrc). sh,

POSIX

bash POSIX

posix

Version
()

bash

version

Login
( )

bash

l (
l
)

shopt

opt shopt (. []O [opt]


). O ( O ) ;
+O

End of options
.
( ) ,
()


Bourne

Again

Shell

. set ,
shopt .
, bash.

, , ?
,
, , . bash info-
options ,
values of variables controlling optional shell behavior.

348 8. Bourne Again Shell

set o:
bash set ( tcsh set,
) o +o ,
bash . ,
noclobber (. noclobber:
. 148):
$ set -o noclobber

(
):
$ set +o noclobber

set o , set,
( on off). set+o
,
. bash .8.13.

shopt:
shopt (shell option ),
tcsh, , bash, , . , bash
, (.)
( s set ):
$ shopt -s dotglob

(
), u unset :
$ shopt -u dotglob

shopt ,
:
$ shopt dotglob
dotglob
off

shopt . shopt s
, ,
on. shopt u
(off) . bash .8.13.

SET O SHOPT
shopt ,
set o.
shopt s u o. , ,
noclobber:
$ shopt -o -s noclobber

bash: 349
8.13. bash

allexport

set o allexport

set a

braceexpand

bash
( ;
. ,
. 353)

set o
braceexpand

set B

cdspell


,
cd

shopt s cdspell

cmdhist

shopt s cmdhist

dotglob

shopt s dotglob
,

(
. 157)
.
. . .. ,

emacs

emacs
( ; .
emacs . 335)

set o emacs

errexit

bash
(

set o errexit

execfail


,
exec. , exec
, ,

shopt s execfail

expand_aliases ( -

. 363) (
)
hashall

bash ,
,
PATH ( PATH:
, . 312),

set e

shopt s expand_
alias

set o hashall

set h

350 8. Bourne Again Shell


8.13 ()

histappend

shopt s
bash HISTFILE histappend
( ,
. 324). bash

histexpand


( ; .
(!) . 329).

set o histexpand set H

history

(
; .
.324)

set o history

huponexit

bash SIGHUP

,

shopt s
huponexit

ignoreeof

, bash

EOF.

set o ignoreeof

monitor

(
; . . 299)

set o monitor

nocaseglob

,
(
. 157)

( )

shopt s nocase
glob

noclobber


( ; .

noclobber
: . 148)

set o noclobber

set C

noglob

(
; .
. 157)

set o noglob

set f

notify

set o notify

( . 362)

.

set b

nounset

unset.
unset

set o nounset

set m

set u

351

nullglob

shopt s nullglob
bash
(
. 157),
, .
bash

posix

bash POSIX

set o posix

verbose

,
bash

set o verbose

vi

vi (
vi . 334

set o vi

xpg_echo

echo - shopt s xpg_echo


,
c
e ( echo -e
.442)

xtrace

( . 428)

set o xtrace

set v

set x


bash
, bash .
bash, if case,
, . bash
, ,
.
bash (PS2, >; .
PS2: () . 315,
:
$ echo 'hi
> end'
hi
end
$ function hello () {
> echo hello there
> }
$

bash
.

352 8. Bourne Again Shell


. 326 ,
. , !!, , ,
.
, set +o histexpand.
( )
.


(. )
.
.
, shopt u expand_aliases.


bash .
( ),
.
, .
.
(cp, ~/letter .):
$ cp ~/letter .


,
, , . , , , ,
, .
,
Bourne Again Shell, , TC
Shell
, 9, () . 371.
Bourne Again Shell
.
. , ,

353

( "$@" .
$* $@ . 463).
1. , .
2. .
3. .
4. .
5. .
6. .
7. .
8. .
bash , , , .
.


, bash , .
, ,
, , , bash .
$ SENDIT="> /tmp/saveit"
$ echo xxx $SENDIT
xxx > /tmp/saveit
$ cat /tmp/saveit
cat: /tmp/saveit: No such file or directory

.
, ,
SENDIT. > /tmp/saveit bash echo, . /tmp/saveit .
. ,
-. , .
.

,
, ,
C Shell, ,
.
,

354 8. Bourne Again Shell


;
.

, set +o braceexpand.
(.
. 308).
, , . ls ,
. echo ,
, . (
).
$ ls
$ echo chap_{one,two,three}.txt
chap_one.txt chap_two.txt chap_three.txt

echo , .
chap_, ,
.txt, . ,
.
. ,
, ,
. ,
, .
. main.c,
f1.c, f2.c tmp.c, /usr/local/src/C:
$ cp /usr/local/src/C/{main,f1,f2,tmp}.c .

:
$ ls -F
file1 file2 file3
$ mkdir vrs{A,B,C,D,E}
$ ls -F
file1 file2 file3 vrsA/

vrsB/

vrsC/

vrsD/

vrsE/

F ls (/) (*)
.

( ,
):
$ rmdir vrs*
$ mkdir vrs[A-E]
$ ls -F
file1 file2 file3

vrs[A-E]/

355

. - , ,
vrs[AE], bash mkdir,
.
[ ] . 160.


4
.
.
(~) , . bash ,
(/) ,
, . ( ),
HOME. ,
letter
Max :
$ echo $HOME
/home/max
$ echo ~
/home/max
$ echo ~/letter
/home/max/letter
$ cp ~/letter .

, , .
, :
$ echo ~zach
/home/zach
$ echo ~root
/root
$ echo ~xx
~xx

(.
. 302). , ~+ PWD ( ),
~ OLDPWD ( ,
).



($), . , (. -

356 8. Bourne Again Shell


. 459), (.
. 457). ,
(. . 306),
, (. , . 311).
bash man- info- .
, (
). ,
.


,
.
tcsh . 386.
bash :
$(())
$(()) . [$()]
. $(())
.
, C;
C (. .10.8).
bash .
(. . 311)
, , , .
($) .
read (
read
: . 466) age,
,
:
$ cat age_check
#!/bin/bash
echo -n "How old are you? "
read age
echo "Wow, in $((60-age)) years, you'll be 60!"
$ ./age_check
How old are you? 55
? 55
Wow, in 5 years, you'll be 60! , 5 60!

, bash . ,
(*) :
$ echo There are $((60*60*24*365)) seconds in a non-leap year.
There are 31536000 seconds in a non-leap year. 31536000

357

, letter.txt, wc, cut,


(. .358).
wc (word count ) (. V),
l, , ( )
1 4, ( ). cut (. V) c14 .
$ wc -l letter.txt
351 letter.txt
$ wc -l letter.txt | cut -c1-4
351

;
:
$ echo $(( $(wc -l letter.txt | cut -c1-4)/66 + 1))
6

wc cut . $( ).
66,
. ,
.

($)
$(( )) ,
, :

$ x=23 y=37
$ echo $((2*$x + 3*$y))
157
$ echo $((2*x + 3*y))

cat
wc, wc
, .
wc :
$ wc -l < letter.txt
351


:
$ numpages=$(( $(wc -l < letter.txt)/66 + 1))

let ( tcsh )
, $(( ). :

let

$ let "numpages=$(wc -l < letter.txt)/66 + 1"

358 8. Bourne Again Shell


( , ,
, )
let.
let. 0,
let 1; 0. let
:
$ let a=5+3 b=7+2
$ echo $a $b
8 9

let $(( )) ,
($) . ,
,
.


. bash

:
$()

bash , , , tcsh:
''


.
pwd
. , , echo,
.
$ echo $(pwd)
/home/max

pwd
where, :
$ cat where
where=$(pwd)
echo "You are using the $where directory."
$ ./where
You are using the /home/zach directory. /home/zach

, . pwd
:
$ cat where2
echo "You are using the $(pwd) directory."
$ ./where2
You are using the /home/zach directory.

359

find
README , .
find
ls.
$ ls -l $(find . -name README -print)

'':
$ ls -l 'find . -name README -print'


, ,
.
. ,
README, ./README:
$ ls -l $(find . -name README -size +$(echo $(cat ./README | wc -c)c ) -print )

set x (.
. 428), , bash .
README , ls l.
, ,

lnks
. 425,
echo

e
. 422 : ( ) .474.

$(( $(
$(( . ,
. , $(

)
, (.
() . 298), $(
( .


, . IFS (. IFS:
( ) . 316) , bash
. IFS , bash
(-TAB-NEWLINE). IFS
, bash .


(.
.157), ,

. noglob (. .8.13), ,
*, ?, [ ]. bash -

360 8. Bourne Again Shell


- , ,
.
,
( nullglob .8.13). TCShell
.
echo tmp* (tmp1, tmp2 tmp3) echo.
echo , .
rm tmp*,
, tmp*.
echo, .
$ ls
tmp1 tmp2 tmp3
$ echo tmp*
tmp1 tmp2 tmp3
$ rm tmp*
$ echo tmp*
tmp*

TC Shell
:
tcsh $ echo tmp*
echo: No match

dotglob (. .8.13), , , , (/),


. nocaseglob (. .8.13)
.
,
.
. echo
$max, ,
. , sonar. echo, ,
. , , tmp*.

$ echo tmp* $max


tmp1 tmp2 tmp3 sonar
$ echo "tmp* $max"
tmp* sonar
$ echo 'tmp* $max'
tmp* $max

, .
, ,
, (*).

361

,
letter. var letter*, ,
( ). letter* ,
. set (
grep) , var letter*.

$ ls letter*
letter1 letter2 letter3
$ var=letter*
$ set | grep var
var='letter*'
$ echo '$var'
$var
$ echo "$var"
letter*
$ echo $var
letter1 letter2 letter3

echo . $var
,
$var echo, .

var,
, . , .
,
, (
echo
) .


Bourne Again Shell
. , <(),
(FIFO
, ). .
,
. , >() ,
.
sort (. sort:
. 74, V) m
(merge , ,
) .
, ,
, , .
$ sort -m -f <(grep "[^A-Z]..$" memo1 | sort) <(grep ".*aba.*" memo2 |sort)

362 8. Bourne Again Shell

, . ,
.
, . , ,
.
, ,
.
1. , .
2. , , ./.
3. bash tcsh, .
, PATH.
1 2 , , . 3 , ,
.

, . fg ,
, . bg, , ( ,
CONTROL+Z).
jobs.

. ; unset. ,
, export (bash) setenv
(tcsh) .
. ,
. ( ), .


(PID), Linux-.
() ,
.
() . (&),
,
.
, ,
, . ,

363

.
,
.
,
CShell,
, ,
.
, .

Bourne Again Shell


, Bourne Again Shell
(vim emacs). , vim,
. .
emacs , .

,
. .
Bourne Again TC ,
.

, ,
,
. . .
.
,
, .
, , .

.
bash .
bash
set shopt.

Bourne Again Shell


.
(, , ). .8.6.
. , .
,

364 8. Bourne Again Shell


. , () .

1. :
$ whereis date
date: /bin/date
$ echo $PATH
.:/usr/local/bin:/usr/bin:/bin
$ cat > date
echo "This is my own version of date."
$ ./date
Fri May 22 11:45:49 PDT 2009

2.
, ?
, ?
3. PATH?
1) PATH,
:

/usr/local/bin

/usr/bin

/bin

/usr/kerberos/bin

bin .

.

2) doit /usr/bin
~/bin, ? ( ,
.)
3) PATH ,
, ?
4) /usr/games
PATH?
4. , :
$ person=zach

, :
1) echo $person
2) echo '$person'
3) echo "$person"
5. journal-file,
.
.
$ cat journal
# journal:
# $HOME/journal-file

365
file=$HOME/journal-file
date >> $file
echo -n " : "
read name
echo "$name" >> $file
echo >> $file
cat >> $file
echo "----------------------------------------------------" >> $file
echo >> $file

1) , ?
2)
read, cat?
6. , /home/zach/grants/biblios /home/zach/biblios.
Zach
. , .
1)
$ pwd
/home/zach/grants
$ CDPATH=$(pwd)
$ cd
$ cd biblios

2)
$ pwd
/home/zach/grants
$ CDPATH=$(pwd)
$ cd $HOME/biblios

7. PID- ,
.
8. :
$ sleep 30 | cat /etc/inittab

- sleep? cat
? ,
?


1. , , .
2. , .
3. :
$ cat quote_demo
twoliner=" 1.
2."
echo "$twoliner"
echo $twoliner

1) echo ? .
2) IFS, echo
, .

366 8. Bourne Again Shell


4. , :
$ [0] ls xxx
ls: xxx: No such file or directory
$ [1]

5. dirname , , , :
$ dirname a/b/c/d
a/b/c

dirname ( /),
dirname .:
$ dirname simple
.

dirname bash. , ,
/, .
6. basename,
, bash. ,
a/b/c/d, basename d :
$ basename a/b/c/d
d

7. Linux basename .
basename , basename :
$ basename src/shellfiles/prog.bash .bash
prog
$ basename src/shellfiles/prog.bash .c
prog.bash

, 14.

9. TC Shell
TC Shell (tcsh) , Bourne Again Shell
: Linux. TC Shell ,
.
, , . ,
.
8 10,
bash, tcsh.
tcsh, bash ,
bash. , tcsh,
www.tcsh.org.
TC Shell C Shell (csh),
Berkeley UNIX. T TC
Shell TENEX TOPS-20,
. tcsh
, csh, , .
csh, tcsh
.
tcsh
. bash, tcsh
C Shell.
tcsh bash,
. ,
tcsh :
set =
bash , tcsh . , tcsh , , ,
, (
). ( , ), tcsh, bash, .
, tcsh ,
(>), $,
.
tcsh $, bash.

368 9. TC Shell

TCSH
UNIX C TC Shell,
tcsh .
, TC Shell ,
bash. ,
bash. Bourne Again Shell dash (.
dash . 283), bash, Linux
, .


TC Shell , tcsh, ,
Bourne Again Shell , bash.

,
, .
bash tcsh . , ,
. #!
. 294.
(#)
(!),
TC
Shell

tcsh. , tcsh
sh dash bash.

ECHO:

, tcsh echo
, n,
\c. echo, bash,
n (. read: . 466).


TC

Shell
,
(#), (
tcsh), tcsh sh
dash bash.
tcsh, tcsh bash.
set

(.
set
: .461)
bash tcsh -. (
. 390) , :
tcsh $ cat user_in
echo -n " : "
set input_line = "$<"
echo $input_line
tcsh $ user_in
:
,
tcsh
,
tcsh

, tcsh :
tcsh $ tcsh user_in
:

TC Shell 369

TC Shell
tcsh tcsh.
, ps. , : tcsh,
bash, sh ( bash) - . finger,
, ,
; /etc/passwd. ( Mac OS X
Open Directory.)
tcsh, chsh
(change shell ):
chsh

bash $ chsh
Changing shell for sam.
sam
Password:
New shell [/bin/bash]: /bin/tcsh
Shell changed.

bash $


, . , ,
/etc/passwd.
tcsh .
:
ignoreeof , ,
( ), , . tcsh
CONTROL+D ,
. ,
, . ignoreeof
(. ignoreeof . 396)
, CONTROL+D (
Bourne Again Shell).
ignoreeof , CONTROL+D . ignoreeof ,
. tcsh exit.
logout ,
.


TC

Shell
. ,
, tcsh ,
. , ,
. bash
. 284.

370 9. TC Shell
,
/etc/csh.cshrc /etc/csh.login . , root,
tcsh,
. ,
, , ..
/etc/csh.cshrc
/etc/csh.
login

~/.tcshrc , , ~/.cshrc (~/ ; . 4, ~ ()


.104). ,
.
tcsh .
.tcshrc ,
(. . 374) path
:
.tcshrc
.cshrc

tcsh $ cat ~/.tcshrc


set noclobber
set dunique
set ignoreeof
set history=256
set path = (~/bin $path /usr/games)
alias h history
alias ll ls -l

, , ~/.history. histfile, tcsh .history ,


.
.history

, ,
~/.login. ,
, .
( )
setenv (. . 384). .login

.
.login

tcsh $ cat ~/.login


setenv history 200
setenv mail /var/spool/mail/$user
if ( -z $DISPLAY ) then
setenv TERM vt100
else
setenv TERM xterm
endif
stty erase '^h' kill '^u' -lcase tab3
date '+Login on %A %B %d at %I:%M %p'

.login TERM ( if , -

, Bourne Again Shell TC Shell 371

, , , ,
TERM). stty
(. V) date (. V)
.
TC Shell
/etc/csh.logout ~/.logout.
.logout date .
sleep , echo , .
, .
/etc/csh.logout
.logout

tcsh $ cat ~/.logout


date '+Logout on %A %B %d at %I:%M %p'
sleep 5

, Bourne Again Shell


TC Shell
, , bash tcsh,
C Shell:
( ; .
() . 371).
(. . 372).
(. . 374).
(. . 376).
(. . 376).
(. . 377).
(. . 377).
, bash.
bash tcsh.

()
Bourne Again Shell
. 351. man-, tcsh,
expansion, bash,
substitution
.
TC
Shell

, :
1) ;
2) ;
3) ;
4) ;
5) ;
6) .

372 9. TC Shell

TC Shell
. tcsh (.

prompt
. 392). , .

history
bash, tcsh history ,
. ,
.
history, .
, 10
history, 23 tcsh, .
.
32 $ history 10
23 23:59
24 23:59
25 23:59
26 0:00
27 0:00
28 0:00
29 0:00
30 0:00
31 0:00
32 0:00

set prompt = "! $ "


ls -l
cat temp
rm temp
vim memo
lpr memo
vim memo
lpr memo
rm memo
history


. ,
!! tcsh, , bash, . !328 328; !?txt?
, txt.
(!)
. 329. .9.1 , tcsh
bash.
9.1.

. , a u l
.

, Bourne Again Shell TC Shell 373


tcsh $ echo $VERSION
VERSION: Undefined variable.

tcsh $ echo !!:1:al
echo $version
tcsh 6.14.00 (Astron) 2005-03-25 (i486-intel-linux) options wide,nls,dl,

,
,
(. .381).

, tcsh, , bash. ,
, , bash
HISTSIZE HISTFILESIZE, tcsh
history savehist (.9.2).
9.2.

history

100

histfile

~/.history

history

savehist

tcsh
~/.history. .
savehist , .history (
, history). savehist , tcsh . history savehist (
setenv, set). history
, ,
savehist , (. .9.2).
history ,
. , .
500 , ~/.tcshrc:
history
savehist

tcsh $ set history = 500

tcsh
200 :
tcsh $ set savehist = 200

:
tcsh $ set history=500 savehist=200

374 9. TC Shell
savehist
;
200 .
, savehist
~/.tcshrc.
histlit (history literal ), history ,
, , , .
( 32):
histlit

tcsh $ cat /etc/csh.cshrc

tcsh $ cp !!:1 ~
cp /etc/csh.cshrc ~
tcsh $ set histlit
tcsh $ history

31 9:35
cat /etc/csh.cshrc
32 9:35
cp !!:1 ~
33 9:35
set histlit
34 9:35
history
tcsh $ unset histlit
tcsh $ history

31 9:35
cat /etc/csh.cshrc
32 9:35
cp /etc/csh.cshrc ~
33 9:35
set histlit
34 9:35
history
35 9:35
unset histlit
36 9:36
history

bash

tcsh
- . !250
w
,
bash
250,
w
. tcsh ,
250w. : bash
250
,
w
tcsh
250w. ( 250 , tcsh .)
w 250, w,
:
!{250}w

alias unalias, tcsh, , bash (. . 341).


alias :
alias

, Bourne Again Shell TC Shell 375

ls:
tcsh $ alias ls "ls -lF"

bash tcsh :
$ alias nam "echo Hello, \!^ is my name"
$ nam Sam
Hello, Sam is my name

\!* :
$ alias sortprint "sort \!* | lpr"

:
$ alias n2 "echo \!:2"


, ,
tcsh . ,
tcsh , ,
.9.3. .
, alias.
alias,
.
9.3.

beepcmd

cwdcmd

periodic

, tperiod .
tperiod , 0, periodic

precmd

shell

,
, #! ( #!
. 294)



, ,
. ,
8. ,
:

376 9. TC Shell
21 $
22 $
test
23 $
24 $
temp

alias last echo \!:$


last this is just a test
alias fn2 echo \!:2:t
fn2 /home/sam/test /home/zach/temp /home/barbara/new

21 last, , .
23 fn2, ,
.


bash (.
. 299) tcsh .
,
. %
, , .
,
, .
bash PID

- , tcsh , . , . 299, tcsh


:
tcsh $ find . -print | sort | lpr & grep -l zach /tmp/* > zachfiles &
[1] 18839 18840 18841
[2] 18876


TC Shell *, ? [ ] ,
bash (. . 157).
* , ? , [ ] ,
, .
TC Shell ,
(~), ,
bash (. . 379). ~
, .
bash ~+ ~ tcsh .
tcsh , (.
, . 353).
, , ,
. tcsh csh (globbing),
. tcsh
, ,

377

( ). noglob , ,
, .


, tcsh, bash (. 8, ).
dirs , pushd popd
.


tcsh $(), , .
''.
bash tcsh . . 358.


tcsh bash , (>)
, 2> tcsh
. tcsh
(>&). bash,
. , , bash
. 290, x y, :
tcsh
cat:
tcsh
This
tcsh
tcsh
cat:
This

$ cat
x: No
$ cat
is y.
$ cat
$ cat
x: No
is y.

x
such file or directory
y
x y >& hold
hold
such file or directory


y.

y.

y cat
. x cat
.
bash tcsh .
. cat xy
(
, . ( )
.298). > -

378 9. TC Shell
outfile. ,
, , ,
errfile. , errfile , .
tcsh
tcsh
This
tcsh
cat:

$ (cat x y > outfile) >& errfile


$ cat outfile
is y.
y.
$ cat errfile
x: No such file or directory

,

, . , - ,
find (.

V
) , .
, biblio. findout.
find ,
, findout ,
, .
tcsh $ find / -name "*biblio*" -print >& findout &


,
, findout .
, ,
, ,
tail (.V) f. tail
:
tcsh $ tail -f findout

tail,
( CONTROL+C).


,
.


TC Shell
, . tcsh
.

379


TC Shell ,
.
, .
, .
, ,
, TAB; tcsh , ,
.
cat trig1A TAB; ,
trig1A:
tcsh $ cat trig1ATABcat trig1A.302488

, tcsh
.
,
, , , , :
tcsh $ ls h*
help.hist help.trig01 help.txt
tcsh $ cat hTABcat help. ()

TAB.
CONTROL+D, tcsh :
tcsh $ cat help.CONTROL+D
help.hist
help.trig01 help.txt
tcsh $ cat help.

tcsh , ( TAB) .


TC Shell (~), , TAB
:
tcsh $ cd ~zaTABcd ~zach/
tcsh $ pwd
/home/zach

(/), tcsh , . .


, , ,

380 9. TC Shell
. ,
path. ,
tcsh, .
tcsh $ upTAB ()CONTROL+D
up2date
updatedb
uptime
up2date-config
update-mime-database
up2date-nox
updmap
tcsh $ uptTABuptime
9:59am up 31 days, 15:11, 7 users, load average: 0.03, 0.02, 0.00

, , autolist,
, ,
TAB. CONTROL+D .
tcsh $ set autolist
tcsh $ upTAB ()
up2date
updatedb
uptime
up2date-config
update-mime-database
up2date-nox
updmap
tcsh $ uptTABuptime
10:01am up 31 days, 15:14, 7 users, load average: 0.20, 0.06, 0.02

autolist ambiguous,
TAB ,
.
TAB ,
, TAB
:
tcsh $ set autolist=ambiguous
tcsh $ echo $hTAB ()
histfile history home
tcsh $ echo $hiTABecho $histTAB
histfile history
tcsh $ echo $histoTABecho $history
1000

,
, : , ,
. ;
$,
. . which :
up , .
which updates (
), which :
tcsh $ ls up*
updates
tcsh $ which updatedb ups uptime
/usr/bin/updatedb
/usr/local/bin/ups
/usr/bin/uptime
tcsh $ which upTABwhich updates
updates: Command not found.

381


tcsh ,
bash.
emacs ( ), vi ( m ).
vi(m) bindkey v, emacs bindkey e.
,
( ) ,
.
bindkey
,
:
Bindkey

tcsh $ bindkey
Standard key bindings

"^@"
-> set-mark-command
"^A"
-> beginning-of-line
"^B"
-> backward-char
"^C"
-> tty-sigintr
"^D"
-> delete-char-or-list-or-eof

Multi-character bindings

"^[[A"
-> up-history
"^[[B"
-> down-history
"^[[C"
-> forward-char
"^[[D"
-> backward-char

Arrow key bindings


-
down
-> down-history
up
-> up-history
left
-> backward-char
right
-> forward-char
home
-> beginning-of-line
end
-> end-of-line

^ CONTROL (^B = CONTROL+B). ^[


META ALT; META ALT
.
META
ALT, ESCAPE,
. ^[[F META+[
ALT+[, F ESCAPE [F.
down-up-left-right --, home-end HOME END . META 7, :
. 231.
, OS X,
META ALT. ,
Macintosh, OPTION.

382 9. TC Shell
, bindkey
emacs. vi(m)
(bindkey v) bindkey .
, bindkey less.


tcsh , (
emacs). :
.


tcsh
, .
: spell-line spell-word:
$ bindkey | grep spell
"^[$"
-> spell-line
"^[S"
-> spell-word
"^[s"
-> spell-word

, bindkey, , spell-line
META+$ (ALT+$ ESCAPE $), spell-word META+S META+s (ALT+s
ESCAPE s ALT+S ESCAPE S). ,
, META+s. META+$ spellline, :
tcsh $ ls
bigfile.gz
tcsh $ gunzippMETA+s gunzip bigfele.gzMETA+sgunzip bigfile.gz
tcsh $ gunzip bigfele.gzMETA+$gunzip bigfile.gz
tcsh $ ecno $usfrMETA+$echo $user


correct , tcsh
. correct, tcsh
:
tcsh $ unset correct
tcsh $ ls morning
morning
tcsh $ ecno $usfr morbing
usfr: Undefined variable.

, , (.
() . 371).
- , .

383

correct cmd ,
all , , complete
:
tcsh $ set correct = cmd
tcsh $ ecno $usfr morbing
CORRECT>echo $usfr morbing (y|n|e|a)? y
usfr: Undefined variable.

tcsh $ set correct = all
tcsh $ echo $usfr morbing
CORRECT>echo $user morning (y|n|e|a)? y
zach morning

correct cmd, tcsh


ecno echo. correct all, tcsh , . ,
.
TC Shell , y,
, n, , e, , a, . , , prompt3.
correct
ls, tcsh
. , , ls,
. , :
tcsh $ set correct=cmd
tcsh $ lx -l ()
CORRECT>lex -l (y|n|e|a)? e
tcsh $ lx -l

correct complete, tcsh , (. . 379). correct complete up.


Ambiguous command,
, .
. TAB, , up,
t . ,
uptime:
tcsh $ set correct = complete
tcsh $ up
Ambiguous command
tcsh $ uptuptime
4:45pm up 5 days, 9:54, 5 users,


load average: 1.62, 0.83, 0.33

384 9. TC Shell

tcsh ,
. tcsh , . @
.
, , tcsh .
tcsh .


tcsh ,
20, , .
, .


,
set, @ setenv. set setenv . @
. set @
. setenv
( ).
setenv
export Bourne Again Shell.

. 454.
, tcsh ,
($).

, . , ,
.


TC Shell ,
Bourne Again Shell.
: tcsh , set ( setenv) ()
.
tcsh $ set name = fred
tcsh $ echo $name

385
fred
tcsh $ set
argv
()
cwd
/home/zach
home
/home/zach
name
fred
path
(/usr/local/bin /bin /usr/bin /usr/X11R6/bin)
prompt $
shell
/bin/tcsh
status 0
term
vt100
user
zach

name
fred. bash tcsh ,
. name.
set ,
( ).
set ,
.
unset:
tcsh $ set name
tcsh $ echo $name
tcsh $ unset name
tcsh $ echo $name
name: Undefined variable.

setenv , . tcsh , echo ,


, exit
. , ,
setenv set:
setenv

tcsh $ setenv SRCDIR /usr/local/src


tcsh $ tcsh
tcsh $ echo $SRCDIR
/usr/local/src
tcsh $ exit

setenv , ( ), ,
. ,
, .
set, setenv ,

.
unset, unsetenv,
.

386 9. TC Shell


, (1, 2, 3 . .). tcsh ,
( 1).
,
, .
, :
8 $ set colors = (red green blue orange yellow)
9 $ echo $colors
red green blue orange yellow
10 $ echo $colors[3]
blue
11 $ echo $colors[2-4]
green blue orange
12 $ set shapes = ('' '' '' '' '')
13 $ echo $shapes
14 $ set shapes[4] = square
15 $ echo $shapes[4]
square

8 colors .
,
( 12).

(9). , ,
( 10, 14 15). ,
, (11).
. 389.


@ ( . 373, tcsh
). @
, , set
. @ , . set, @
.
, @
, C.
@ ( @ ):
@ _

_ ,
. C: =, +=, =,*=, /=

387

%=. ( . . 12.4.)
, C (.
). , . ,
: <, >, & |, .

$
bash, tcsh , (
, ), ($).
tcsh $ @ $answer = 5 + 5
:
answer: Undefined variable.
( )
, answer ,
@: Variable name must begin with a letter.
( )

tcsh $ @ answer = 5 + 5
10 answer.

, bash (. 10, ). , , , . 9.8.


:
1. 0.
2. .
3. , != ==, .
4. , &, |, <, >, ( ).
, @:
216
217
0
218
219
7
220
221
0
222
223
12
224
225
13

$ @ count = 0
$ echo $count
$ @ count = ( 10 + 4 ) / 2
$ echo $count
$ @ result = ( $count < 5 )
$ echo $result
$ @ count += 5
$ echo $count
$ @ count++
$ echo $count

388 9. TC Shell
216 count 0.
218 , .
220 @ , , result.
false (= 0), count 5.
222 :
tcsh $ @ count = $count + 5

224 count
.



(++) (),
, ,
:
tcsh $ @ count = 0
tcsh $ @ count++
tcsh $ echo $count
1
tcsh $ @ next = $count++
@: Badly formed number.

C bash, tcsh

.


@
, set,
. set
, ,
.

. ,
.
:
@ _[]

. 1.
, ,
. , , ,
.
, set, tcsh ,
: @: Subscript out of range.
226 $ set ages = (0 0 0 0 0)

389
227 $ @ ages[2] = 15
228 $ @ ages[3] = ($ages[2] + 4)
229 $ echo $ages[3]
19
230 $ echo $ages
0 15 19 0 0
231 $ set index = 3
232 $ echo $ages[$index]
19
233 $ echo $ages[6]
ages: Subscript out of range.

.
226 ,
0.

227 228 , 229 .

230 ,

232 , 233 .


, bash, tcsh
:
$ set bb=abc
$ echo $bbdef
bbdef: Undefined variable.
$ echo ${bb}def
abcdef


_:
$#_

, _, , :
$?_

1, _
, 0 :
tcsh
tcsh
5
tcsh
1
tcsh
tcsh
0

$ set days = (mon tues wed thurs fri)


$ echo $#days
$ echo $?days
$ unset days
$ echo $?days

390 9. TC Shell


tcsh
, set.
input_line:
echo -n " : "
set input_line = "$<"

$< .
$< , input_line .


TC

Shell
,
. (,
PID- ). :
, , , .
:
~/.login ~/.tcshrc (. . 369).

,
( ) , . tcsh,
, ; argv[1]
. $argv[n] $n.
argv[*] ; $*.
$0 (.
. 459). Bourne Again Shell
argv, . argv .
argv

$#argv
$#

argv. .
. 389.

autolist

(.
. 379).

tcsh
. , tcsh .
60,
, .
autologout

cd , bash
CDPATH (. CDPATH:
cd . 317). cdpath
cdpath

391

, (.
path), , , ~/.login, ,
:
set cdpath = (/home/zach /home/zach/letters)

cd
. , cd
, cdpath.
cmd
, all
complete
. ,
(. . 382).
correct


. (. . 130), tcsh cwd .
cwd

pushd, popd dirs .


. 302.
dirstack

fignore

, tcsh
.

gid

(group ID).

,
(.
. 373). ~/.history.
histfile

history

. . . 372.

. cd
(~) (. . 365).

home
HOME

, , . TC Shell 10,
mail ,
.
mail

owd

()
. ~ -

bash.
, tcsh (. PATH: ,
. 312). , path PATH

392 9. TC Shell
.
path :
tcsh $ set path = ( /usr/bin /bin /usr/local/bin /usr/bin/X11 ~/bin . )

, PS1, bash (. PS1:


() . 314). , > , root-,
#. . .tcshrc, prompt, :
prompt

set prompt = '! $ '

.9.4 ,
prompt .
9.4. prompt

%/

cwd ( )

%~

, %/,

%! %h !

%d

%D

%m

%M

%n

%t

%p

%W

%y

%Y

%#

(#), root

-; (>)

%?

, tcsh .
%R?. TC Shell ,
%R ,
foreach foreach (. foreach . 403), while while (. while . 404).
, , , tcsh
prompt2

393

, ,
:
% echo "Please enter the three values
Unmatched ".

, ,
(), .
tcsh .
(?).
% echo ", , \
? ."
, ,
> .

,
.
CORRECT>% R (y|n|e|a)? , R
.
prompt3

,
.
~/.history.
, (. history . 373).
savehis

shell

. TC Shell
. 1.
shlvl

, .
$?, bash (. $?:
. 458).
status

tcsh

tcsh.

: time ,
time.
, , .
;
, , , time
. 0 . ,
, .9.5.
time

394 9. TC Shell
9.5. , time

%U

, ,
( )

%S

, , ( )

%E

( ),

%P

,
, (%U+%S)/%E

%W

%X

, ,

%D

, , ,

%K

, (
%X+%D),

%M

, ,

%F

(
, )

%I

%O

time
"%Uu %Ss %E %P%

%X+%Dk %I+%Oio %Fpf+%Ww"

:
tcsh $ time
0.200u 0.340s 17:32:33.27 0.0%

0+0k 0+0io 1165pf+0w

. ,
, , . , time,

.
tperiod

, periodic (. . 375).

user

version

tcsh, .

watch

,
-

395

. any
, (any any) , (zach ttyS1
any console $user any) zach
ttyS1 , ,
(,
).
10, , , . watch (1 any console), .
.
,
log. , ,
watch, who.
who

, , watch (. 9.6).

9.6. , who

%n

%a

%l

%M

( , ),

$m

who , watch- : "%n has %a %l from %m",


:
sam has logged on tty2 from local
$

bash, PID- ;
$$.

,
,
. ,
. , .
~/.tcshrc,
.

(. . 382).
autocorrect

396 9. TC Shell
pushd
,
. dunique , , .
echo
.
echo tcsh x
set.
filec
(.
. 379) tcsh csh ( csh
tcsh).
histlit
,
, (.
histlit . 374).
ignoreeof
CONTROL+D
, . exit
logout.
listjobs
.
listlinks
lsF ,
, @.
loginsh
, .
nobeep
, .
noclobber

(.9.7). noclobber, ,
, (, >! >>!).
noclobber: . 148.
dunique

9.7. noclobber

noclobber

noclobber

x >


x .
,


x .
,

x >>


x .
,
. ,


x .
,
.
,
,

397


.
*, ?, ~ [ ] .
noglob

, .
. nonomatch,
tcsh No match ( ) .
nonomatch

tcsh
cat:
tcsh
tcsh
cat:

$ cat questions?
No match

$ set nonomatch
$ cat questions?
questions?: No such file or directory

, tcsh
.
tcsh
(. . 376).
notify

pushdtohome

pushd
(
pushd ).

pushdsilent

pushd popd .

rmstar

rm *.


(. . 372).
tcsh v set.
verbose

visiblebell


TC

Shell

, Bourne Again Shell. ,


. , .
. 417.

if
if
if ( )_
if
.
ifthen (. ).

398 9. TC Shell
tcsh $ cat if_1
#!/bin/tcsh
# , if.
#
if ( $#argv == 0 ) echo "if_1: ."

if_1 . ,
, true
, if .
, ,
if_1, ,
. :
n _

n , .9.8.
(true),
1, (false), 0.
, tcsh 0.
,
( ):
tcsh $ cat if_2
#!/bin/tcsh
if -f $1 echo "$1 ."
9.8. n
n

, (group ID)

(sticky bit)

(FIFO)

( )

( , )

399
n

, ,
, $path

- , .
, ox _ , .
o _ && x _.
. n_,
n , .9.9.
9.9. n
n

A:

M:

i- *

C:

i- ,

.
(, ),

i- . I-

:i-.

(user ID)

U:

(group ID),

G:

* ( , 1 1970 ).

,
, , . 0 () , (,

400 9. TC Shell
0), 1, 0,
.
F , ,
i- .
, filetest:
tcsh $ filetest -z if_1
0
tcsh $ filetest -F if_1
2051:12694
tcsh $ filetest -Z if_1
131

goto
goto :
goto

goto ,
: goto :
tcsh $ cat goto_1
#!/bin/tcsh
#
#
#
if ($#argv == 2) goto goodargs
echo " : $0 1 2"
exit 1
goodargs:

goto_1 (.
. 426), ,
.


onintr (
on

interrupt
) , . onintr :
onintr
, ,
:.
. , -

401


.
onintr . , ,
:
tcsh $ cat onintr_1
#!/bin/tcsh
# onintr
onintr close
while ( 1 )
echo " ."
sleep 2
end
close:
echo " ."

, onintr .
close:
rm -f /tmp/$$*

/tmp/$$*
/tmp, PID

- (. $$: PID- . 457).

ifthenelse
ifthenelse . , if,
, true.
.
if () then

endif

. true, . false,
, else.
if () then

else

endif

ifthenelif (. ifthenelif . 423). , , true, .


if () then

402 9. TC Shell
else if () then

else

endif

class 0, 1, 2 3,
.
, class , , ,
. ,
number.
tcsh $ cat if_else_1
#!/bin/tcsh
#
#
set class
set number = $argv[1]
#
if ($number < 0) then
@ class = 0
else if (0 <= $number && $number < 100) then
@ class = 1
else if (100 <= $number && $number < 200) then
@ class = 2
else
@ class = 3
endif
#
echo " $number ${class}."

if , number . , class 0
, endif.
, if , number 0 100. && true,
true.
number 0 100, class 1
, endif.
, number
100 200. , else
class 3. if
endif. class
({}).
;
.
, ,
- .

403

foreach
foreach bash for
in (. forin . 429). :
foreach _ (_)

end

. _
_. end, _ _
. ,
_.
tcsh foreach , , . , ,
, memo letter.
memo.1, dailymemo memories letter.1, dailyletter letterries.
: () . _ foreach
,
, . ,
, mv
. echo sed (), : ,
, ,
. . 358. sed (. 13, sed)
. $1 $2
$argv[1] $argv[2].
tcsh $ cat ren
#!/bin/tcsh
# : ren 1 2
#
1
#
2
if ($#argv != 2) goto usage
foreach i ( *$1* )
mv $i 'echo $i | sed -n s/$1/$2/p'
end
exit 0
usage:
echo " : ren 1 2"
exit 1

404 9. TC Shell

foreach
buffer:
tcsh $ cat foreach_1
#!/bin/tcsh
# 20 argv
#
set buffer = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
set count = 1
#
if ($#argv > 20) goto toomany
#
foreach argument ($argv[*])
set buffer[$count] = $argument
@ count++
end
# command
# , .
exec command $buffer[*]
#
toomany:
echo " ."
echo " : foreach_1 [ 20 ]"
exit 1

foreach_1 , command
, 20 . foreach_1
, ,
command 20. 20
1, .
foreach . foreach
argument.
buffer. buffer count. count, @
(@ count++). , command
exec ( bash tcsh; . exec:
. 469). command ,
, .

while
while :
while ()

end

, true. false,
.
tcsh $ cat while_1
#!/bin/tcsh
# while.

405
# 1 n;
# n .
#
set limit = $argv[1]
set index = 1
set sum = 0
#
while ($index <= $limit)
@ sum += $index
@ index++
end
#
echo " $sum"

n , n
. +=
sum + index sum.

break continue
foreach while break
continue. , , . break ,
end, .
continue end,
.

switch
switch bash case (. case . 439):
switch (_)
case :

breaksw
case :

breaksw

default:

breaksw
endsw

breaksw ,
endsw. breaksw , . ,
.10.2, ( | ).

406 9. TC Shell
tcsh $ cat switch_1
#!/bin/tcsh
# switch.
#
# yes no
# .
#
# argv[1]
if ($#argv != 1) then
echo " : $0 [yes|no]"
exit 1
else
# argv[1] , switch
switch ($argv[1])
# YES
case [yY][eE][sS]:
echo " yes."
breaksw
#
# NO
case [nN][oO]:
echo " no."
breaksw
#
#
default:
echo " yes no."
breaksw
endsw
endif


(builtins) ,
( ). , ,
. ,
;
.
, .
, ,
, ,
PATH. , .
,
(if, foreach, endsw ..), .9.10, . .9.10 tcsh,
.

407
9.10. tcsh

fg.
, (.
fg: . 300)

% &

bg.
, (.
bg: . 301)

set, (. . 386)

Alias

; bash ,
tcsh (. . 374)

Alloc

bg

(. bg: . 301)

bindkey


, tcs

bindkey

bindkey
(. . 381)

bindkey l

bindkey e

emacs
(. . 381)

bindkey v

vi(m)
(. . 381)

bindkey

bindkey b

, , Cx ( x ,
CONTROL),
meta- M-x (
, Linux, META
ALT; Mac OS X OPTION
[ , option
Terminal]) F-x

.
,

bindkey s

tcsh

bindkey c

builtins

cd chdir

(.
cd
: . 108)

dirs

(. dirs: . 302)

408 9. TC Shell
9.10 ()

echo

. echo
, , n (. . 390)
\c (. read:
. 466). echo echo (. V)

eval

. eval
, . ,
. - , ,
(.
eval . 346)

exec

, ,
.
exec:
.469, source

exit

TC Shell. exit
, tcsh

(. $?: . 458)

fg

(. fg:
. 300)

Filetest

, , , ,
(. .9.9).

Glob

, echo,

Hashstat

tcsh. , path. . rehash unhash


History

(.
history . 372)

Jobs

( , )

Kill

(.
kill
: . 475)

Limit

, , .
,
, , ..

Log

, tcsh
10 watch
(. watch . 394)

409

Login

Logout

, ( )

lsF

, ls F (. V), .
( lsF, .)

Nice

.
, ,
. root- nice
. nice V

Nohup


, . .
nohup V

Notify


(.
. 362)

onintr

,
(. . 400).
bash trap:
. 472

popd

,

(. . 302)

printenv

pushd

(. . 302)

rehash

, . tcsh
path
.
path, rehash,
. , tcsh (.
hashstat unhash )

repeat

(
)

sched

. ,
10 : tcsh $ sched 10:00 echo .. sched .
, tcsh

410 9. TC Shell
9.10 ()

set

,
(. )

setenv

,
(. )

shift

bash shift (. shift:


. 460).
shift argv.

,

source

,
: source
. bash
.() (. . () source

: . 287). source
TC

Shell

, #!. source ,
, set,
.
.tcshrc .login source , . source

stop

, .
stop

suspend


. ,
, .

time

, .
,
, time
(. time . 393). time
,

umask

, tcsh
(. V)

unalias

(. . 374)

unhash

(.
hashstat rehash )

unlimit

(. limit ),

unset

(. )

unsetenv

(.

411

wait

. wait , tcsh
, , .
,
, wait

where

, , ,
: ,
, path

which

where, , .
, Linux- which,

Bourne Again Shell, TC Shell


, . TC Shell,
C Shell, ,
, ,
.
tcsh,
C Shell.
tcsh, chsh. tcsh
, tcsh ;
- bash,
,
tcsh.
.
bash . , ,
. , tcsh . ,
set. @
.
tcsh export,
( ) setenv. setenv
. unset , , unsetenv .
setenv

tcsh alias alias, bash.

412 9. TC Shell
bash tcsh
.
tcsh, , , , bash.
tcsh bash, .
(globbing), Bourne Shell, , (* ?), . tcsh , ,
. bash,
.
tcsh ,
. ,
,
.

1. , :
37
38
39
40
41
42
43
44
45
46
47

mail zach
cd /home/sam/correspondence/business/cheese_co
less letter.0321
vim letter.0321
cp letter.0321 letter.0325
grep hansen letter.0325
vim letter.0325
lpr letter*
cd ../milk_co
pwd
vim wilson.0321 wilson.0329

, :
1) Zach;
2) vim wilson.0329;
3) wilson.0329 ;
4) wilson.0321 wilson.0329.
2. 1) ?
2) homedots,
, .
3. 1) , , ?
2) , , ,
?
4. ~/.tcshrc ?
?

413
5. , :
adams.ltr.03
adams.brief
adams.ltr.07
abelson.09
abelson.brief
anthony.073
anthony.brief
azevedo.99

, TAB ?
1) less adams.l
2) cat a
3) ls ant
4) file az
, CONTROL+D ?
1) ls ab
2) less a
6. backup,
, .bak.
7. qmake (
quiet
make


make
), make ,
make.log. qmake ,
make.
8. tcsh
?


1. Bourne Again Shell command_menu ( 10 . 10.2), TC Shell?
.
2. rm ( rm i) ,
. delete,
, , ~/.trash. undelete, ~/.trash
. ~/.logout ,
, , delete
:
/bin/rm -f $HOME/.trash/* >& /dev/null

, , ~/.logout :
rm $HOME/.trash/*

3. foreach_1 ( foreach . 403),


, exec.

414 9. TC Shell
4. while_1 ( while
. 404), .
time.
5. find myfind, findout, ,
, . myfind , find. ,
myfind , ?
6. , foreach_1 (
foreach . 403) 20 , , toomany:? (
exit?)

IV

10. Bourne Again Shell . .

416

11. Perl . . . . . . . . . . . . . . . . . . . . . . . . . . 504


12. AWK . . . . . . . . . . . . . . . . 552
13. sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
14. rsync . . . . . . . . . 602

10.
Bourne Again Shell
5 , 8
, Bourne Again Shell.

Bourne
Again

Shell

, , .

,
. ,
.
,
.
,
, ,
. Bourne

Again

Shell

, , C.
, , , , .
type, ,
read, , . , exec,
exec exec
.
trap, (, ,
CONTROL+C) .
kill, , getopts,
. , .10.6.
, . 10
.
.
, .
, ,
. , ,
.

417

TEST
test

, ,
Linux. PATH
, ,
.

, .

Linux, . ,
. , ,
.


, , , . TC Shell (.
. 397)
, Bourne Again Shell.
ifthen, forin, while, until
case. ,
break continue.

ifthen
ifthen
:
if -
then

fi
,
, ,
,
. , ,
, .
.10.1 , if , -, . if
fi (if ).

test

if
-

False

True

then

fi

. 10.1. -
if..then

418 10. Bourne Again Shell


, ,
, test ( tcsh
test) , if. (
test, test, V.)
test true, , false,
. $word1 $word2 test , ,
:
$ cat if1
echo -n " 1: "
read word1
echo -n " 2: "
read word2
if test "$word1" = "$word2"
then
echo ""
fi
echo " ."
$ ./if1
1: peach
2: peach

-
test"$word1"="$word2". test true, -

, .
true (= 0), ,
then fi. false (=0),
, fi, , then fi. if
, .
..
Bourne Again Shell test ,
. , /usr/bin/test. Bourne Again Shell.
bash , tcsh.
, ,
.
, .

(Builtins)

if

419

. test eq
; $# (. $#:
. 459) ,
. 1, :
$ cat chkargs
if test $# -eq 0
then
echo " ."
exit 1
fi
echo " ."
$ ./chkargs
.
$ ./chkargs abc
.

, chkargs,
, .
, ,
. (, chkargs).

.

.
, ( ) .
test f
($1):
$ cat is_ordfile
if test $# -eq 0
then
echo " ."
exit 1
fi
if test -f "$1"
then
echo "$1 "
else
echo "$1 "
fi
test, .10.1,

.
test
, . test V.

420 10. Bourne Again Shell


10.1. test

( )

( 0 )



, ,
. ,
, .
.

,
chkargs,
Linux . test ([ ]).
test . (
).

[ ]

test

$ cat chkargs2
if [ $# -eq 0 ]
then
echo " : chkargs2 " 1>&2
exit 1
fi
echo " ."
exit 0
$ ./chkargs2
: chkargs2
$ ./chkargs2 abc
.

, chkargs2,
1>&2 (. . 288).
chkargs2 1, . exit 0, , chkargs2 0 ,
. ,
Bourne Again Shell 0.

, . -

421

Linux, ,
chkargs2. , , ,
.
, cp, :
$ cp
cp: missing file operand
Try 'cp --help' for more information.



cp --help'

ifthenelse
else if ,
.10.2. ifthenelse (
tcsh, ) :
if -
then

else

fi

True

if
-

then

False

else

fi

. 10.2. - ifthenelse

(;) ,
, then if,
. ( if then , ;
[.

422 10. Bourne Again Shell


(;) () . 296.) ,
.
if -; then

else

fi

- true, if ,
then else, ,
fi. - false, if
, else.
out , ,
. v ( ), out less (.
less more: . 68)
. ,
, out ,
, v. true ( v), out
shift (. shift:
. 460) , v
, less.
false ( v),
cat:
$ cat out
if [ $# -eq 0 ]
then
echo " : out [-v] _ " 1>&2
exit 1
fi
if [ "$1" = "-v" ]
then
shift
less -- "$@"
else
cat -- "$@"
fi
out , cat less, , ()
.
, .
, . (, cat > _
.) Linux,
getopts (. getopts: . 476), more . ,
rm , (rm
_), .

423

True

if
-

then

True

False

elif
-

then

False

else

fi

. 10.3. - ifthenelif

ifthenelif
.10.3 ifthenelif ( tcsh ) :
if -
then

elif -
then

else

fi
elif else if
ifthenelse (. .10.3).

424 10. Bourne Again Shell


else elif ,
else fi,
elif fi.
ifthenelif . , . if test (a).
test true , true ( word1 word2, word2
word3). test true,
, then, ,
fi, :
$ cat if3
echo -n " 1: "
read word1
echo -n " 2: "
read word2
echo -n " 3: "
read word3
if [ "$word1" = "$word2" -a "$word2" = "$word3" ]
then
echo ": 1, 2 & 3"
elif [ "$word1" = "$word2" ]
then
echo ": 1 & 2"
elif [ "$word1" = "$word3" ]
then
echo ": 1 & 3"
elif [ "$word2" = "$word3" ]
then
echo ": 2 & 3"
else
echo " "
fi
$ ./if3
1: apple
2: orange
3: pear

$ ./if3
1: apple
2: orange
3: apple
: 1 & 3
$ ./if3
1: apple
2: apple
3: apple
: 1, 2 & 3

,
elif, , -

425

. , if ,
then, ,
fi. elif
elif. ,
echo, (&),
.
LNKS

lnks ifthen
ifthenelif. ,
. , lnks
, . ,
lnks .
.
$ cat lnks
#!/bin/bash
#
# : lnks []
if [ $# -eq 0 -o $# -gt 2 ]; then
echo " : lnks []" 1>&2
exit 1
fi
if [ -d "$1" ]; then
echo " ." 1>&2
echo " : lnks []" 1>&2
exit 1
else
file="$1"
fi
if [ $# -eq 1 ]; then
directory="."
elif [ -d "$2" ]; then
directory="$2"
else
echo " ." 1>&2
echo " : lnks []" 1>&2
exit 1
fi
#
if [ ! -f "$file" ]; then
echo "lnks: $file " 1>&2
exit 1
fi
#
set -- $(ls -l "$file")
linkcnt=$2
if [ "$linkcnt" -eq 1 ]; then
echo "lnks: $file " 1>&2
exit 0
fi

426 10. Bourne Again Shell

# i-
set $(ls -i "$file")
inode=$1
# i-
echo "lnks: find " 1>&2
find "$directory" -xdev -inum $inode -print

Max letter.

. Max lnks
. lnks, /home,
, . Mac OS X /home /Users. lnks , /home/max/letter /home/zach/draft
:
$ ./lnks letter /home
lnks: find
/home/max/letter
/home/zach/draft

ifthenelif, lnks , . lnks .


lnks , , # ! (.
#! . 294):

#!/bin/bash

#! . , ,
, .
lnks ,
,
. lnks , , :

#
# : lnks []

if lnks :

if [ $# -eq 0 -o $# -gt 2 ]; then


echo " : lnks []" 1>&2
exit 1
fi

true, lnks 1. ,
,
.
, .
if , ( $1)
( d test true,
):
if [ -d "$1" ]; then
echo " ." 1>&2

427

echo " : lnks []" 1>&2


exit 1
else
file="$1"
fi

, lnks . ,
lnks $1 file,
set . $1
set, .

lnks ifthenelif:

if [ $# -eq 1 ]; then
directory="."
elif [ -d "$2" ]; then
directory="$2"
else
echo " ." 1>&2
echo " : lnks []" 1>&2
exit 1
fi

-
. - 0 (true), directory
(.). - false, elif
. , directory
, , $2. $2 , lnks
1.
if lnks ,
$file. lnks
. test, :
!, f $file, true, , $file, :
[ ! -f "$file" ]

!, f test, ,
false, , $file,
.
, , $file, lnks set ls l:
#
set -- $(ls -l "$file")
linkcnt=$2
if [ "$linkcnt" -eq 1 ]; then
echo "lnks: $file " 1>&2
exit 0
fi

set (. . 358) ls l.
, linkcnt , $2. , set,
, ls l, (
, ).

428 10. Bourne Again Shell

if , $linkcnt ,
lnks .
, . lnks ,
. lnks

, .
, 1, lnks
i- , $file. , ls i- . 129, i-, ,
,
. (
HFS
+ , .) lnks set
ls i.
set i- ,
inode $1:
# i-
set $(ls -i "$file")
inode=$1

lnks ,
i
, $inode, find (. V):
# i-
echo "lnks: find " 1>&2
find "$directory" -xdev -inum $inode -print

find , ,
, , ,
($directory).
, i-, $inode, . i-
, find
, , ,
$directory. xdev ( ) ,
find .
. 126.
echo, lnks find ,
find, , find
. lnks exit, lnks , find.


, lnks, .
, x.
. .
lnks , ,
.
x (set x), , x
, .

429
$
+
+
+
+
+
+
+

bash -x lnks letter /home


'[' 2 -eq 0 -o 2 -gt 2 ']'
'[' -d letter ']'
file=letter
'[' 2 -eq 1 ']'
'[' -d /home ']'
directory=/home
'[' '!' -f letter ']'

, , ,
PS4. (+).
, .
PS4 , ,
. PS4 >>>>,
, :
PS4

$ export PS4='>>>> '

x , , - set:
set -x

, set x . :
set +x

set o xtrace set +o xtrace , set x


set +x .

forin
forin ( tcsh foreach) :
for _ in _
do

done
forin (.10.4) _

_ ,
do done. do done
.
done, _ _
.
do done , _.
_, ,
done.
forin apples fruit, fruit, apples.
fruit oranges .

430 10. Bourne Again Shell


,
, done, :


_
_

$ cat fruit
for fruit in apples oranges pears bananas
do
echo "$fruit"
done
echo " ."

do



_
?

$ ./fruit
apples
oranges
pears
bananas
.

done

. 10.4. -
forin

, ,
, , test , :
$ cat dirfiles
for i in *
do
if [ -d "$i" ]
then
echo "$i"
fi
done

* ( ) .
*
i.

for
for ( tcsh)
:
for _
do

done

for _
. for , forin (.

10.4), _.
for ,
.

431

for,
. , for arg, for
arg in "$@", "$@" "$1" "$2" "$3" ..
forin.
$ cat for_test
for arg
do
echo "$arg"
done
$ for_test candy gum chocolate
candy
gum
chocolate
WHOS

whos , if "$@". whos


, whos
. whos
, /etc/passwd. ,
.
whos , ,
, . whos
finger, whos . Mac OS X
passwd Open Directory.
$ cat whos
#!/bin/bash
if [ $# -eq 0 ]
then
echo " : whos " 1>&2
exit 1
fi
for id
do
gawk -F: '{print $1, $5}' /etc/passwd |
grep -i "$id"
done

whos ,
chas, , Marilou Smith:
$ ./whos chas "Marilou Smith"
chas Charles Casey
msmith Marilou Smith

whos for . for "$@" , for , .

$@

432 10. Bourne Again Shell

Marilou Smith ,
. for "$@" Marilou Smith,
.
whos
/etc/passwd. for gawk ( 12
awk mawk) ($1) ($5)
/etc/passwd. F: mawk
/etc/passwd (:),
. mawk $1 $5;
. , .
grep (. V). grep
$id (
). i grep .
grep , $id.

gawk

, mawk,
, bash (|):
( ) ,
( , ),
, . who | . ( tcsh) . sort ,
, who. ,
.

while
while ( tcsh) :
while -
do

done

- (.10.5) true, while


, do done.
-.
- false, ,
done.

number, . test
, number 10.
, test lt. ne (not equal ), eq (equal
), gt (greater than ), ge (greater than or equal to

test

433

), lt (less than ) le (less than or equal to


). test = () != ( ). test
0 (true) , number 10. test
true, , do done. V test,
test.

while
-

False

done

True

do

. 10.5. - while

$ cat count
#!/bin/bash
number=0
while [ "$number" -lt 10 ]
do
echo -n "$number"
((number +=1))
done
echo
$ ./count
0123456789
$
echo, do,
number. n echo
. ((()); .

. 480) number. done


while . echo count ,
(
9).

434 10. Bourne Again Shell

SPELL_CHECK

aspell .
list aspell ( Linux) :
, aspell
.
,
letter.txt:
$ aspell list < letter.txt
quikly
portible
frendly

spell_check while. ,
spell_check aspell .
: aspell.
, ,
. , spell_check
aspell ,
.
spell_check :
, , ,
. if
. if
. ( ; r test , .
, , .)
$ cat spell_check
#!/bin/bash
# aspell
if [ $# -ne 2 ]
then
echo " : spell_check 1 2" 1>&2
echo "1: " 1>&2
echo "2: " 1>&2
exit 1
fi
if [ ! -r "$1" ]
then
echo "spell_check: $1 " 1>&2
exit 1
fi
if [ ! -r "$2" ]
then
echo "spell_check: $2 " 1>&2
exit 1
fi
aspell list < "$2" |
while read line
do

435

if ! grep "^$line$" "$1" > /dev/null


then
echo $line
fi
done

spell_check aspell ( list


)
while, (
) . - ( read line)
true, .
while grep if1,
,
. grep ( $line)
, (
^ $). , grep
, $line
. ( grep
aspell , paul,
paulson.) $line
.
grep /dev/null (.
/dev/null: . 151), . if
grep ( , true false, ), 0 true ( false), . 0 false (true ),
, . echo
, , .
EOF (end of file ), read
false, while,
. spell_check
, , . , , Blinkenship and
Klimowski, Attorneys, Blinkenship Klimowski.
, spell_check
memo Blinkenship Klimowski :
$ aspell list < memo
Blinkenship
Klimowski
targat
hte
$ cat word_list
Blinkenship
Klimowski
$ ./spell_check word_list memo
targat
hte

aspell ( Linux) V.

if :

if ! grep -qw "$line" "$1"


q grep, . w
grep .

436 10. Bourne Again Shell

until
-

True

done

False

do

. 10.6. - until

until
until ( tcsh) while ( tcsh
)
. .10.6 , until
, -
true. while , - true . until
:
until -
do

done
until,
read. , -
, .
$ cat until1
secretname=zach
name=noname
echo " !"
echo
until [ "$name" = "$secretname" ]
do
echo -n " : "
read name
done
echo " ."
$ ./until1
!
:
:
:
:
.

helen
barbara
rachael
zach

437

locktty lock
Berkeley UNIX Lock Screen ( )
GNOME. ()
until . until
,
, ,
. locktty
.
, , -
.
$ cat locktty
#! /bin/bash
trap '' 1 2 3 18
stty -echo
echo -n ": "
read key_1
echo
echo -n " : "
read key_2
echo
key_3=
if [ "$key_1" = "$key_2" ]
then
tput clear
until [ "$key_3" = "$key_2" ]
do
read key_3
done
else
echo "locktty: " 1>&2
fi
stty echo

LOCKTTY?
(), ()
, locktty.

trap, tcsh (.
trap: . 472),
locktty,
(, ). 18 , CONTROL+Z ( , tty), .
.10.5. sttyecho (. V) , , , .
,
trap

438 10. Bourne Again Shell


key_1,
key_2. key_3= NULL. key_1 key_2 , locktty
( tput) until. key_3.
,
(key_2), until , .

break continue
for, while until break
continue. break ,
done, .
continue done,
.
.
forin 1 10. if
, index 3
($indexle3). if , index 8 ($indexge8). if
echo index. 3 if continue, continue,
echo $index if,
for. , 8, if
break break, .
$ cat brk
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index -le 3 ] ; then
echo "continue"
continue
fi
#
echo $index
#
if [ $index -ge 8 ] ; then
echo "break"
break
fi
done
$ ./brk
continue
continue
continue
4
5

439
6
7
8
break

case
case (. .10.7)
. , ,
- . case ( tcsh switch)
:
case - in
-1)
-1
;;
-2)
-2
;;
-3)
-3
;;

esac

case -. letter. -
A, , A.
case,
. - B C,
, . (*)
,
. -
(*), ,
esac, - case.
$ cat case1
echo -n " A, B C: "
read letter
case "$letter" in
A)
echo " A"
;;
B)
echo " B"
;;
C)
echo " C"
;;
*)

440 10. Bourne Again Shell


echo " A, B, C"
;;
esac
$ ./case1
A, B C: B
B
case

-
=
1
?

-
=
2
?

-
=
3
?

-1

-2

-3

esac

. 10.7. - case

case1 , b
. - b ,
B ( case), , ,
:
$ ./case1
A, B C: b
A, B, C

441

case .
, .10.2.
, :
$ cat case2
echo -n " A, B C: "
read letter
case "$letter" in
a|A)
echo " A"
;;
b|B)
echo " B"
;;
c|C)
echo " C"
;;
*)
echo " A, B, C "
;;
esac
$ ./case2
A, B C: b
B
10.2.

[]

. , .

,
case
, case . command_menu echo
. (
select.) case
.
$ cat command_menu
#!/bin/bash
#
echo
echo
echo
echo
echo

-e
"
"
"
-e

"\n
\n"
a. "
b. , "
c. "
" d. \n"

442 10. Bourne Again Shell

echo -n " a, b, c d: "


read answer
echo
#
case "$answer" in
a)
date
;;
b)
who
;;
c)
pwd
;;
d)
ls
;;
*)
echo " : $answer"
;;
esac
$ ./command_menu

a.
b. ,
c.
d.
a, b, c d: a
Wed Jan

3 12:31:12 PST 2009

e echo \n .
, echo
, () \n.
e echo ,
(.10.3). ,
echo, ( ). xpg_echo (. 8.13)
e.

echo e

10.3. echo ( e)

echo

\a

()

\b

BACKSPACE

\c

\f

(FORMFEED)

\n

(NEWLINE)

\r

(RETURN)

\t

443

echo

\v

\\

\nnn

ASCII nnn, nnn


, echo

case
, .
safedit case,
($#).
, vim.
$ cat safedit
#!/bin/bash
PATH=/bin:/usr/bin
script=$(basename $0)
case $# in
0)
vim
exit 0
;;
1)
if [ ! -f "$1" ]
then
vim "$1"
exit 0
fi
if [ ! -r "$1" -o ! -w
then
echo "$script:
exit 1
else
editfile=$1
fi
if [ ! -w "." ]
then
echo "$script:
"
exit 1
fi
;;

"$1" ]
$1" 1>&2

" \
" 1>&2

*)
echo " : $script [_]" 1>&2
exit 1
;;
esac
tempfile=/tmp/$$.$script
cp $editfile $tempfile
if vim $editfile
then
mv $tempfile bak.$(basename $editfile)

444 10. Bourne Again Shell

echo "$script: "


else
mv $tempfile editerr
echo "$script: -- " \
" editerr" 1>&2
fi

safedit , case
vim -. ,
safedit . ( vim,
, :w
. 182.)
safedit , case , $1,
. safedit ,
. safedit
, case 1.
case
, safedit
. PATH /bin /usr/bin.
PATH ,
, . , - ,
PATH .
,
.

PATH

script

, :

script=$(basename $0)

basename ,
, script . $0 , (. $0: . 459). ,
, basename safedit:
$ /home/max/bin/safedit memo
$ ./safedit memo
$ safedit memo

script
. ,
, , , ,
, .
safedit $$
. , esac,
tempfile . ,
/tmp. PID- .

445

PID- . safedit
,
safedit .
,
- /tmp. PID-
, $script ,
UNIX 14- . Linux
. PID- ,
. ( ,
$script, .) ,
if ,
bak., .
bak , ,
14, .bak , . basename
$editfile , bak.
safedit if -: vim $editfile.
- vim ,
$editfile. vim, . if , . , vim 0,
, then. vim
( , ,
vim kill [. V]), vim
, , else.

select
select ( tcsh) , Korn Shell. ,
.
select :
select _ [in . . . ]
do

done

select .
in , select
.
. , select,
select fruit in apple banana blueberry kiwi orange watermelon STOP

:
1) apple
2) banana

3) blueberry
4) kiwi

5) orange
6) watermelon

7) STOP

select LINES () COLUMNS (). ( LINES

446 10. Bourne Again Shell


24, COLUMNS 80.)
COLUMNS 20, :
1)
2)
3)
4)
5)
6)
7)

apple
banana
blueberry
kiwi
orange
watermelon
STOP

select PS3,
select. PS3 ?#, .
PS3 ( , ) select _ , .
_
. select REPLY,
, ,
do done. ,
PS3.
select PS3
, - break exit. break
, exit .
select :
PS3

$ cat fruit2
#!/bin/bash
PS3=" : "
select FRUIT in
do
if [ "$FRUIT" == "" ]; then
echo -e " .\n"
continue
elif [ $FRUIT = STOP ]; then
echo " !"
break
fi
echo " $FRUIT."
echo -e " $REPLY.\n"
done
$ ./fruit2
1)
3)
5)
7)
2)
4)
6)
: 3
.
3.

447
: 99
.
: 7
!

PS3
select fruit2 do done.
,
_ ($FRUIT) . $FRUIT
, echo , continue PS3.
, ,
. , echo ,
break select ( ).
,
, . ( e echo
, command_menu.)

Here Document
Here document ( )
.
Here document , here , , ,
. birthday Here
document. , Here document,
(<<) .
, Here document,
.
,
. ,
, . ,
grep ,
, :
$ cat birthday
grep -i "$1" <<+
Max
June 22
Barbara February 3
Darlene May 8
Helen
March 13
Zach
January 23
Nancy
June 26
+
$ ./birthday Zach
Zach
January 23
$ ./birthday june
Max
June 22
Nancy
June 26

448 10. Bourne Again Shell


birthday Here document, , . , , birthday Zach,
Zach. .
i grep , .
bundle1
Here document. bundle ,
shell archive (shar). ,
, ,
:
$ cat bundle
#!/bin/bash
# bundle:
echo "# bash"
for i
do
echo "echo $i 1>&2"
echo "cat >$i <<' $i'"
cat $i
echo " $i"
done

, ,
,
,
Here document.
, bundle ( )
, bothfiles (
). Here document ,
bundle ( file1 file2).
bothfiles
bash. Here document cat,
bothfiles Here document :
$ cat file1
.
.
$ cat file2
.

.
$ ./bundle file1 file2 > bothfiles
$ cat bothfiles
# bash
echo file1 1>&2
cat >file1 <<' file1'
.

(Brian W. Kernighan) (Rob Pike), The

Unix Programming Environment (Englewood Cliffs, N.J.: Prentice-Hall, 1984), . 98.


.

449

.
file1
echo file2 1>&2
cat >file2 <<' file2'
.

.
file2

file1 file2 bothfiles .


bothfiles echo .
ls , bothfiles file1 file2:
$ rm file1 file2
$ bash bothfiles
file1
file2
$ ls
bothfiles
file1
file2


. 289,
, .
, Linux

( ).
.
.
, .
, , . Linux :
( 0), ( 1) ( 2). . ,
>
1>
2>.
, , , 0, 1 2,
, - . ,
- .

Bourne Again Shell exec:

exec n> _
exec m< _

450 10. Bourne Again Shell


_ , n. _
, m.
<& _, >& _. ,
, , .
n m :
exec n<&m
, . -, -
, ,
>&n,
<&n. -, read
(. read: . 466) echo.
, (. . 363),
. ,
,

exec n<&

, mycp,
, ,
, , . , , ,
. mycp
.


mycp ; ,
. ( :
,
, , .) .
e,
. () (. . () source:
. 287). , ,
(. . 345).
function mycp ()
{
case $# in
0)
#
# 3
# 4
exec 3<&0 4<&1
;;
1)
#
# , ,
# 3

451
# 4
exec 3< $1 4<&1
;;
2)
#
# , ,
# 3
# , ,
# 4
exec 3< $1 4> $2
;;
*)
echo " : mycp [ []]"
return 1
;;
esac
# cat , 3,
# , 4
cat <&3 >&4
# 3 4
exec 3<&- 4<&}

, cat. 3 4,
cat,
.
,
.
, , ,
.
$ cat sortmerg
#!/bin/bash
usage ()
{
if [ $# -ne 2 ]; then
echo " : $0 file1 file2" 2>&1
exit 1
fi
}
# ,
: ${TEMPDIR:=/tmp}
#
usage "$@"
#
file1=$TEMPDIR/$$.file1
file2=$TEMPDIR/$$.file2
#
sort $1 > $file1
sort $2 > $file2

452 10. Bourne Again Shell

# $file1 $file2 . 3 4.
exec 3<$file1
exec 4<$file2
# , .
read Line1 <&3
status1=$?
read Line2 <&4
status2=$?
# : :
#
, .
#
, .
while [ $status1 -eq 0 -a $status2 -eq 0 ]
do
if [[ "$Line2" > "$Line1" ]]; then
echo -e "1.\t$Line1"
read -u3 Line1
status1=$?
else
echo -e "2.\t$Line2"
read -u4 Line2
status2=$?
fi
done
# .
# , .
# file1:
while [ $status1 -eq 0 ]
do
echo -e "1.\t$Line1"
read Line1 <&3
status1=$?
done
# file2:
while [[ $status2 -eq 0 ]]
do
echo -e "2.\t$Line2"
read Line2 <&4
status2=$?
done
#
exec 3<&- 4<&rm -f $file1 $file2
exit 0


. 304.
, ,
, .

453

,
Bourne Again Shell ,
. ,
( 0).

:
=(1 2 )
NAMES :
$ NAMES=(max helen sam zach)

:
$ echo ${NAMES[2]}
sam

, [*] [@], ,
-. @ , , * (
), , , IFS ( ).
A NAMES *, B @.
declare a ( , bash ):
$ A=("${NAMES[*]}")
$ B=("${NAMES[@]}")
$ declare -a
declare -a A='([0]="max helen sam zach")'
declare -a B='([0]="max" [1]="helen" [2]="sam" [3]="zach")'

declare -a NAMES='([0]="max" [1]="helen" [2]="sam" [3]="zach")'

declare , NAMES B
. A, * , : A
.
echo 1 A.
, A ,
0. 0 A . 1 B , 0 .
$ echo ${A[1]}
$ echo ${A[0]}
max helen sam zach
$ echo ${B[1]}
helen
$ echo ${B[0]}
max

454 10. Bourne Again Shell



${#[*]}:
$ echo ${#NAMES[*]}
4

, * , :
$ echo ${#NAMES[1]}
5

,
:
$ NAMES[1]=max
$ echo ${NAMES[*]}
max max sam zach


, . , ,
(). bash
export.
tcsh,
, setenv
(. . 384). bash,
.
export ,
,
.
.
extest1 american cheese, (extest1)
cheese. extest1 subtest,
. subtest cheese . subtest ,
, extest1.
extest1 cheese.
export

$ cat extest1
cheese=american
echo "extest1 1:
subtest
echo "extest1 2:
$ cat subtest
echo "subtest 1:
cheese=swiss
echo "subtest 2:

$cheese"
$cheese"
$cheese"
$cheese"

455
$ ./extest1
extest1 1: american
subtest 1:
subtest 2: swiss
extest1 2: american

subtest cheese
extest1, extest1 . bash,
, . ,
subtest, , .
extest2 extest1, ,
export, cheese
subtest:
$ cat extest2
export cheese=american
echo "extest2 1: $cheese"
subtest
echo "extest2 2: $cheese"
$ ./extest2
extest2 1: american
subtest 1: american
subtest 2: swiss
extest2 2: american

cheese american
swiss.
, cheese :
american.
export :
export cheese=american

:
cheese=american
export cheese

, ,
.
.

, ,
,
, .
$
>
>
>

function nam () {
echo $myname
myname=zach
}

456 10. Bourne Again Shell


$ myname=sam
$ nam
sam
$ echo $myname
zach

myname sam. nam myname (sam)


zach . echo ,
myname zach.
, .
, , ,
( ), , . .
typeset
, .
. count.
10 . , echo,
count_down. count typeset
. , ,
4 1, echo, . , ,
(.
. 450).

$ function count_down () {
> typeset count
> count=$1
> while [ $count -gt 0 ]
> do
> echo "$count"
> ((count=count-1))
> sleep 1
> done
> echo "."
> }
$ count=10
$ count_down 4
4
3
2
1
.
$ echo $count
10

457

((count=count1)) , (.
. 480). ($).


, .

($). ,
,
.

$$: PID-
$$ PID- , .
echo , ps . ,
PID- 5209:
$ echo $$
5209
$ ps
PID TTY
5209 pts/1
6015 pts/1

TIME CMD
00:00:00 bash
00:00:00 ps

echo ,
. , , echo ,
$$ ,
. echo (/bin/echo),
, , .
$$
cp :
$ echo $$
8232
$ cp memo $$.memo
$ ls
8232.memo memo

PID-
, ; . ,

.

458 10. Bourne Again Shell


,
. id2 PID- ,
( , ; $$
, id2):
$ cat id2
echo "$0 PID= $$"
$ echo $$
8232
$ id2
./id2 PID= 8362
$ echo $$
8232

echo PID

- . id2 ($0)
PID
- , . echo , PID-
.
PID- , , $! ( tcsh). sleep
, echo $!:

$!

$ sleep 60 &
[1] 8376
$ echo $!
8376

$?:
- , .
. $? ( tcsh $status)
.
false , . true , .
ls , ,
:
$ ls es
es
$ echo $?
0
$ ls xxx
ls: xxx: No such file or directory
$ echo $?
1

, ,
exit, ,
. exit ,

459

,
.
$ cat es
echo 7.
exit 7
$ es
7.
$ echo $?
7
$ echo $?
0

es
exit, 7,
. echo
es. echo
echo. 0 , echo
.


.
,
.
set (. set:
. 461).
. set tcsh .

$#:
$# ( ), :
$ cat num_args
echo " , $#."
$ ./num_args sam max zach
, 3

$0:
,
$0. ,
:
$ cat abc
echo " $0"
$ ./abc
./abc
$ ~sam/abc
/home/sam/abc

460 10. Bourne Again Shell


echo .
basename :
$ cat abc2
echo " $(basename $0)"
$ ~sam/abc2
abc2

$1$n:
$1,
$2 , $n. n 9
. , ${12}. ,
:
$ cat display_5args
echo $1 $2 $3 $4 $5
$ ./display_5args zach max helen
zach max helen

display_5args .
, , . $4 $5
.

shift:
shift .
( $1) . ( $2) (
$1 ), . .

unshift
, . shift
( ); 1.
demo_shift . , echo
. ,
:
$ cat demo_shift
echo "arg1= $1
arg2= $2
arg3=
shift
echo "arg1= $1
arg2= $2
arg3=
shift
echo "arg1= $1
arg2= $2
arg3=
shift
echo "arg1= $1
arg2= $2
arg3=
shift
$ ./demo_shift alice helen zach

$3"
$3"
$3"
$3"

461
arg1= alice
arg2= helen
arg3= zach
arg1= helen
arg2= zach
arg3=
arg1= zach
arg2=
arg3=
arg1=
arg2=
arg3=

shift ,
. ,
shift, ifthenelse . 421.

set:
set , $1 ( tcsh ). set
$1, $2 $3:
$ cat set_it
set this is it
echo $3 $2 $1
$ ./set_it
it is this

(. .358)
set , . , date set
. . cat
dateset.

date. , echo $*, ,
set. $1, $2, $3 $6. ,
:
$ date
Wed Aug 13 17:35:29 PDT 2008
$ cat dateset
set $(date)
echo $*
echo
echo " 1: $1"
echo " 2: $2"
echo " 3: $3"
echo " 6: $6"
echo
echo "$2 $3, $6"
$ ./dateset
Wed Aug 13 17:35:34 PDT 2008
1: Wed
2: Aug
3: 13

462 10. Bourne Again Shell


6: 2008
Aug 13, 2008

date + (. date V).


set
, , , ,
. bash ,
, declare typeset, .
set , ( tcsh ).
set o: . 348.

$* $@:

display_all, $* :
$ cat display_all
echo : $*
$ ./display_all a b c d e f g h i j k l m n o p
: a b c d e f g h i j k l m n o p


. , .
, set , :
$ cat showargs
echo " $0 : $#, :$1:."
$ ./showargs a b c
./showargs : 3, :a:.
$ echo $xx
$ ./showargs $xx a b c
./showargs : 3, :a:.
$ ./showargs $xx" a b c
./showargs : 4, ::.

showargs ($#), , .
showargs . echo , $xx .
showargs $xx. showargs a b c,
showargs . showargs "" a b c, showargs

463

. showargs
,
.
$* $@ , ,
.
"$*" (
IFS [. 8, IFS:
( ) . 316] ), "$@" ,
.
"$@" , "$*".
. ,
echo .
bb1 , set "$*"
:

"$*" "$@"

$ cat bb1
set "$*"
echo '"$*"' $#
echo 1: $1
echo 2: $2
echo 3: $3
$ ./bb1 a b c
"$*" 1
1: a b c
2:
3:

bb2 , set "$@"


:
$ cat bb2
set "$@"
echo '"$@"' $#
echo 1: $1
echo 2: $2
echo 3: $3
$ ./bb2 a b c
"$@" 3
1: a
2: b
3: c


${} ( $, ) .
, bash ${} .

464 10. Bourne Again Shell


Bourne
Again Shell :
.
.
.

. , bash ,
set o nounset (. .8.13).

:
: , :
${:__}
: :
, __
. ,
LIT.
, /home/max/literature:
$ ls ${LIT:-/home/max/literature}

__
:
$ ls ${LIT:-$HOME/literature}

:=
: .
:=
${:=__}
${:=__} ,
${:-__},
__. , , LIT
, LIT
/home/max/literature:
$ ls ${LIT:=/home/max/literature}

:
(
)

: ( ), :=,
. : -

465

, .
(:) , .
(
):
: ${:=__}
TEMPDIR,
TEMPDIR /tmp, TEMPDIR :
: ${TEMPDIR:=/tmp}

:?
, - .
, :?

1:
${:?}
, ,
(
parameter null or not set). :? . TESTDIR
, , :?.
%T, ,
: , .
cd ${TESTDIR:?$(date +%T) , .}
bash: TESTDIR: 16:16:14 , .


, 5,
.
type, read, exec, trap, kill getopts. .10.6
bash. tcsh .9.10.

type:
type ( tcsh which) :
$ type cat echo who if lt
cat is hashed (/bin/cat)

466 10. Bourne Again Shell


echo is a shell builtin
who is /usr/bin/who
if is a shell keyword
lt is aliased to 'ls -ltrh | tail'

, , cat
who . cat
, -, type , cat
. , echo
echo, if , lt .

read:

,
. tcsh . 390.

:
$ cat read1
echo -n " : "
read firstline
echo " : $firstline"
$ ./read1
: .
: .

read1
echo. n , , .
firstline.
read
firstline. (
), , , ,
. , ,
* :
$ cat read1_no_quote
echo -n " : "
read firstline
echo : $firstline
$ ./read1_no_quote
: *
: read1 read1_no_quote script.1
$ ls
read1
read1_no_quote
script.1

ls

, , , . $firstline
, . read1
:

467
$ ./read1
: *
: *

read , . , , read, bash REPLY.


echo,
p. read1a
, read1:
REPLY

$ cat read1a
read -p " : "
echo " : $REPLY"

read2 ,
cmd.
, cmd:
$ cat read2
read -p " : " cmd
$cmd
echo ""

read2 , echo. ,
. read2 ,
who:
$ ./read2
: echo , .
, .

$ ./read2
: who
max
pts/4
Jun 17 07:50 (:0.0)
sam
pts/12
Jun 17 11:54 (bravo.example.com)

cmd ,
:
$ ./read2
: xxx
./read2: line 2: xxx: command not found

read3 . read
( ) :
$ cat read3
read -p " -: " word1 word2 word3
echo " 1: $word1"
echo " 2: $word2"
echo " 3: $word3"

468 10. Bourne Again Shell


$ ./read3
-:
1:
2:
3:

, ,
read, ,
. read1
read2 ,
. read
: , ,
.
$ ./read3
-: .
1:
3:
3: .

.10.4 ,
read.
10.4. read

a aname

() aname

d delim

() delim

(Readline) , Readline
(. Readline . 334)

n num

( ) num .
num , read ;

p prompt () -

. ,

s

()

un

( ) n , read .
read u4 arg1 arg2


read arg1 arg2 <&4

. . 449,

, read
0.
EOF.
while.
names names.

469
$ cat names
Alice Jones
Robert Smith
Alice Paulson
John Q. Public
$ while read first rest
> do
> echo $rest, $first
> done < names
Jones, Alice
Smith, Robert
Paulson, Alice
Q. Public, John
$

while , (<). done,


read.
, ,
:
$ read line1 < names; echo $line1; read line2 < names; echo $line2
Alice Jones
Alice Jones

read names names.


names ,
, . read
:
$ (read line1; echo $line1; read line2; echo $line2) < names
Alice Jones
Robert Smith


exec (. . 449):
$ exec 3< names
$ read -u3 line1; echo $line1; read -u3 line2; echo $line2
Alice Jones
Robert Smith
$ exec 3<&-

exec:

exec ( tcsh) :
,
, (.
. 449). , ,
. ( ) ,

470 10. Bourne Again Shell


, .
( . 454.) exec () .

exec:
exec, ,
:
exec
, exec ,
. () (. . ()
source: . 287). . (), , exec , .
, . ()
, exec . , . ()
, exec .
exec

. ()

exec , . -
exec ,
. , :
exec

$ cat exec_demo
who
exec date
echo " ."
$ ./exec_demo
zach
pts/7
May 30 7:05 (bravo.example.com)
hls
pts/1
May 30 6:59 (:0.0)
Mon May 25 11:42:56 PDT 2009

out (.
ifthenelse . 401) exec
, . out
cat, less, , , out2,
exec cat, less:
$ cat out2
if [ $# -eq 0 ]
then
echo "Usage: out2 [-v] filenames" 1>&2
exit 1
fi
if [ "$1" = "-v" ]
then
shift
exec less "$@"

471
else
exec cat -- "$@"
fi

exec:
exec , , , .
,
, infile:
exec < infile

outfile errfile :
exec > outfile 2> errfile

exec
, exec .

, , .
/dev/tty , ;
,
, . ( tty .)
/dev/tty ,
,
. , /dev/tty,
.
to_screen1 : , . to_screen1
, /dev/tty, . out err
, .
/dev/tty

$ cat to_screen1
echo " "
echo " " 1>&2
echo " " > /dev/tty
$ ./to_screen1 > out 2> err

$ cat out

$ cat err

:
exec > /dev/tty


. to_screen2 exec

472 10. Bourne Again Shell


, > /dev/tty . ,
exec, , /dev/
tty ( ). .
$ cat to_screen2
exec > /dev/tty
echo " "
echo " " 1>&2
echo " " > /dev/tty
$ ./to_screen2 > out 2> err

exec
/dev/tty ,
, exec .
, (
), /dev/tty ( ):
read name < /dev/tty

exec < /dev/tty

trap:

. Linux

, (,
), , , . trap (tcsh onintr) () ,
, .
,
. , , ,
, , .10.5.
, kill l ( l
), trap l ( l ) man 7 signal.
10.5.

EXIT

, exit (,
)

(Hang up)

SIGHUP
HUP

SIGINT
INT

(
CONTROL+C)

(Quit)

SIGQUIT
QUIT

(
CONTROL+SHIFT+|
CONTROL+SHIFT+\)

473

(Kill)

SIGKILL
KILL

kill 9 ( ;
)

SIGTERM
TERM
(Software termination)

15

kill

(Stop)

SIGTSTP
TSTP

20

(
CONTROL+Z)

(Debug)

DEBUG

, trap (
, trap)

(Error)

ERR

, trap , ( ,
trap)

, :
, , - , , .
trap , ,
. 10.5 ( EXIT, DEBUG ERR), . KILL,

kill KILL ( kill 9). (. kill: . 475.)
trap :
trap ['][]
,
, .
, INT 2.
, trap , .
trap , ,
.
,
trap.
, , .
trap .
, . , trap
, ,
() , locktty (. until . 436).
15, :

trap '' 15

474 10. Bourne Again Shell


, trap
(2).
SIGINT, INT 2. 1:
$ cat inter
#!/bin/bash
trap 'echo ; exit 1' INT
while true
do
echo " ."
sleep 1
done
$ ./inter
.
.
.
CONTROL+C

$

inter trap
, INT.
trap , , . echo
, exit
, , .
exit,
while. ,
, true true.
true : ( ), 0 (true).
trap
, . addbanner
, , , trap:

:
(
)

$ cat addbanner
#!/bin/bash
script=$(basename $0)
if [ ! -r "$HOME/banner" ]
then
echo "$script: $HOME/banner" 1>&2
exit 1
fi
trap 'exit 1' 1 2 3 15
trap 'rm /tmp/$$.$script 2> /dev/null' 0
for file

475
do
if [ -r "$file" -a -w "$file" ]
then
cat $HOME/banner $file > /tmp/$$.$script
cp /tmp/$$.$script $file
echo "$script: $file" 1>&2
else
echo "$script: $file" 1>&2
fi
done

addbanner
, .
, ,
.
~/banner. addbanner HOME, ,
.
Max , $HOME
/home/max, Zach,
Zach , addbanner
Max Zach ( , Zach
).
trap addbanner 1
, (
) . trap 0, ,
, ,
exit .
traps
, .
trap
/dev/null. rm
, , . trap
locktty until.

kill:
kill :
kill [] PID
(, INT 2), PID
, . PID
%n (. , PID- . 155).

476 10. Bourne Again Shell


, kill TERM ( , 15). .10.5.
TERM 1, ,
, () (
):
$ kill -TERM %1

TERM kill ,
kill %1.
kill l ( l ).
:
( ),
.
.
INT, QUIT TERM.
, INT ( CONTROL+C, ).
,
KILL, ;
. kill
V. killall.

getopts:
getopts ( tcsh )
, , , Linux. getopts
:
getopts _ _ [ ]
_ , _
, , . ,
getopts . _
(:), ,
getopts.
, , getopts OPTIND ( ) OPTARG (
). OPTIND 1.
getopts,
, OPTIND , , . ,
bash OPTARG.
, ,
_ (:). _ dxo:lt:r ,
getopts d, x, o, l, t r, , o t
.

477

getopts - while .
getopts ,
_. getopts _.
, ,
:
1. b, ,
.
2. t, , ,
. /tmp.
3. u, ,
.
,
().
, ,
. getopts
:
SKIPBLANKS=
TMPDIR=/tmp
CASE=lower
while [[ "$1" = -* ]]
# [[ = ]]
do
case $1 in
-b) SKIPBLANKS=TRUE ;;
-t) if [ -d "$2" ]
then
TMPDIR=$2
shift
else
echo "$0: -t ." >&2
exit 1
fi ;;
-u) CASE=upper ;;
--) break
;;
#
*) echo "$0: $1 ." >&2 ;;
esac
shift
done


, . , case,
. while. * ;
, .

478 10. Bourne Again Shell


shift.
, ,
.
,
getopts:
SKIPBLANKS=
TMPDIR=/tmp
CASE=lower
while getopts :bt:u arg
do
case $arg in
b)
SKIPBLANKS=TRUE ;;
t)
if [ -d "$OPTARG" ]
then
TMPDIR=$OPTARG
else
echo "$0: $OPTARG ." >&2
exit 1
fi ;;
u)
CASE=upper ;;
:)
echo "$0: -$OPTARG ." >&2
exit 1 ;;
\?) echo " -$OPTARG ." >&2 ;;
esac
done

while getopts .
getopts OPTIND , . shift .
getopts case
, arg
( getopts ). , getopts
, ,
case , .
getopts , ,
. , , _ . getopts ,
_ ?, OPTARG
. getopts
_ :, OPTARG , .
\? ,
getopts . : ,
getopts .
getopts , .

479

_ , ,
_ ?. OPTARG , getopts
.
, ,
, .
getopts
.

.


.10.6 bash.
, , type
(. type: ).
5, , bash.
10.6. bash

0 true (. : ( ) . 464)

. ()

(.
. () source: . 287)

bg

(.
bg: . 301)

break

(. break continue . 438)

cd

(. cd: . 108)

continue

(.
break continue . 438)

echo

(. echo: . 77)

eval

(.
eval . 346)

exec

(. exec: . 469)

exit

( ,
CONTROL+D ; . $?: . 469)

export

( ; . . 454)

fg

(. fg:
. 300)

getopts

, (. getopts:
. 476)

jobs

(. . 299)

480 10. Bourne Again Shell


10.6 ()

kill

(. V)

pwd

(. . 102)

read

(.
read
: )

readonly

(.
. 309)

set

, ; (. set
o: . 348,
. 384 set:
. 461

shift

(. shift:
. 460)

test

(. test . 432,
V)

times

trap

(. trap: . 472)

type

, (. type: . 465)

umask

,
(.V)

unset

(. unset: . 308

wait

(. . 9.10 . 407)

, ,
. , , bash,
.10.8.


Bourne

Again

Shell

,
. . let:
$ let VALUE=VALUE * 10 + NEW"

VALUE NEW .
let ($)
. , ,

481

.
, bash (()) let "",
, :
$ ((VALUE=VALUE * 10 + NEW))

, ,
, , . (*) ,
(. 8,
):
$ let VALUE=VALUE*10+NEW

let ,
:
$ let "COUNT = COUNT + 1" VALUE=VALUE*10+NEW


:
$ ((COUNT = COUNT + 1, VALUE=VALUE*10+NEW))



.
, 8, . 356,
$(()),
$(()) .
.
let

(()),
.
.

(()) , [[]]. age_check


(. . 356),
:

$ cat age2
#!/bin/bash
echo -n "How old are you? "
read age
if ((30 < age && age < 60)); then
echo "Wow, in $((60-age)) years, you'll be 60!"
else
echo "You are too young or too old to play."
fi
$ ./age2
How old are you? 25
?
You are too young or too old to play. ,

482 10. Bourne Again Shell


- if ,
0 (true), true, 1 (false)
.

( )
:
[[ ]]


() . ($).
, test, .
, , , test (. V).
(AND) a,
[[ ]] &&. , test
o (OR), [[]]
||.
, , , age2, . [[]]

:
if [[ 30 < $age && $age < 60 ]]; then

test gt, ge, lt,


le, eq ne:
if [[ 30 -lt $age && $age -lt 60 ]]; then

test .
[[]] . > < (,
"aa" < "bbb"). = , :
[[=]], true, .
, . , [[ artist = a* ]] true (= 0), [[ a* = artist ]]
false (= 1):

$
$
0
$
$
1

[[ artist = a* ]]
echo $?
[[ a* = artist ]]
echo $?

, . bin src/
myscript.bash. true, cp
src/myscript.bash bin/myscript. , chmod myscript . -
, echo .

483
$ [[ -d bin && -f src/myscript.bash ]] && cp src/myscript.bash \
bin/myscript && chmod +x bin/myscript || echo " \
myscript "


Bourne Again Shell ,
.
, . .10.7
.
10.7.

##

%%

:
${_ }
. 10.7, ,
, .
,
:
$ SOURCEFILE=/usr/local/src/prog.c
$ echo ${SOURCEFILE#/*/}
local/src/prog.c
$ echo ${SOURCEFILE##/*/}
prog.c
$ echo ${SOURCEFILE%/*}
/usr/local/src
$ echo ${SOURCEFILE%%/*}
$ echo ${SOURCEFILE%.c}
/usr/local/src/prog
$ CHOPFIRST=${SOURCEFILE#/*/}
$ echo $CHOPFIRST
local/src/prog.c
$ NEXT=${CHOPFIRST%%/*}
$ echo $NEXT
local

${#} :
$ echo $SOURCEFILE
/usr/local/src/prog.c
$ echo ${#SOURCEFILE}
21

484 10. Bourne Again Shell

bash
, ,
C. .10.8
. . , .
10.8.

var++
var

++var
var

! ( )
~ ( )

**
, ,
*
/
%
,
-
+

<<
>>

<=
>=
<
>
==
!=

485

& (AND)
^ (XOR)
| (OR)
()
&& (AND)
|| (OR)

? :

=, *=, /=, %=, +=, =,


<<=, >>=, &=, ^=, |=

, .
, . ,

$ cmd1 | cmd2 || cmd3 | cmd4 && cmd5 | cmd6

,
$ ((cmd1 | cmd2) || (cmd3 | cmd4)) && (cmd5 | cmd6)

:

.
, ,
.

, , . - ,
( ++COUNT VALUE), (++ 1, 1),
. - ( COUNT++ VALUE);
,
.

$ N=10
$ echo $N
10
$ echo $((--N+3))
12
$ echo $N
9

486 10. Bourne Again Shell


$ echo $((N++ - 3))
6
$ echo $N
10

(%)
. , $((15%7)) 1.

0 (true), 1 (false). && () || () .



, . && , .
, bash ,
.
.

$ mkdir bkup && cp -r src bkup

bkup. mkdir , src bkup.


|| bash , : ()
, (
).
$ mkdir bkup || echo " mkdir bkup" >> /tmp/log


. .
:
$ (mkdir bkup && cp -r src bkup) || echo " mkdir" >> /tmp/log

, && ||
. true
false. true (0) false
(1) :
$ false; echo $?
1

$? (.
$?: . 458). 1 (false):
$
$
1
$
$
1

true || false && false


echo $?
(true || false) && false
echo $?

0 (true):
$ false && false || true
$ echo $?
0

487
$ (false && false) || true
$ echo $?
0

|| && ,
.
, ,
, . , :
$ ((N=10,Z=0))
$ echo $((N || ((Z+=1)) ))
1
$ echo $Z
0

N , || ()
1 (true), , . ,
((Z+=1)) , Z .
? : , ,
, , :
1 ? 2 : 3
1 false (0), 3; 2.
2 3, , .
1 true, 3 . 1
false, 2.

$
$
$
1
$
1

((N=10,Z=0,COUNT=1))
((T=N>COUNT?++Z:--Z))
echo $T
echo $Z

, +=,
. , N+=3 , ((N=N+3)).

#n 2 ( ).
v1 0101 (5 ),
v2 0110 (6 ).
echo.

$
$
$
5

((v1=2#0101))
((v2=2#0110))
echo "$v1 $v2"
6

488 10. Bourne Again Shell


(&) , 5
( 0101), 6 (0110). 0100
(4 ).
$ echo $(( v1 & v2 ))
4

(&&) 1, , 0 .
(|) , 0101, 0110,
0111 (7 ).
(||) 1,
, 0 .
$ echo $(( v1 && v2 ))
1
$ echo $(( v1 | v2 ))
7
$ echo $(( v1 || v2 ))
1

(^) , , 0101 0110,


0011 (3 ). (!) 1,
0, 0 .
$(( ! v1 )) ,
,
. 1, true,
0 .
$
3
$
0
$
1
$
0

echo $(( v1 ^ v2 ))
echo $(( ! v1 ))
echo $(( v1 < v2 ))
echo $(( v1 > v2 ))


Bourne

Again

Shell
, . , bash, , ,
, ,
C Perl. :
, .

Bourne
Again

Shell

489
.

Bourne Again Shell


. ,
Bourne Again Shell .
, , .
,
if.

, while for.
,
, .
,
.
, , , . ,
,
.
8
bash, ,
. ,
.
.


,
. - ,
, .
- , . , ,
. . ,
, .
, ,
: , ,
. .
Linux . ,
R chmod, chown cp, V.
, :
makepath, .
, makepath a/b/c/d a, a/b, a/b/c a/b/c/d.
( mkdir p.
mkdir p.)

490 10. Bourne Again Shell


:
1. .
, .
2. , ( mkdir) .
3. makepath,
.
, ,
mkdir.
, ,
, .
, :
makepath

#
#
#
function makepath()
{
if [[ ${#1} -eq 0 || -d "$1" ]]
then
return 0
#
fi
if [[ "${1%/*}" = "$1" ]]
then
mkdir $1
return $?
fi
makepath ${1%/*} || return 1
mkdir $1
return $?
}

( if ) ,
/ (. .10.7). (,
$1 max), .
, , , , /usr,
${1%/*} .
, :

. , : ,
.
, , , :
$ set -o xtrace

491

( , ,
(+).)
. (+).
, +, , makepath.
a/b/c. a/b, , a. ( mkdir)
makepath.
$
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

./makepath a/b/c
makepath a/b/c
[[ 5 -eq 0 ]]
[[ -d a/b/c ]]
[[ a/b = \a\/\b\/\c ]]
makepath a/b
[[ 3 -eq 0 ]]
[[ -d a/b ]]
[[ a = \a\/\b ]]
makepath a
[[ 1 -eq 0 ]]
[[ -d a ]]
[[ a = \a ]]
mkdir a
return 0
mkdir a/b
return 0
mkdir a/b/c
return 0


.
makepath ,
. ,
, /a/b. .
, .
$
+
+
+
+
+
+
+
+
+
+
+
+

./makepath /a/b
makepath /a/b
[[ 4 -eq 0 ]]
[[ -d /a/b ]]
[[ /a = \/\a\/\b ]]
makepath /a
[[ 2 -eq 0 ]]
[[ -d /a ]]
[[ '' = \/\a ]]
makepath
[[ 0 -eq 0 ]]
return 0
mkdir /a

492 10. Bourne Again Shell


mkdir: cannot create directory '/a': Permission denied
/a':
+ return 1
+ return 1

, makepath
/a. ,
makepath .


,
.
. ,
, .
,
, ,
, .
.
, , . ,
,
typeset. . 455.

quiz
, bash:
quiz ( ), .
, .

.

: ,
?
? ?
( ) ,
?
, . :

quiz. QUIZDIR,
~/quiz. , , (engineering), (art) (politics): ~/
quiz/engineering, ~/quiz/art ~/quiz/politics. ,
quiz , /usr/games (
root-).
. .

493
, , .

, . (
read.)
, .
. . :
Who discovered the principle of the lever?
4
Euclid
Archimedes
Thomas Edison
The Lever Brothers
Archimedes
q


Lever Brothers

. quiz , CONTROL+C,
,
, . ,
,
.
q , .
:
1. . ,

. , , CONTROL+C.
2. .
3. .
4. ( ).
.
5. , quiz .
6. .
, (, 3) ,
, (, 4), . ,
, ,
trap.
, , :
function initialize
{
# .
}

494 10. Bourne Again Shell


function choose_subj
{
# .
}
function scramble
{
#
# - questions.
}
function ask
{
# , .
# 1 0 .
# , 2.
}
function summarize
{
# , .
}
#
initialize

# 1

subject=$(choose_subj)
[[ $? -eq 0 ]] || exit 2
cd $subject || exit 2
echo
scramble

#
#
#
#
#

for ques in ${questions[*]}; do


ask $ques
result=$?
(( num_ques=num_ques+1 ))
if [[ $result == 1 ]]; then
(( num_correct += 1 ))
fi
echo
sleep ${QUIZDELAY:=1}
done
summarize
exit 0

# 5

2

3

4

#
# 6

, sleep. ,
$QUIZDELAY ( = 1).
. ,
.
( ), . ,

495

, .
,
. ,
,
.
.
.
cd , QUIZDIR,
, QUIZDIR ,
~/quiz.
function initialize ()
{
trap 'summarize ; exit 0' INT
num_ques=0
num_correct=0
first_time=true
cd ${QUIZDIR:=~/quiz} || exit 2
}

#
#
#
#




true,

, cd . ,
. cd ,
2.
, choose_subj, . , select:
function choose_subj ()
{
subjects=($(ls))
PS3=" : "
select Subject in ${subjects[*]}; do
if [[ -z "$Subject" ]]; then
echo " . ." >&2
exit 1
fi
echo $Subject
return 0
done
}

ls ,
subjects. select (.
select . 445)
( , ls)
Subject. ,
.
subject : [subject=$(choose_
subj)].
scramble .
(questions).
, RANDOM (

496 10. Bourne Again Shell


RANDOM
0 32767):
function scramble ()
{
typeset -i index quescount
questions=($(ls))
quescount=${#questions[*]}
((index=quescount-1))
while [[ $index > 0 ]]; do
((target=RANDOM % index))
exchange $target $index
((index -= 1))
done
}

questions (), .
quescount , .
: index ,
quescount 1 ( ).
index
0 index .
((target=RANDOM % index))

0 index 1 ( %), $RANDOM index. questions, target


index. exchange:
function exchange ()
{
temp_value=${questions[$1]}
questions[$1]=${questions[$2]}
questions[$2]=$temp_value
}

ask select. , , ,
, (. ,
). ,
$1,
ask 3. ques, num_opts. choices,
,
. PS3 ques, select
, ques. select
answer,
.
choices
. , ,

497

choices . , , choices .
select:
quiz

$ cat quiz
#!/bin/bash
# , #
# set -o xtrace
#==================
function initialize ()
{
trap 'summarize ; exit 0' INT
#
num_ques=0
#
num_correct=0
#
first_time=true
# true,
cd ${QUIZDIR:=~/quiz} || exit 2
}
#==================
function choose_subj ()
{
subjects=($(ls))
PS3=" : "
select Subject in ${subjects[*]}; do
if [[ -z "$Subject" ]]; then
echo " . ." >&2
exit 1
fi
echo $Subject
return 0
done
}
#==================
function exchange ()
{
temp_value=${questions[$1]}
questions[$1]=${questions[$2]}
questions[$2]=$temp_value
}
#==================
function scramble ()
{
typeset -i index quescount
questions=($(ls))
quescount=${#questions[*]}
#
((index=quescount-1))
while [[ $index > 0 ]]; do
((target=RANDOM % index))
exchange $target $index
((index -= 1))
done
}

498 10. Bourne Again Shell


#==================
function ask ()
{
exec 3<$1
read -u3 ques || exit 2
read -u3 num_opts || exit 2
index=0
choices=()
while (( index < num_opts )) ; do
read -u3 next_choice || exit 2
choices=("${choices[@]}" "$next_choice")
((index += 1))
done
read -u3 correct_answer || exit 2
exec 3<&if [[ $first_time = true ]]; then
first_time=false
echo -e " .\n"
fi
PS3=$ques" "
# $ques select
# .
select answer in "${choices[@]}"; do
if [[ -z "$answer" ]]; then
echo . , .
elif [[ "$answer" = "$correct_answer" ]]; then
echo "!"
return 1
else
echo ", : $correct_answer."
return 0
fi
done
}
#==================
function summarize ()
{
echo
#
if (( num_ques == 0 )); then
echo " "
exit 0
fi
(( percent=num_correct*100/num_ques ))
echo " $num_correct \
$num_ques ."
echo " $percent."
}
#==================
# Main program
initialize
# 1
subject=$(choose_subj)
# 2
[[ $? -eq 0 ]] || exit 2
#

499
cd $subject || exit 2
echo
scramble
for ques in ${questions[*]}; do
ask $ques
result=$?
(( num_ques=num_ques+1 ))
if [[ $result == 1 ]]; then
(( num_correct += 1 ))
fi
echo
sleep ${QUIZDELAY:=1}
done
summarize
exit 0

#
#
#
#

3

4
5

#
# 6

. ,
, .
, ,
. .
,
, ifthen, ifthenelse ifthenelif.
case .
forin, for, until while.
.
break continue : break , continue
.
Here document ,
.

Bourne Again Shell . read echo,


, , .

typeset bash
, readonly ( ). Bourne
Again Shell ,
, .

500 10. Bourne Again Shell



let , , C.
Bourne Again Shell
, type, read, exec, trap, kill getopts. type , , read , .
exec .
, PID-
.

Linux
, .
trap , Linux , , ,
.
,
, .
kill . getopts ,
, ,
Linux .

,
, Linux.
, find ,
, ,
.

: . , .
()
true false.
, Linux, true
, false .

,
, , ,
,
, , .

501

1. journal 5 8,
journal-file,
. , journal-file
.
.
2. out (. ifthenelse . 421)
"$@". , , "$*".
3. ,
(. case . 444
, .
4. , .
1) ?
2) ,
,
, .
3) , ?
4) , . ?
5.
? .
6. ,
, .
7. , 15. man-,
date, , %r.
( clear) .
8. , savefiles,
:
$ cat savefiles
#! /bin/bash
echo " savethem."
exec > savethem
for i in *
do
echo "==================================================="
echo ": $i"
echo "==================================================="
cat "$i"
done

1) ?
, ,
savethem.
2) ,
? .

502 10. Bourne Again Shell


9. man- info-, bash, :
1) ?
2) hash?
3) , ,
exec ?
10. find, :
1) ,
.
2) , , .
3) core ,
.
4) i- , .c.
5) , ,
30 .
11. , , .
, .
, . (.
cut.)
12. ,
, .
. ,
. f (force )
,
, .


1. , , , ( ).
2. 13 ,
- .
-
.
3. funload, , . funload
, ;
funload .
, funload ,
FUNPATH .
, FUNPATH , PATH,
FUNPATH , PATH.

503
4. bundle (. Here Document . 447),
. ,
, .
, , srcshell
.c test1.c test2.c.
:
$ bash srcshell test1.c test2.c

5. lnks (.
ifthenelif . 423)? ?
6. , ,
, while until. makepath
(. . 489),
. ? ?
7. (:)
. ( PATH.)

:
PATH=/opt/bin:$PATH

,
. addenv, : , ,
, ,
. ,
addenv PATH /opt/bin
/opt/bin PATH ,
. ,
.
. PATH /usr/opt/bin, /opt/
bin, /opt/bin
PATH. (. , , locate_field, ,
.)
8. ,
, . ,
, .
9. , 20,
,
.
10. , , , , , FIFO
, . :
1) lsl.
2) [[]].
11. quiz (. quiz . 492),
.

11. Perl

(Larry Wall)

Perl (Practical Extraction and Report Language

.

Perl
, awk, sed, C, Bourne Shell,
Smalltalk, Lisp .

. 1987

, Perl

, .
, , Perl ,
.
Perl , Perl
(. www.cpan.org/ports). Perl
, , , , . ,
- ,
.

Perl
. - I will buy a car if
I
win

the

lottery

( , ).
Perl
. ,
Perl ,
, .

Perl
Perl :
Perl .

,
, Perl, , ,
. Do What
I Mean, DWIM, , .

.
Perl Theres more than one way to do it (
). ,
.
Perl

, , .
Perl The Comprehensive Perl Archive Network (CPAN; www.cpan.org),
,

Perl 505

Perl. ,
Perl CPAN . 544.

PERL-DOC
perl-doc .
Perl, .


Perl
. ,
.
Perl
. #!/usr/bin/
perl,
Perl. , /usr/bin/perl,
Perl. (r) ,
/usr/bin/perl /usr/bin /usr/sbin, grep,
, Perl:
$ grep -r /usr/bin/perl /usr/bin /usr/sbin | head -4
/usr/bin/defoma-user:#! /usr/bin/perl -w
/usr/bin/pod2latex:#!/usr/bin/perl
/usr/bin/pod2latex:
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
/usr/bin/splain:#!/usr/bin/perl

, , Perl
. .
root-, , .

man-: man- perl perltoc,


man-, Perl

Perl: www.perl.com

CPAN: www.cpan.org
: perlbuzz.com
Programming Perl, , (Wall),
(Christiansen) (Orwant), OReilly & Associates, 2000.
2006 .

Perl , .

Perl
, , , . Perl ,
. w
. strict (. man- perldebtut)
, ,

506 11. Perl


.
- ,
Perl .
man- perldebtut perldebug.

perldoc
perldoc, perl-doc.
perldoc Perl.
man (. man:
.53), Perl. ,
pod (plain old documentation
). Perl pod
, .
Perl,
pod. , =cut, ,
pod.
$ cat pod.ex1.pl
#!/usr/bin/perl
=head1 Perl-, I<.>
Perl- B<pod>.
B<=cut> B<perldoc>,
.
=cut
# Perl
print ".\n";
=head1 pod pod
B<pod> B<perldoc.perl.org/perlpod.html>,
Perl B<perldoc.perl.org>.

Perl:
$ perl pod.ex1.pl
Hi there.

perldoc:
$ perldoc pod.ex1.pl
POD.EX1(1)
User Contributed Perl Documentation
POD.EX1(1)
Perl-, .
Perl- pod.
=cut perldoc,
.
pod pod
pod perldoc.perl.org/perlpod.html,
Perl perldoc.perl.org.
perl v5.10.0
2008-10-14
POD.EX1(1)

Perl 507

, , , Perl,
pod-. ,
Perl print:
$ perldoc
print
print
print

-f print
FILEHANDLE LIST
LIST
Prints a string or a list of strings. Returns true if successful. FILEHANDLE may be a scalar variable name, in which case
the variable contains the name of or a reference to the filehandle, thus introducing one level of indirection. (NOTE: If
FILEHANDLE is a variable and the next token is a term, it may

- (. CPAN
. 544),
perldoc. perldoc
Timestamp::Simple:
$ perldoc Timestamp::Simple
Timestamp::Simple(3) User Contributed Perl Documentation Timestamp::Simple(3)
NAME
Timestamp::Simple - Simple methods for timestamping
SYNOPSIS
use Timestamp::Simple qw(stamp);
print stamp, "\n";

man-, perldoc,
, man perldoc perldoc perldoc.

PERL
Perl

,
, ,
.

, .
Perl ,
.
Perl. . Perl , (::).
Time-stamp::Simple WWW::Mechanize.

Perl , .
search.cpan.org.
Timestamp-Simple ( Timestamp-Simple-1.01.tar.gz

508 11. Perl


Timestamp::Simple) WWW-Mechanize ( WWW-Mechanize-1.34.tar.gz
WWW::Mechanize, ,
WWW::Mechanize::Link WWW::Mechanize::Image).
Perl. ,
$WWW::Mechanize::ex $ex
WWW::Mechanize , .
, , WWW::Mechanize, , .

,
({}), .
.
ifthen
. 417.

, .
(, $Text::Wrap::columns).
,
.

,
my (.
. 512). ,
. . Perl 4 local , Perl 5 lexical.
bash ,
(. . 454), , .

, .
:

(2, 4, 'Zach')

, ,
. @a . ,
, , . 453.

@a = (2, 4, 'Zach')

,
. , if (.
. 520) if, , .

Perl
, Perl, .
e :

Perl 509
$ perl -e 'print ".\n"'
.

e
Perl , . ,

Perl
. ,
.
.

Perl
, (. ,
. 148), perl
, CONTROL+D ( ).
Perl
:
$ perl
print .\n";
CONTROL+D
.


, .
, Perl . ,
.pl.
, :
$ cat simple.pl
print ".\n";

, Perl:
$ perl simple.pl
.

, , ,
Perl, . chmod (.
chmod: . 292) simple2.pl . , #!
. 294, #! /usr/bin/perl.
$ chmod 755 simple2.pl
$ cat simple2.pl
#!/usr/bin/perl -w
print ".\n";
$ ./simple2.pl
.

simple2.pl ./simple2.pl,
PATH
(. PATH: ,
. 312). w Perl
.

510 11. Perl

Perl 5.10
Perl 5.10. ,
Perl ,
:
$ perl -v
This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

say Perl 6, Perl 5.10.


, print,
, (\n) . Perl say.
use Perl say.
use, ,
Perl.


say (use
feature 'say')

$ cat 5.10.pl
use feature 'say';
say ' say.';
print ' print.';
say '.'
$ perl 5.10.pl
say.
..
$

Perl,
say print \n:

Perl

$ cat
print
print
print

5.8.pl
' print say.', "\n";
' print.';
'.', "\n";

$ perl 5.8.pl
print say.
print..

Perl.
Perl , (;).
, ,
.
, . .

Perl 511

, . .
say,
say (use feature say) . 510.
$ cat statement1.pl
$n=4;
say " ", $n + 2;
$ perl statement1.pl
6
$ cat statement2.pl
$n = 4; say " ", $n+2;
$ perl statement2.pl
6

Perl C, . Perl
.


. Perl
, (. . 306):
Perl
, \n ( ), .
Perl .11.1.
, ( )
Perl

. print , Perl
$string, , \n. \n print .

$ cat string1.pl
$string="5";

# $string ,

print '$string+5\n';

#
#
print "\n$string+5\n"; #
#
print $string+5, "\n"; #
#
#

Perl $string+5 ,

Perl $string ,

Perl
$string 5;
\n

$ perl string1.pl
$string+5\n
5+5
10


(/). , hours

512 11. Perl


our; Perl
/ . 538.
$ perl -e 'if ("hours" =~ /our/) {say "yes";}'


Perl
, say (. say (use feature say) . 510):
$ perl -e 'use feature "say"; if ("hours" =~ /our/) {say "yes";}'

, ,
() . Perl "\\n" \n.
Perl ,
(. string1.pl . 511) .

, Perl (#)
(
).

.11.1 , Perl. , Perl . .11.3 ,


.

11.1. Perl

\0xx ()

ASCII-, xx

\a

(bell beep) ASCII- 7

\e

ESCAPE (ASCII- 27)

\n

, NEWLINE (ASCII- 10)

\r

RETURN (ASCII- 13)

\t

TAB (ASCII- 9)

,
Perl
. ,
. Perl : (
), ( ) (
, ). Perl

, .
($), at (@),
(%).
, Perl
, :
$ name="Zach" ; echo "$name"
Zach
$ perl -e '$name="Zach" ; print "$name\n";'
Zach

(bash)
(perl)

513

, , , (_). Perl
(. ),
my, (. ), , ,
. Perl
. 535.


,

, ,
, .

Perl ,
,
. Perl

:
$ cat variable1.pl
#!/usr/bin/perl
my $name = 'Sam';
print ", $nam, ?\n";

# , 'e' name

$ ./variable1.pl
, , ?

Perl , use
strict . man-
perldebtut. use strict Perl
:
use strict

$ cat variable1b.pl
#!/usr/bin/perl
use strict;
my $name = 'Sam';
print ", $nam, ?\n"; # , 'e' name
$ ./variable1b.pl
Global symbol "$nam" requires explicit package name at ./variable1b.pl line 4.
Execution of ./variable1b.pl aborted due to compilation errors.
"$nam" ./variable1b.pl
4.
./variable1b.pl - .

MY:
variable1.pl, $name

my
;
vari
able1.pl.
, . ,
.
, ,

514 11. Perl


.
,
, .
, ,
, , .
Perl -. ,
,
(. . 454). Perl

,
my, ,
, .

w use warnings :
Perl . Perl . , $nam
3, , , .
, . Perl 5.10
. , . , , .
w

use warnings

$ cat variable1a.pl
#!/usr/bin/perl -w
my $name = 'Sam';
print ", $nam, ?\n"; # ( w)
$ ./variable1a.pl
Name "main::nam" used only once: possible typo at ./variable1a.pl line 3.
"main::nam" : , 3
./variable1a.pl.
Use of uninitialized value $nam in concatenation (.) or string at ./variable1a.pl
line 3.
$nam (.)
./variable1a.pl, 3.
, , ?

w . e, ,
( e w ). .
e . 539.
$ perl -w -e 'my $name = "Sam"; print "Hello, $nam, how are you?\n"'
Name "main::nam" used only once: possible typo at -e line 1.
"main::nam" : , e, 1
Use of uninitialized value $nam in concatenation (.) or string at -e line 1.
$nam (.)
e, 1
, , ?

undef, (0),
(""). , ,

undef
defined

515

defined. , , , defined $name , (! ).


, print , $name .
$ cat variable2.pl
#!/usr/bin/perl
if (!defined($name)) {
print " '\$name' .\n"
};
$ ./variable2.pl
'$name' .

w Perl
,
.


($),
, .
Perl
, . Perl
,
, , .
Perl .
.
(3 4) Sam $name,
5 2 $n1 $n2 .
,
(;). ,
say (use feature 'say') . 510.
$ cat scalars1.pl
#!/usr/bin/perl -w
$name = "Sam";
$n1 = 5; $n2 = 2;
say
say
say
say
say

"$name $n1 $n2";


"$n1 + $n2";
'$name $n1 $n2';
$n1 + $n2, " ", $n1 * $n2;
$name + $n1;

$ ./scalars1.pl
Sam 5 2
5 + 2
$name $n1 $n2

516 11. Perl


7 10
Argument "Sam" isn't numeric in addition (+) at ./scalers1.pl line 11.
Sam (+) ./scalers1.pl
11
5

say , ,
( , ). Perl . say ,
. say (+).
Perl , + . Perl
.

say , , . Perl , ,
.
say , Perl

. ,
, Perl (710). say
; w Perl
, 5. Sam,
Perl 0, 5 (0 + 5 = 5).

,
, ,
, at (@)
( ). , .
, (. ,
. 519), .
Perl
. ,
, , Perl undef ( ).

@arrayvar .
Perl
, say
( 1). $arrayvar[1]
(), .
say @arrayvar[1,2] (
), ( 1 2).
$ cat arrayvar1.pl
#!/usr/bin/perl -w
@arrayvar = (8, 18, "Sam");
say $arrayvar[1];
say "@arrayvar[1,2]";

517

$ perl arrayvar1.pl
18
18 Sam


print. arrayvar2.pl @arrayvar2.
, Perl .
$num ,
@arrayvar2.
$ cat arrayvar2.pl
#!/usr/bin/perl -w
@arrayvar2 = ("apple", "bird", 44, "Tike", "metal", "pike");
$num = @arrayvar2;
print ": ", $num, "\n";
print ": $num\n";
print " : $#arrayvar2\n";

#
# print
#

$ ./arrayvar2.pl
: 6
: 6
: 5

print arrayvar2.pl : , ,
$num ,
. ,
print. print
, Perl ( ),
.
print arrayvar2.pl , Perl $#
. Perl , , , .

(.; , , ).
. , ,
, .
, Perl $va . $vb Sam,
uel, Samuel ( print).
$#

$ cat arrayvar3.pl
#!/usr/bin/perl -w
$v1 = 5; $v2 = 8;

518 11. Perl


$va = "Sam"; $vb = "uel";
@arrayvar3 = ($v1, $v1 * 2, $v1 * $v2, "Max", "Zach", $va . $vb);
print $arrayvar3[2], "\n";
print @arrayvar3[2,4], "\n";
print @arrayvar3[2..4], "\n\n";

#
#
#

print "@arrayvar3[2,4]", "\n";


# , ,
print "@arrayvar3[2..4]", "\n\n"; # , ,
print "@arrayvar3\n";

# , ,

$ ./arrayvar3.pl
40
40Zach
40MaxZach
40 Zach
40 Max Zach
5 10 40 Max Zach Samuel

print arrayvar3.pl (
2) @arrayvar3. @
$, . print @,
. , ,
. ,
print .

( .. ; ), Perl , . , , . print
2, 3 4 (, ),
2..4. Perl .
print, , ,
, Perl

. print . print , .

, . @colors, . shift
, push , pop
. splice ; splice
@ins, 1 ( ),
. say,
say (use feature say) . 510.
, , man- perlfunc.
shift, push,
pop splice

519
$ cat ./shift1.pl
#!/usr/bin/perl -w
@colors = ("red", "orange", "yellow", "green", "blue", "indigo", "violet");
say "
: @colors";
say "
: ", shift (@colors);
say "
: @colors";
push (@colors, "WHITE");
say ". - : @colors";
say " . - : ", pop (@colors);
say "
: @colors";
@ins = ("GRAY", "FERN");
splice (@colors, 1, 2, @ins);
say "
2 3 : @colors";
$ ./shift1.pl

:
:
:
. - :
. - :
:
2 3 :

red orange yellow green blue indigo violet


red
orange yellow green blue indigo violet
orange yellow green blue indigo violet WHITE
WHITE
orange yellow green blue indigo violet
orange GRAY FERN blue indigo violet

,
, , , .
() ,
, .
.
(. , . 516)
. , , , ,
, .
Perl .

:
$ cat hash1.pl
#!/usr/bin/perl -w
$hashvar1{boat} = "tuna";
$hashvar1{"number five"} = 5;
$hashvar1{4} = "fish";
@arrayhash1 = %hashvar1;
say "@arrayhash1";
$ ./hash1.pl
boat tuna 4 fish number five 5


, . -

520 11. Perl


, ,
. ,
. , , , , ,
, .
, keys values
. %hash2 hash2.pl keys
%hash2 @array_keys.
values
@array_values.
$ cat hash2.pl
#!/usr/bin/perl -w
%hash2 = (
boat => "tuna",
"number five" => 5,
4 => "fish",
);
@array_keys = keys(%hash2);
say "
: @array_keys";
@array_values = values(%hash2);
say ": @array_values";
$ ./hash2.pl
: boat 4 number five
: tuna fish 5

Perl ,
=>, boat
. , number five,
, .


Perl-. 10
bash - .
Perl , ,
bash, .

bash.
, , , , ;
, ,
Perl, ,

521

{} (. . 507).
, ,
. , if unless . 521.

if unless
if unless ,
:
if () {}
unless () {}

. if
, true, unless
, true (
false).
if , , .
, Perl (.
. 507) , {}, , .
, r memo1, r , memo1
, .
,
,
. ( Perl , bash; . .10.1.)
true, Perl (
), .
false, Perl .
, Perl
.

$ cat if1.pl
#!/usr/bin/perl -w
if (-r "memo1") {
say " 'memo1' .";
}
$ ./if1.pl
'memo1' .

,
if. , ,
, .
$ cat if1a.pl
#!/usr/bin/perl -w
say " 'memo1' ." if (-r "memo1");

522 11. Perl


print,
$entry $entry = <>;. ,
(<>) , ,
. 530.
Perl . .11.2 . if
==, , , 28. ,
28, 28.0 00028, true.
, Perl
, , .
w Perl , ;
Perl false.

$ cat if2.pl
#!/usr/bin/perl -w
print " 28: ";
$entry = <>;
if ($entry == 28) {
# ==
print " , 28.\n";
}
print ".\n";
$ ./if2.pl
28: 28.0
, 28.
.
11.2.

, ,
, ,

==

eq

True,

!=

ne

True,

<

lt

True,

>

gt

True,

<=

le

True,

>=

ge

True,

<=>

cmp

0 , 1 , 1

, , . chomp (. chomp chop .531)



,
. eq .

523

true, five.
,
5 false, ,
.
$ cat if2a.pl
#!/usr/bin/perl -w
print " 'five': ";
$entry = <>;
chomp ($entry);
if ($entry eq "five") {
# eq
print " , 'five'.\n";
}
print ".\n";
$ ./if2a.pl
'five': five
, 'five'.
.

ifelse
ifelse , bash ifthenelse (.
ifthenelse . 421).
. :
if () {} else {}

, $num1 $num2.
, if
die, .
, ifelse ,
. ,
, .
die

$ cat ifelse.pl
#!/usr/bin/perl -w
print " : ";
$num1 = <>;
print " , : ";
$num2 = <>;
if ($num1 == $num2) {
die (", .\n");
}
if ($num1 > $num2) {
print " .\n";
}

524 11. Perl


else {
print " .\n";
}
$ ./ifelse.pl
: 8
, : 8
, .
$ ./ifelse.pl
: 5.5
, : 5
.

ifelsifelse
Perl ifelsifelse
ifthenelif bash (. ifthen
elif . 423) ,
ifelse. :
if () {} elsif {} else {}
, ifelse.pl,
ifelsifelse. die print,
,
. Perl ,
, STDERR (.
. 530).
$ cat ifelsif.pl
#!/usr/bin/perl -w
print " : ";
$num1 = <>;
print " , : ";
$num2 = <>;
if ($num1 > $num2) {
print " .\n";
}
elsif ($num1 < $num2) {
print " .\n";
}
else {
print ", .\n";
}

foreach for
Perl foreach for , . ,

525

.
foreach, for, .
foreach.

foreach: 1
foreach
forin, (. forin . 429):
foreach|for [] () {}
. Perl

, , .
, Perl $_ (. $_ . 529).
foreach.
Perl Mo $item, say , .
$item Larry Curly.
, Perl ,
foreach.
. say, say (use feature 'say') . 510.
$ cat foreach.pl
foreach $item ("Mo", "Larry", "Curly") {
say "$item .";
}
$ perl foreach.pl
Mo .
Larry .
Curly .

$_, :
$ cat foreacha.pl
foreach ("Mo", "Larry", "Curly") {
say "$_ .";
}

:
$ cat foreachb.pl
@stooges = ("Mo", "Larry", "Curly");
foreach (@stooges) {
say "$_ .";
}

foreach:
$ cat foreachc.pl
@stooges = ("Mo", "Larry", "Curly");
say "$_ ." foreach @stooges;

526 11. Perl


( $item $_)
, . . uc
. ,
$stooge @stooges:
$ cat foreachd.pl
@stooges = ("Mo", "Larry", "Curly");
foreach $stooge (@stooges) {
$stooge = uc $stooge;
say "$stooge .";
}
say "$stooges[1] "
$ perl foreachd.pl
MO .
LARRY .
CURLY .
LARRY


@ARGV . 532.

last next
Perl last next Bourne Again Shell break continue (.
break continue . 438). last
, ,
, . next
,
.
if ,
$item two. , ,
say, . next last, Perl
three.
say, say (use feature 'say') . 510.
$ cat foreach1.pl
foreach $item ("one", "two", "three") {
if ($item eq "two") {
next;
}
say "$item";
}
$ perl foreach1.pl
one
three

527

foreach: 2
foreach for, C:
foreach|for (1; 2; 3) {}
1 foreach; Perl 1
, . 2 ; Perl
, 2 true. Perl 3
, 2.
foreach2.pl
; ;
, , , ,
. (<>)
. 530.
$ cat ./foreach2.pl
#!/usr/bin/perl -w
print " : ";
$start = <>;
print " : ";
$end = <>;
print " : ";
$incr = <>;
if ($start >= $end || $incr < 1) {
die (" ,\n",
" .\n");
}
foreach ($count = $start+0; $count <= $end; $count += $incr) {
say "$count";
}
$ ./foreach2.pl
: 2
: 10
: 3
2
5
8

,
, 1. || (OR);
, if, true, ,
true.

528 11. Perl


foreach $start+0
$count. 0 () $start Perl
, .
,
.

while until
while (. 10, while)
until (. until . 436)
, .
:
while () {}
until () {}
.
while , true,
until ,
true ( , false).
, , . ,
( ),
, (. chomp chop).
,
CONTROL+D.
$line = <>.

$line (<>; .
. 530). true , .
, false. while
(
), .
$ cat while1.pl
#!/usr/bin/perl -w
$count = 0;
while ($line = <>) {
print ++$count, ". $line";
}
print "\n : $count.\n";
$ ./while1.pl
.
1. .
.
2. .
CONTROL+D
: 2.

529

$count ,
. ++
(++$count; )
, Perl

. $count 1, $count++ (
), print $count
, .
$. ,
. $., :
$.

$ cat while1a.pl
#!/usr/bin/perl -w
while ($line = <>) {
print $., ". $line";
}
print "\n : $..\n";

Perl $_. Perl .


$_ , .
. ,
$line . $line, chomp
(. chomp chop . 531),
$line .
$_

while (my $line = <>) {


chomp $line;
if ($line =~ /regex/)
}

$line $_, :
while (my $_ = <>) {
chomp $_;
if ($_ =~ /regex/)
}

$_
, $_ :
while (<>) {
chomp;
if (/regex/)
}

#
#
#
#

$_
$_ chomp
$_
regex

530 11. Perl


, Perl
, () .

(. . 449).
, , (.
. 143), (.
. 288).
, .
STDIN, STDOUT STDERR.
, .
open
open (_, ['',] "__");
_
, , __.
<, Perl ().
> , >> .
. 546.

print .
print :
print [_] "";
_ , open,
, . _,
, STDOUT STDERR. ,
, print
. _ .
print ,
.

, (\n), -

_:
<_>
,
$line = <IN>;

$line
, IN.
, ,
, Perl
.
. :

(<>)

$line = <>;

531

Perl
Linux: ,
,
, . , cat, ,
. 148.
print, ,
STDOUT; print ; print STDERR,
.
print - . , , , ,
.
,
, $userline. , file1.pl , , ,
. , file1.pl, 2> (. . 289)
( print) file1.err.
$ cat file1.pl
print STDOUT " -: ";
$userline = <>;
print "1>>>$userline<<<\n";
chomp ($userline);
print "2>>>$userline<<<\n";
print STDERR "3. .\n";
$ perl file1.pl 2> file1.err
-:
1>>>
<<<
2>>><<<
$ cat file1.err
3. .

print, file1.pl, $userline,


(>), (<).
, $userline
: ,
, . chomp , . $userline chomp print ,
. ( chop ,
chomp, ,
.)
chomp chop

532 11. Perl


, . $infile /usr/share/dict/words
open. while
, $infile,
$line. while , false, while.
, , ,
, .
/usr/share/dict/words .
head (. head:
. 72), ( ), (|, . | (): . 76).
$ cat file2.pl
open (my $infile, "/usr/share/dict/words") or die " : $!\n";
while ($line = <$infile>) {
print $line;
}
$ perl file2.pl | head -4
A
A's
AOL

$! .
, . words , file2.pl :
$!

: No such file or directory


, :
: Permission denied

$!
, , .

,
Perl ,
, , open . file2.pl
or open Perl die (. die
. 523), open . die
, ,
.

@ARGV ,
Perl.
, .
, die
@ARGV

533

. foreach , @ARGV,
$filename .
foreach open. Perl open,
(or), ,
, Perl , or ( die). Perl ,
$filename, IN , (
) die .
print , , .
Perl $line = <IN> print,
$line .
.
$ cat file3.pl
foreach $filename (@ARGV) {
open (IN, $filename) or die " '$filename': $!\n";
print "$filename: ", $line = <IN>;
close (IN);
}
$ perl file3.pl f1 f2 f3 f4
f1: First line of file f1.
f2: First line of file f2.
'f3': No such file or directory

, , Perl, . ,
, . Perl
. $filename my.
while , , ;
$ARGV. ,
[(<>)] while false, while , . Perl
, , .
Perl .
, . while .
, .
while if , .
, if , $filename ($ARGV).
$ cat file3a.pl
my $filename;
while (<>) {
if ($ARGV ne $filename) {

534 11. Perl


print "$ARGV: $_";
$filename = $ARGV;
}
}
$ perl file3a.pl f1 f2 f3 f4
f1: First line of file f1.
f2: First line of file f2.
Can't open f3: No such file or directory at file3a.pl line 3, <> line 3.
f3: ; file3a.pl 3,
<> 3.
f4: First line of file f4.

sort , .
reverse sort; .
@colors , .
sort,
@colors , @scolors
@scolors .
, .
Orange Violet, .

sort, {$a cmp $b}, Perl cmp,
. sort ,
, Perl
.
reverse

$ cat sort3.pl
@colors = ("red", "Orange", "yellow", "green", "blue", "indigo", "Violet");
say "@colors";
@scolors = sort {$a cmp $b} @colors;
say "@scolors";

#
#

@scolors = sort @colors;


say "@scolors";

#
#

@scolors = sort {$b cmp $a} @colors;


say "@scolors";

@scolors = sort {lc($a) cmp lc($b)} @colors;


say "@scolors";

#
#

535
$ perl sort3.pl
red Orange yellow green blue indigo
Orange Violet blue green indigo red
Orange Violet blue green indigo red
yellow red indigo green blue Violet
blue green indigo Orange red Violet

Violet
yellow
yellow
Orange
yellow

$a $b , .

, ,
. Orange Violet
.
, cmp <=>. :
$ cat sort4.pl
@numbers = (22, 188, 44, 2, 12);
print "@numbers\n";
@snumbers = sort {$a <=> $b} @numbers;
print "@snumbers\n";
@snumbers = sort {$b <=> $a} @numbers;
print "@snumbers\n";
$ perl sort4.pl
22 188 44 2 12
2 12 22 44 188
188 44 22 12 2

(. . 507),
(. )
my. , , .
add().
$one, $two $ans,
: , .
,
. ,
, ,
my.
subroutine1.pl ,
. .

536 11. Perl


$ cat subroutine1.pl
$one = 1;
$two = 2;
add();
print ": $ans\n";
sub add {
$ans =$one + $two
}
$ perl subroutine1.pl
: 3

,
return, .
$ans . .
$ cat subroutine2.pl
$one = 1;
$two = 2;
$ans = add();
print ": $ans\n";
sub add {
return ($one + $two)
}
$ perl subroutine2.pl
: 3

.
. , .
- , Perl
@_. @_ , ,
. @_
, .
.
subroutine3.pl addplusone() , .
@_. my
. my
, ,
.
@_

537

$lcl_one $lcl_two .
print $lcl_one. return
, .
$ cat subroutine3.pl
$one = 1;
$two = 2;
$ans = addplusone($one, $two);
print ": $ans\n";
print " 'lcl_one' : $lcl_one\n";
print " 'one' : $one\n";
sub addplusone {
my ($lcl_one, $lcl_two) = @_;
$lcl_one++;
$lcl_two++;
print " 'lcl_one' : $lcl_one\n";
return ($lcl_one + $lcl_two)
}
$ perl subroutine3.pl
'lcl_one' : 2
: 5
'lcl_one' :
'one' : 1

, , print , $lcl_one
( ), $one .
,
. , ,
@_,
.
$ cat subroutine4.pl
$one = 1;
$two = 2;
$ans = addplusone($one, $two);
print ": $ans\n";
sub addplusone {
return ($_[0] + $_[1] + 2);
}
$ perl subroutine4.pl
: 5

Perl.
max()
. $biggest , ,
shift.

538 11. Perl


shift 2 1 (8),
3 2 (64), 4
3 (2). foreach (@_). foreach Perl $_ .
$biggest $_, ,
$biggest. max()
, $biggest ,
.
$ cat subroutine5.pl
$ans = max (16, 8, 64, 2);
print " : $ans\n";
sub max {
my $biggest = shift;

#
# , max()
foreach (@_) {
#
$biggest = $_ if $_ > $biggest;
}
return ($biggest);
}
$ perl subroutine5.pl
: 64


Linux

, Perl.
Perl ,
.

,
Perl
. Perl, .

=~
Perl l chomp
\n
. Perl l e (.
Perl . 508).
, Perl . Perl.
l

Perl (/).
ge aged =~ .

539

. , =~ , ge aged. , ,
; ge aged.
, , true, Perl print.
$ perl -le 'if ("aged" =~ /ge/) {print "true";}'
true

if:
$ perl -le 'print "true" if "aged" =~ /ge/'
true

E
e ,
e. , Perl ,
; e ,
, .
perl l e perl le, perl e l perl el.

!~ =~. true,
xy aged:

!~

$ perl -le 'print "true" if ("aged" !~ /xy/)'


true

,
a..d aged:
$ perl -le 'print "true" if ("aged" =~ /a..d/)'
true

. :
qr//

/a..d/ ( ) $re :
$ perl -le '$re = qr/a..d/; print "true" if ("aged" =~ $re)'
true

,
. , (/), . Perl
/ /usr , /, , ()
(\).
$ perl -le 'print "true" if ("/usr/doc" =~ /\/usr/)'
true

540 11. Perl


m
, {}. :
$ perl -le 'print "true" if ("/usr/doc" =~ m{^/usr})'
true

:
$ perl -le '$pn = qr{^/usr}; print "true" if ("/usr/doc" =~ $pn)'
true

( ), , Perl ,
. ,
vim sed.
s Perl
, (worst, ),
, , (best). ,
, , =~.

$ cat re10a.pl
$stg = "This is the best!";
$stg =~ s/best/worst/;
print "$stg\n";
$ perl re10a.pl
This is the worst!

. 11.3 , Perl .
perldoc perlre.
11.3. Perl

^ ( )

$ ( )

()

(.
)

. ()

(\n)

\\

(\)

\b

( )

\B

, ([^\b])

\d

([09])

\D

, ([^09]
[^\d])

\s ( )

, ,
, ,

541

\S ( )

([^\s])

\w ( )

( ; [azAZ09])

\W ( )

, ([^\w])


Perl , ,
. /{.*} / ,
, (
{ } { }).
Perl
(//).
$ cat 5ha.pl
$string = " { } { } .";
$string =~ s/{.*} //;
print "$string\n";
$ perl 5ha.pl
.

, ,
.
.
,
, ,
( [^}]),
(*),

,
.
( , , ,
, , [^}] ,
.)

$ cat re5b.pl
$string = " { } { } .";
$string =~ s/{[^}]*} //;
print "$string\n";
$ perl re5b.pl
{ } .

Perl
, . {.*?}

542 11. Perl


, , ,
, , .
$ cat re5c.pl
$string = " { } { } .";
$string =~ s/{.*?} //;
print "$string\n";
$ perl re5c.pl
{ } .


. Linux
( \( \)).
Perl . Perl
.
Perl, .

. ,
, , , Perl
.
My name is Sam $stg.
/My name is (.*)/ ,
$stg. ,
, Sam; $1
( ) . , $stg, Sam.
$ cat re11.pl
$stg = "My name is Sam";
$stg =~ s/My name is (.*)/$1/;
print "Matched: $stg\n";
$ perl re11.pl
Matched: Sam


. ,
. . \D*
, : \D
.
, ( What is). \d+
.

543

, ; $1
, . .*
.
$string.
,
$string2. print .
$ cat re8.pl
$string = " 488 78?";
$string2 = $string;
$string =~ s/\D*(\d+).*/$1/;
$string2 =~ s/\D*\d+\D*(\d+).*/$1/;
print "$string\n";
print "$string2\n";
print $string - $string2, "\n";
$ perl re8.pl
488
78
410

,

,
.

. ag(e ,
age, , ag(ed).
$ perl -le 'if ("ag(ed)" =~ /ag(ed)/) {print "true";} else {print "false";}'
false

, ,
,
Perl .
.
$ perl -le 'if ("ag(ed)" =~ /ag\(ed\)/) {print "true";} else {print "false";}'
true

Perl :
$ perl -le 'if ("ag(ed)" =~ /ag(e/) {print "true";} else {print "false";}'
Unmatched ( in regex; marked by <-- HERE in m/ag( <-- HERE e/ at -e line 1.
( , <-- HERE m/ag( <-- HERE e/ e,
1

, , Perl
:
$ perl -le 'if ("ag(ed)" =~ /ag\(e/) {print "true";} else {print "false";}'
true

544 11. Perl

CPAN
CPAN (Comprehensive Perl Archive Network
Perl) - (www.cpan.org)

Perl, (FAQ), (.
. 507) . 16000
(. ) ,

Perl
, ( Perl).
- search.cpan.org
, .
, CPAN
. Perl
, (::). , , Timestamp::Simple, - search.cpan.org/
dist/Timestamp-Simple. (timestamp)
YYYYMMDDHHMMSS
- Perl,
, . - search.cpan.
org/~shoop/Timestamp-Simple-1.01/Simple.pm
Download ().
.
.
Perl tar- (.
tar- . 83). ,
:
$ tar xzvf Timestamp-Simple-1.01.tar.gz
Timestamp-Simple-1.01/
Timestamp-Simple-1.01/Simple.pm
Timestamp-Simple-1.01/Makefile.PL
Timestamp-Simple-1.01/README
Timestamp-Simple-1.01/test.pl
Timestamp-Simple-1.01/Changes
Timestamp-Simple-1.01/MANIFEST
Timestamp-Simple-1.01/ARTISTIC
Timestamp-Simple-1.01/GPL
Timestamp-Simple-1.01/META.yml

README, ,
. .
$ cd Timestamp-Simple-1.01
$ perl Makefile.PL
Checking if your kit is complete
Looks good
Writing Makefile for Timestamp::Simple



Makefile Timestamp::Simple

, , perl Makefile.PL -

CPAN 545

.
(makefile), .
, , .
make,
. make
make test, .
$ make
cp Simple.pm blib/lib/Timestamp/Simple.pm
Manifying blib/man3/Timestamp::Simple.3pm
$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl
1..1
# Running under perl version 5.100000 for linux
# Current time local: Fri Sep 4 18:20:41 2009
# Current time GMT:
Sat Sep 5 01:20:41 2009
# Using Test.pm version 1.25
ok 1
ok 2
ok 3

, root-, :
# make install
Installing /usr/local/share/perl/5.10.0/Timestamp/Simple.pm
Installing /usr/local/man/man3/Timestamp::Simple.3pm
Writing /usr/local/lib/perl/5.10.0/auto/Timestamp/Simple/.packlist
Appending installation info to /usr/local/lib/perl/5.10.0/perllocal.pod

,
perldoc.
perldoc . 506.
SYNOPSIS.
, ,
SYNOPSIS Perl:
$ cat times.pl
use Timestamp::Simple qw(stamp);
print stamp, "\n";
$ perl times.pl
20090905182627

Perl.
timestamp :
$ cat fn.pl
use Timestamp::Simple qw(stamp);
#
$ts = stamp, "\n";
#
$ts =~ s/.(.*)/\1/;

546 11. Perl


#
$fn = "myfile." . $ts;
# ,
open (OUTFILE, '>', "$fn");
print OUTFILE ".\n";
close (OUTFILE);
$ perl fn.pl
$ ls myf*
myfile.0905183010

substr . , $ts =~, . substr


, $ts, 4 :
substr

$ts = substr ($ts, 4);

Perl.
,
Perl.
Linux

, , .
, ,
. %ENV Perl
,
Perl
. , .
$user . shift
(. shift, push, pop splice . 518)
, .
, $user. ,
shift , Perl ,
(||). , USER
%ENV, .
@list.
, , . $fh.
(|) __ (.
. 530) Perl
,
. grep, /etc/group , ,

547

$user. Perl , die .


$ cat groupfind.pl
$user = shift || $ENV{"USER"};
say " $user :";
@list = ();
open (my $fh, "grep $user /etc/group |") or die ": $!\n";
while ($group = <$fh>) {
chomp $group;
$group =~ s/(.*?):.*/$1/;
push @list, $group;
}
close $fh;
@slist = sort @list;
say "@slist";
$ perl groupfind.pl
:
adm admin audio cdrom dialout dip floppy kvm lpadmin

while groupfind.pl
grep , grep.
, /etc/group, ,
, , . :
sam:x:1000:max,zach,helen


$group =~ s/(.*?):.*/$1/;

,
. .*: , ; ,
(. . 541).
, ,
, Perl ,
, . .*
. Perl $1 , ( , ), ( ) $group.
chomp ( ). push $group @list. chomp,
.
while , grep, sort @list
@slist. ,
.

548 11. Perl


opendir readdir.
opendir , open . :
. readdir .
opendir ( .), $dir.
opendir , Perl

, or: die
. while , ,
readdir, $entry. if print , (d).
print , . .. .
readdir , false,
, while.
closedir , print ,
, .
opendir
readdir

$ cat dirs2a.pl
#!/usr/bin/perl
print " :\n";
opendir my $dir, '.' or die " : $!\n";
while (my $entry = readdir $dir) {
if (-d $entry) {
print $entry, ' ' unless ($entry eq '.' || $entry eq '..');
}
}
closedir $dir;
print "\n";
$ ./dirs2a.pl
:
two one

split ,
. split
split (/_/, );
_ (
), .
, split .
Linux
UID,
/etc/passwd, 100. passwd
$user while, , , split. ,
@row, @row.
split

549

if true, (

UID
) , 100. >=, ;
ge.
print UID-
, $sortout. open
,
sort n. sort (. sort:
. 74) ,
, split3.pl ,
$sortout, passwd.
$ cat split3.pl
#!/usr/bin/perl -w
open ($pass, "/etc/passwd");
open ($sortout, "| sort -n");
while ($user = <$pass>) {
@row = split (/:/, $user);
if ($row[2] >= 100) {
print $sortout "$row[2] $row[0]\n";
}
}
close ($pass);
close ($sortout);
$ ./split3.pl
100 libuuid
101 syslog
102 klog
103 avahi-autoipd
104 pulse

@ARGV (. @ARGV . 532)


, .
foreach @ARGV,
. ++
$count .
$ cat 10.pl
#!/usr/bin/perl -w
$count = 0;
$num = @ARGV;
print " : $num, :\n";
foreach $arg (@ARGV) {
print ++$count, ". $arg\n";
}
$ ./10.pl apple pear banana watermelon

550 11. Perl


: 4, :
1. apple
2. pear
3. banana
4. watermelon

Perl (Larry Wall) 1987.


Perl , ,
, ,
, .
Perl
,
CPAN.
perldoc Perl.
, Perl,
pod- (plain old documentation ),
.
Perl : ( ,
$), ( ,
@) ( ;
, %).
, .

Perl
. , Perl

, , (
).
Perl. ,
, Perl

, .

1. Perl ?
2. ?
3. , ?
1) IP- .
2) , , .
4. , ,
:
He said, "Go get me the wrench," but I didn't hear him.

551
5. , IP- .
6. , . ,
.


1. , *~ *.ico . (.
File::Find.)
2. , , Perl.
3. Perl, , ,
.
4. , .
5. , .ico,
, icons, .
(. File::Find File::Copy.)
6. , Apache.
, .
. , .
Apache. ,
HTTP/1.1, . 200 ,
. (), ,
.
__DATA__
92.50.103.52 - - [19/Aug/2008:08:26:43 -0400] "GET /perl/automated-testing/next_active.gif
HTTP/1.1" 200 980 "http://example.com/perl/automated-testing/navigation_bar.htm"
"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/3.0.0.6
(Ubuntu-feisty); Blazer/4.0"

12.
AWK
AWK ,
( ),
, . ,
.
, AWK , :
,
.
AWK .
, . AWK
. AWK ( ,
Alfred V. Aho, Peter J. Weinberger, Brian W. Kernighan)
.
.
AWK
C

:
.
.
.
.
.
.
.
, C, printf.
( gawk).
( gawk).

gawk :
gawk [] [] [_]
gawk [] f _ [_]
gawk , , . getline
, gawk (.

553

getline: . 559).
(coprocess) gawk (. : - . 581; awk
mawk ). gawk .

gawk, . _ ,
gawk.
gawk-, _.
gawk ,
. ,
. _
, gawk. .
_ , gawk ,
getline (. getline: . 559),
(. C: - . 581).

AWK
AWK

UNIX

awk

.

Linux
gawk

(
GNU
- awk

) mawk

(
, awk). Mac OS X awk.
gawk. ,
,
awk

mawk
, ,
(. : -
. 581). gawk Linux.
Mac OS X - gawk.darwinports.
com. gawk , GNU EXTENSIONS
man- gawk info-, .

, (),
gawk. awk mawk .
field-separator fs
F fs
fs (field separator)
(. FS .12.2).
--file _
f _
gawk- _,
. .
- -file.

554 12.

AWK

help W help

gawk ( gawk).
lint W lint

gawk ( gawk).
posix W posix

POSIX- gawk. , man-, gawk (


gawk).
traditional W traditional

gawk- GNU,
, UNIX awk ( gawk).
assign =
v =
. gawk- BEGIN (. BEGIN
END . 555). .

, AWK.
Linux

/bin/awk /bin/gawk /bin/mawk.


, .


gawk (, _) , ()
:
{ }
. gawk
, . , ,
gawk .
, gawk .
, gawk .
gawk ( , _ ) .
( ), gawk , . , gawk . gawk ,
.
, .

555

, gawk ,
, . gawk
.

, . ~ ,
(. ~ ( )).
!~ . ,
. 12.1, , .
|| () && () .
~ !~

12.1.

<

<=

==

!=

>=

>

, BEGIN END, ,
gawk , , . gawk ,
BEGIN, , END.
BEGIN END . 555.
BEGIN END

.
gawk , gawk ,
, . ,
gawk, , . , gawk
. gawk
, ,
. ,
, ( ) . 565.
, ()

gawk, gawk
. , gawk
. print (
{print}). ( , ; . . 557) .
print , gawk .
. print

556 12.

AWK

( gawk- >; .
. 569), (>>)
(| ). (|& )
, ,
( gawk; . :
- . 581).
print , gawk . gawk ,
(OFS; , ; . .12.2).
,
.

gawk ,
(#). , gawk-.

gawk ,
. 0; .
, gawk
. ( ) gawk-, .
.12.2.
12.2.

$0

( )

$1$n

FILENAME

( )

FS

( TAB; . FS . 570)

NF

NR

(. NR ( ) . 567)

OFS

( ; . OFS
. 568)

ORS

( )

RS

( )


assign (v).
, gawk .

557


. gawk
.
,
. ,
, assign
(v).

.12.3 , gawk
.
12.3.

length(str)

str;
(. length . 566)

int(num)

num

index(str1,str2)

str2 str1, 0, str2


str1

split(str,arr,del)

str, del, arr [1]arr [n];

sprintf(fmt,args)

args fmt ;
C

substr(str,pos,len) str , pos -

len

tolower(str)

str,

toupper(str)

str,


gawk, .12.4,
C.
12.4.

**

, , , ,

, ,
,

558 12.

AWK

12.4 ()

, , ,

, , ,

, ,
,

, , ,

, , ,

++

+=

, , , , ,

, , , , ,

*=

, , , , ,

/=

, , , , ,

%=

,
, , .
,


gawk.
.
.
Perl
(. , . 519).
:
[] =
,
, , , .
,
:
for ( in )
,
, ,

559

, , gawk
. .
, ,
. 571 .

printf
, gawk, print
printf. printf, gawk, C. printf :
printf _,1, 2, , n
_ , printf 1, 2, , n.
. _ \n
\t TAB. _
, .
:
%[][x[.y]]conv
() printf , x
, .y
. conv
, .12.5. , printf,
printf . 569.
12.5.
conv

f e, ,


( ) gawk-. ifelse, while for. break
continue,
.
. 417.
.

560 12.

AWK

ifelse
ifelse , , .
ifelse else .
if ()
{}
[else
{}]
, if :
if ($5 <= 5000) print $0

gawk- ifelse. .
$ cat if1
BEGIN {
nam="sam"
if (nam == "max")
print "nam is max"
else
print "nam is not max, it is", nam
}
$ gawk -f if1
nam is not max, it is sam

while
while ,
true. while :
while ()
{}
gawk- while . ,
while . ,
gawk , BEGIN.
$ cat while1
BEGIN{
n = 1
while (n <= 5)
{
print "2^" n, 2**n
n++
}
}
$ gawk -f while1
2^1 2
2^2 4

561
2^3 8
2^4 16
2^5 32

for
for :
for (; ; )
{}
for ,
0 1. , true.
. Gawk- for1
, while1, ,
for, :
$ cat for1
BEGIN
{
for (n=1; n <= 5; n++)
print "2^" n, 2**n
}
$ gawk -f for1
2^1 2
2^2 4
2^3 8
2^4 16
2^5 32

gawk for
:
for ( in )
{}
for
,
.
( for

. 571) for:
END {for (name in manuf) print name, manuf[name]}

break
break for while,
, .

continue
continue for while,
, , , .

562 12.

AWK

cars.
,
- , , , . ( ).

cars

$ cat cars
plym
fury
chevy
malibu
ford
mustang
volvo
s80
ford
thundbd
chevy
malibu
bmw
325i
honda
accord
ford
taurus
toyota rav4
chevy
impala
ford
explor

1970
1999
1965
1998
2003
2000
1985
2001
2004
2002
1985
2003

73
60
45
102
15
50
115
30
10
180
85
25

2500
3000
10000
9850
10500
3500
450
6000
17000
750
1550
9500

gawk- :
{ print }

, .
, gawk . print .
.
$ gawk '{ print
plym
fury
chevy
malibu
ford
mustang
volvo
s80

}' cars
1970
1999
1965
1998

73
60
45
102

2500
3000
10000
9850

, .
, chevy .

/chevy/

gawk ,
chevy. , gawk ,
print.
, chevy:
$ gawk '/chevy/' cars
chevy
malibu 1999
chevy
malibu 2000
chevy
impala 1985

60
50
85

3000
3500
1550

gawk, ,
, -

563

. gawk- , ,
.

( ) .
-
. , , gawk .
($3), ( , ) ($1) :

$ gawk '{print $3, $1}' cars


1970 plym
1999 chevy
1965 ford
1998 volvo

, ,
, chevy,
:
$ gawk '/chevy/ {print $3, $1}' cars
1999 chevy
2000 chevy
1985 chevy

gawk ,
h. , gawk
.
$ gawk '/h/' cars
chevy
malibu 1999
ford
thundbd 2003
chevy
malibu 2000
honda
accord 2001
chevy
impala 1985
~ (
)

60
15
50
30
85

3000
10500
3500
6000
1550

,
h, (~):

$ gawk '$1 ~ /h/' cars


chevy
malibu 1999
chevy
malibu 2000
honda
accord 2001
chevy
impala 1985

60
50
30
85

3000
3500
6000
1550

(^)
, , :
$ gawk '$1 ~ /^h/' cars
honda
accord 2001
30

6000

.
gawk , ,

564 12.

AWK

t m, ,
. "$" $5 , gawk
.
$ gawk '$2 ~ /^[tm]/ {print $3, $2, "$"
1999 malibu $3000
1965 mustang $10000
2003 thundbd $10500
2000 malibu $3500
2004 taurus $17000

$5}' cars

,
gawk-. -, ,
, . -,
(5$). -,
.

$ gawk '$3 ~ /5$/ {print $3, $1, "$"


1965 ford $10000
1985 bmw $450
1985 chevy $1550

$5}' cars

(==) gawk 1985. gawk


(print) , true.
$ gawk '$3 == 1985' cars
bmw
325i
1985
115
chevy
impala 1985
85

450
1550

$3000
.
$ gawk '$5 <= 3000' cars
plym
fury
1970
73
chevy
malibu 1999
60
bmw
325i
1985
115
toyota rav4
2002
180
chevy
impala 1985
85

2500
3000
450
750
1550

gawk ,
ASCII- . gawk , 450 750 , 2000 9000,
.

$ gawk '"2000" <= $5 && $5 < "9000"' cars


plym
fury
1970
73
2500
chevy
malibu 1999
60
3000
chevy
malibu 2000
50
3500
bmw
325i
1985
115
450
honda
accord 2001
30
6000
toyota rav4
2002
180
750

565

, .
. , , .
$ gawk '2000 <=
plym
fury
chevy
malibu
chevy
malibu
honda
accord

$5 && $5 < 9000' cars


1970
73
2500
1999
60
3000
2000
50
3500
2001
30
6000

(,) .
, .
, . ,
, , gawk
. , , volvo ,
, bmw.
, (
)

$ gawk '/volvo/ , /bmw/' cars


volvo
s80
1998
102
ford
thundbd 2003
15
chevy
malibu 2000
50
bmw
325i
1985
115

9850
10500
3500
450

, , .
gawk , chevy ford.
ford, gawk ,
chevy.
$ gawk '/chevy/ , /ford/' cars
chevy
malibu 1999
60
ford
mustang 1965
45
chevy
malibu 2000
50
bmw
325i
1985
115
honda
accord 2001
30
ford
taurus 2004
10
chevy
impala 1985
85
ford
explor 2003
25

3000
10000
3500
450
6000
17000
1550
9500

gawk-
. f (file), ,
gawk-.
file

gawk-, pr_
header, BEGIN. gawk
, BEGIN, :
.
, {print}, .
BEGIN

$ cat pr_header
BEGIN
{print ".
{print}

"}

566 12.

AWK

$ gawk -f pr_header cars


.

plym
fury
1970
73
chevy
malibu 1999
60
ford
mustang 1965
45
volvo
s80
1998
102

2500
3000
10000
9850

, BEGIN, . ,
.
$ cat pr_header2
BEGIN
{
print " .
"
print "----------------------------------------"
}
{print}
$ gawk -f pr_header2 cars
.

---------------------------------------plym
fury
1970
73
2500
chevy
malibu 1999
60
3000
ford
mustang 1965
45
10000
volvo
s80
1998
102
9850

length
, . $0 . gawk
, gawk sort
( n ; . sort V).
cars .

length

$ gawk '{print length, $0}' cars | sort


21 bmw 325i
1985
115
450
22 plym fury
1970
73
2500
23 volvo
s80
1998
102
24 ford explor 2003
25
9500
24 toyota
rav4
2002
180
25 chevy
impala 1985
85
25 chevy
malibu 1999
60
25 chevy
malibu 2000
50
25 ford taurus 2004
10
17000
25 honda
accord 2001
30
26 ford mustang 1965
45
10000
26 ford thundbd 2003
15
10500

-n
9850
750
1550
3000
3500
6000

.
; .

567

NR (), . ,
24. .
NR (
)

$ gawk 'length > 24 {print NR}' cars


2
3
5
6
8
9
11

, (,) NR .
:
$ gawk 'NR == 2
chevy
malibu
ford
mustang
volvo
s80

, NR == 4' cars
1999
60
3000
1965
45
10000
1998
102
9850

END , BEGIN, ,
gawk , ,
. ,
. NR gawk , ,
END.
END

$ gawk 'END {print NR, " ." }' cars


12 .

, ,
, if. gawk
, ( ) . gawk
. .
$ cat separ_demo
{
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
print
}
$ gawk -f separ_demo cars
plymouth fury 1970 73 2500
chevrolet malibu 1999 60 3000
ford
mustang 1965
45
volvo
s80
1998
102
ford
thundbd 2003
15
chevrolet malibu 2000 50 3500
bmw
325i
1985
115
honda
accord 2001
30
ford
taurus 2004
10

10000
9850
10500
450
6000
17000

568 12.

AWK

toyota rav4
2002
180
chevrolet impala 1985 85 1550
ford
explor 2003
25

750
9500

gawk f , , gawk
, .
, , . #!/
bin/gawk f (. #! . 294) gawk. ,
, (. chmod:
. 292).

$ chmod u+rx separ_demo2


$ cat separ_demo2
#!/bin/gawk -f
{
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
print
}
$ ./separ_demo2 cars
plymouth fury 1970 73 2500
chevrolet malibu 1999 60 3000
ford
mustang 1965
45

10000

,
OFS. OFS , escape-
\t. , .

OFS

$ cat ofs_demo
BEGIN {OFS = "\t"}
{
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
print
}
$ gawk -f ofs_demo cars
plymouth
fury
chevrolet
malibu
ford
mustang 1965
volvo
s80
1998
ford
thundbd 2003
chevrolet
malibu
bmw
325i
1985
honda
accord 2001
ford
taurus 2004

1970
1999
45
102
15
2000
115
30
10

73
60
10000
9850
10500
50
450
6000
17000

2500
3000

3500

569
toyota rav4
chevrolet
ford
explor

2002
impala
2003

180
1985
25

750
85
9500

1550

printf .

.

, .
printf

$ cat printf_demo
BEGIN {
print "
"
print ".

( 1000) "
print \
"--------------------------------------------------"
}
{
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
printf "%-10s %-8s
%2d
%5d
$ %8.2f\n",\
$1, $2, $3, $4, $5
}
$ gawk -f printf_demo cars

( 1000)
-------------------------------------------------plymouth
fury
1970
73
$ 2500.00
chevrolet malibu
1999
60
$ 3000.00
ford
mustang
1965
45
$ 10000.00
volvo
s80
1998
102
$ 9850.00
ford
thundbd
2003
15
$ 10500.00
chevrolet malibu
2000
50
$ 3500.00
bmw
325i
1985
115
$
450.00
honda
accord
2001
30
$ 6000.00
ford
taurus
2004
10
$ 17000.00
toyota
rav4
2002
180
$
750.00
chevrolet impala
1985
85
$ 1550.00
ford
explor
2003
25
$ 9500.00

: , chevy, , ford.

$ cat redirect_out
/chevy/ {print > "chevfile"}
/ford/ {print > "fordfile"}
END {print "."}
$ gawk -f redirect_out cars
.
$ cat chevfile

570 12.

AWK

chevy
chevy
chevy

60
50
85

malibu
malibu
impala

1999
2000
1985

3000
3500
1550

summary ,
. , ; gawk .
gawk .
$ cat summary
BEGIN {
yearsum = 0 ; costsum = 0
newcostsum = 0 ; newcount = 0
}
{
yearsum += $3
costsum += $5
}
$3 > 2000 {newcostsum += $5 ; newcount ++}
END {
printf " ( ) %4.1f \n",\
2010 - (yearsum/NR)
printf " $%7.2f\n",\
costsum/NR
printf " $%7.2f\n",\
newcostsum/newcount
}
$ gawk -f summary cars
( ) 17.1
$6216.67
$8750.00

gawk passwd Linux, :


$ awk '/mark/ {print}' /etc/passwd
mark:x:107:100:ext 112:/home/mark:/bin/tcsh


. passwd Linux, (:),

(FS). passwd ID- ( 3). ,
passwd .
($3 > saveit) gawk , ID

- ID-. gawk
ID- saveit. gawk saveit ID- .
, gawk saveit .

FS

571
$ cat find_uid
BEGIN
$3 > saveit
END

{FS = ":"
saveit = 0}
{saveit = $3}
{print " UID - " saveit + 1}

$ gawk -f find_uid /etc/passwd


UID - 1092

cars. ifelse
price. ,
.
$ cat price_range
{
if
($5 <= 5000)
$5 = ""
else if
(5000 < $5 && $5 < 10000) $5 = " "
else if
(10000 <= $5)
$5 = ""
#
printf "%-10s %-8s
%2d
%5d
%-12s\n",\
$1, $2, $3, $4, $5
}
$ gawk -f price_range cars
plym
fury
1970
chevy
malibu
1999
ford
mustang
1965
volvo
s80
1998
ford
thundbd
2003
chevy
malibu
2000
bmw
325i
1985
honda
accord
2001
ford
taurus
2004
toyota
rav4
2002
chevy
impala
1985
ford
explor
2003

73
60
45
102
15
50
115
30
10
180
85
25

manuf cars.
manuf[plym], manuf[chevy], manuf[ford] .. (0). ++ , , .

, END, for,

. sort
, . , gawk, ,
manuf.
for

$ cat manuf
gawk ' {manuf[$1]++}

572 12.

AWK

END {for (name in manuf) print name, manuf[name]}


' cars |
sort
$ ./manuf
bmw 1
chevy 3
ford 4
honda 1
plym 1
toyota 1
volvo 1

, manuf.sh, .
,
.
( {count)
gawk- $1 .
$1, , ,
, : ,
. $1
, $1 (. $1$n:
. 460). $1 gawk. (,
) gawk ,
, .
$ cat manuf.sh
if [ $# != 2 ]
then
echo " : manuf.sh "
exit 1
fi
gawk < $2 '
{count[$'$1']++}
END
{for (item in count) printf "%-20s%-20s\n",\
item, count[item]}' |
sort
$ ./manuf.sh
: manuf.sh
$ ./manuf.sh 1 cars
bmw
1
chevy
3
ford
4
honda
1
plym
1
toyota
1

573
volvo

$ ./manuf.sh 3 cars
1965
1
1970
1
1985
2
1998
1
1999
1
2000
1
2001
1
2002
1
2003
2
2004
1

, gawk-,
v gawk .
. manuf2.sh ,
manuf.sh:
$ cat manuf2.sh
if [ $# != 2 ]
then
echo " : manuf2.sh "
exit 1
fi
gawk -v "field=$1" < $2 '
{count[$field]++}
END
{for (item in count) printf "%-20s%-20s\n",\
item, count[item]}' |
sort

word_usage ,
. tr (. V)
. sort ,
.
, , .
$ cat word_usage
tr -cs 'a-zA-Z' '[\n*]' < $1 |
gawk
'
{count[$1]++}
END
{for (item in count) printf "%-15s%3s\n", item, count[item]}' |
sort +1nr +0f -1
$ ./word_usage textfile
the
42
file
29
fsck
27
system
22
you
22
to
21

574 12.
it
SIZE
and
MODE

AWK

17
14
13
13

,
. C,
gawk.
$ cat word_count
tr -cs 'a-zA-Z' '[\n*]' < $1 |
gawk '
{
count[$1]++
}
END
{
for (item in count)
{
if (count[item] > 4)
{
printf "%-15s%3s\n", item, count[item]
}
}
} ' |
sort +1nr +0f -1

tail , , , , :
$ ./word_count textfile | tail
directories
5
if
5
information
5
INODE
5
more
5
no
5
on
5
response
5
this
5
will
5

.
gawk- date.
1 (NR == 1), ,
,
(NR > 1).
$ cat report
if (test $# = 0) then
echo " ."
exit 1
fi
(date; cat $1) |
gawk '
NR == 1 {print " ", $1, $2, $3 ", " $6}

575
NR >

1 {print $5 "\t" $1}'

$ ./report cars
Mon Jan 31, 2010
2500
plym
3000
chevy
10000
ford
9850
volvo
10500
ford
3500
chevy
450
bmw
6000
honda
17000
ford
750
toyota
1550
chevy
9500
ford

, . numbers
, , ,
. next (
), , , . gawk
.
$ cat numbers
10
20
20
30
30
xyz
40
75
50
20
60
30
70
1134.7
80
75
90
176
100
1027.45
110
123
120
75
$ cat tally
gawk ' BEGIN

30.3
45.7
50
107.2
30.3
45.O
50
107.2
30.3
45.7
50
107.2

40.5
66.1
70
55.6
40.5
66.1
70
55.6
40.5
66.1
57a.5
55.6

NR == 1 {
nfields = NF
}

{
ORS = ""
}
#
# nfields
# (NF)

if ($0 ~ /[^0-9. \t]/)


#
{
# ,
print "\n " NR " :\n\t"
# , TAB
print $0 "\n"

576 12.

AWK

next
}

else
{
for (count = 1; count <= nfields; count++)
#
{
printf "%10.2f", $count > "tally.out"
sum[count] += $count
gtotal += $count
}
print "\n" > "tally.out"
}
}
END

{
#
for (count = 1; count <= nfields; count++)
#
{
print "
-------" > "tally.out"
}
print "\n" > "tally.out"
for (count = 1; count <= nfields; count++)
{
printf "%10.2f", sum[count] > "tally.out"
}
print "\n\n
" gtotal "\n" > "tally.out"
} ' < numbers
$ ./tally
3 :
30
xyz
6 :
60
30
11 :
110
123

50

70

45.O

66.1

50

57a.5

$ cat tally.out
10.00
20.00
30.30
20.00
30.00
45.70
40.00
75.00
107.20
50.00
20.00
30.30
70.00
1134.70
50.00
80.00
75.00
107.20
90.00
176.00
30.30
100.00
1027.45
45.70
120.00
75.00
107.20
------------------580.00
2633.15
553.90
4257.55

40.50
66.10
55.60
40.50
70.00
55.60
40.50
66.10
55.60
------490.50

passwd, , , ID-.

577

( Linux pwck.)
Mac OS X passwd Open Directory, OSX
.
$ cat /etc/passwd
bill::102:100:ext 123:/home/bill:/bin/bash
roy:x:104:100:ext 475:/home/roy:/bin/bash
tom:x:105:100:ext 476:/home/tom:/bin/bash
lynn:x:166:100:ext 500:/home/lynn:/bin/bash
mark:x:107:100:ext 112:/home/mark:/bin/bash
sales:x:108:100:ext 102:/m/market:/bin/bash
anne:x:109:100:ext 355:/home/anne:/bin/bash
toni::164:100:ext 357:/home/toni:/bin/bash
ginny:x:115:100:ext 109:/home/ginny:/bin/bash
chuck:x:116:100:ext 146:/home/chuck:/bin/bash
neil:x:164:100:ext 159:/home/neil:/bin/bash
rmi:x:118:100:ext 178:/home/rmi:/bin/bash
vern:x:119:100:ext 201:/home/vern:/bin/bash
bob:x:120:100:ext 227:/home/bob:/bin/bash
janet:x:122:100:ext 229:/home/janet:/bin/bash
maggie:x:124:100:ext 244:/home/maggie:/bin/bash
dan::126:100::/home/dan:/bin/bash
dave:x:108:100:ext 427:/home/dave:/bin/bash
mary:x:129:100:ext 303:/home/mary:/bin/bash
$ cat passwd_check
gawk < /etc/passwd '
BEGIN
{
uid[void] = ""
# gawk, uid -
}
{
# ;
dup = 0
#
split($0, field, ":")
# , ":"
if (field[2] == "")
#
{
if (field[5] == "")
# info
{
print field[1] " ."
}
else
{
print field[1] " ("field[5]") ."
}
}
for (name in uid)
# uid
{
if (uid[name] == field[3])
# UID
{
print field[1] " UID, " name " : UID = " uid[name]
dup = 1
#
}
}

578 12.

AWK

if (!dup)

# , if (dup == 0)
# UID
# uid

{
uid[field[1]] = field[3]
}
}'
$ ./passwd_check
bill (ext 123) .
toni (ext 357) .
neil UID, toni : UID = 164
dan .
dave UID, sales : UID = 108

, gawk cars :
$ cat list_cars
trap 'rm -f $$.tem > /dev/null;echo $0 aborted.;exit 1' 1 2 15
echo -n " (, 5000 7500):"
read lowrange hirange
echo '

( 1000)

--------------------------------------------------' > $$.tem


gawk < cars '
$5 >= '$lowrange' && $5 <= '$hirange' {
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
printf "%-10s %-8s
%2d
%5d
$ %8.2f\n", $1, $2, $3, $4,
$5
}' | sort -n +5 >> $$.tem
cat $$.tem
rm $$.tem
$ ./list_cars
(, 5000 7500):3000 8000

( 1000)

-------------------------------------------------chevrolet malibu
1999
60
$ 3000.00
chevrolet malibu
2000
50
$ 3500.00
honda
accord
2001
30
$ 6000.00
$ ./list_cars
(, 5000 7500):0 2000

( 1000)

-------------------------------------------------bmw
325i
1985
115
$
450.00
toyota
rav4
2002
180
$
750.00

579
chevrolet

impala

1985

85

1550.00

$ ./list_cars
(, 5000 7500):15000 100000

( 1000)

-------------------------------------------------ford
taurus
2004
10
$ 17000.00

gawk
AWK.
getline,
gawk ,
, .
gawk, awk mawk .
getline:

getline ,
gawk, ,
. getline , . BEGIN g1
getline aa:
$ cat g1
BEGIN
{
getline aa
print aa
}
$ echo aaaa | gawk -f g1
aaaa

alpha:
$ cat alpha
aaaaaaaaa
bbbbbbbbb
ccccccccc
ddddddddd

g1 ,
:
$ gawk -f g1 < alpha
aaaaaaaaa

getline , $0
($1, $2, ):
$ gawk BEGIN {getline;print $1} < alpha
aaaaaaaaa
g2 BEGIN while. getline holdme

.
$ cat g2
BEGIN
{
while (getline holdme)
print holdme
}

580 12.

AWK

$ gawk -f g2 < alpha


aaaaaaaaa
bbbbbbbbb
ccccccccc
ddddddddd

g3 gawk
$0, gawk- (
BEGIN). (NR), $0: $0 (
) .
$ cat g3

{print NR, "$0:", $0}

$
1
2
3
4

gawk -f g3 < alpha


$0: aaaaaaaaa
$0: bbbbbbbbb
$0: ccccccccc
$0: ddddddddd

, g4, , getline gawk $0.


getline , $0, -
($1, $2, ). g4, g3, , gawk.
getline aa.
, aa: aa. g4
, getline , gawk.
$ cat g4

{
print NR, "$0:", $0
getline aa
print NR, "aa:", aa
}

$
1
2
3
4

gawk -f g4 < alpha


$0: aaaaaaaaa
aa: bbbbbbbbb
$0: ccccccccc
aa: ddddddddd

g5 , ,
b. print , gawk
. /^b/ , b, .
getline hold,
:, hold, $1. $1 ,
gawk , , getline.
NR, .
getline $0, , gawk
NR .
$ cat g5

# getline
{print " ", NR, $0}
# "b",

581

/^b/

{
# getline hold
getline hold
# hold
print " :", hold
# getline , $0
# $1
print " :", $1
}
{
print ">>>> ", NR
print ""
}

$ gawk -f g5 < alpha


1 aaaaaaaaa
>>>> 1
2 bbbbbbbbb
: ccccccccc
: bbbbbbbbb
>>>> 3
4 ddddddddd
>>>> # 4
: -

, . 3.1, gawk
.
- , -
SQL . gawk ,
, |&.

GAWK
awk mawk . gawk.

(
) ,
, .
, gawk-
, .
tr
. to_upper tr, ; .
to_upper ,
, . , to_upper
, # set x.

to_upper

$ cat to_upper
#!/bin/bash
#set -x
while read arg
do
echo "$arg" | tr '[a-z]' '[A-Z]'
done

582 12.

AWK

$ echo abcdef | ./to_upper


ABCDEF

g6 to_upper . gawk-
, ,
.
$ cat g6

{
print $0 |& "to_upper"
"to_upper" |& getline hold
print hold
}

$ gawk -f g6 < alpha


AAAAAAAAA
BBBBBBBBB
CCCCCCCCC
DDDDDDDDD

g6 , ,
. , gawk .
, print $0, . |& to_upper,
. . to_upper getline, hold. , print hold,
hold .

, gawk
IP-. ,
/inet/, gawk , .
:

/inet//_/_/_
tcp, udp,
_ 0 (), ,
gawk ( ), _ IP _
. _ _
, http ftp.
g7 rfc-retrieval.txt , www.rfc-editor.
org. www.rfc-editor.org /rfc/rfc-retrieval.txt. g7 server.
TCP-,
www.rfc-editor.org 80.
HTTP- 80 http.

GET- .
, gawk. while
getline.
,
$0. print .

583

, print
gawk, .
$ cat g7
BEGIN

{
# server
#
server = "/inet/tcp/0/www.rfc-editor.org/80"
# GET-
print "GET /rfc/rfc-retrieval.txt" |& server
# while
# getline
while (server |& getline)
print $0
}

$ gawk -f g7

Where and how to get new RFCs


=============================
RFCs may be obtained via FTP or HTTP or email from many RFC repositories.
The official repository for RFCs is:
http://www.rfc-editor.org/

AWK
, (
), .
, , , ,

. AWK ,
awk, gawk mawk.
AWK- ,
() , :
{}
. AWK-
, .
, AWK . , AWK .
AWK- , ,
, , printf,
C. AWK
getline, ( gawk), gawk

584 12.

AWK

,
, ( gawk).

1. AWK

-, , , .
2. AWK-, , , .
3. AWK-, cars (.
cars continue . 562), ,
$5000, .
4. AWK , /usr/share/dict/words
abul. grep.


1. pgawk ( gawk). ?
?
2. gawk- ( awk mawk) net_list, rfc-retrieval.txt www.rfc-editor.org (.
. 582) , .
3. net_list, 6,
to_upper (. to_upper
. 581) ,
. , cars.
4. gawk ( awk mawk) ,
gawk- ? (.
man-, gawk.)

13. sed
sed (
stream
editor

) () . ,
.
. ,
sed , ed. Linux
GNU sed, Mac OS X BSD sed. .

sed :
sed [n] [_]
sed [n] f _ [_]
sed , ,
. sed .

sed, . , sed-
sed-. _

, sed- (.
. 586). _ ,
sed; , . _ ,
sed .

, (),
Linux ( GNU sed). , ,
, Linux (GNU sed), OS X (BSD sed).
file _
f _
sed _,
.
.
help

586 13. sed


sed. (Linux)
in-place[=]
i[]
. sed . sed
. sed
.
. , , .
quiet n
silent

sed ,
, Print (p) .


sed , :
[ [,]] [_]
. , sed
.
, . (), ,
. _ . sed, (;).
sed :
1. _ .
2. _.
( ),
.
3. _.
( ), (, ) .
4. 3 ,
_.
5. 1, ; , sed .

587

, . ,
, $, .
, , .
, sed
, .
, , , ( , ). , sed ,
.
, .
.
.
, ;
. ,
. sed
, .
. sed
, .

sed .
, ,
sed. , , .

(append ). .
, , .
, . :
[ [,]] a\
\
\

, , , .
,
. sed , , n . ,
, , .
a

(change ).
, , ,
c

588 13. sed


. ,
.
(delete ). sed
. sed
, , , _.
d

(insert ).
, , .
i

(next, , ).
(N) .
.
N . . (N) . 595.
N

n
(next ). (n)
, ,
, _.

(print ). , ,
. n.
p

(quit ). sed .
q

(read ). .
.
r

(substitute ). sed
vim (.
. 196). :
[[,]] s//_ /[g][p][w ]
, (/),
, .
_

. () . _ (&),
sed , . g,
.
g (global )
,
.
s

589

p (print ) sed , , .
n.
w (write ) p,
. w .
(write ). ,
, . .
w


(). sed , , , .
, 3!d , 3, $!p ,
.
!

( ).
, ,
. , , (;).
{}

info-, GNU sed,


Commands for sed gurus ( sed-)

awk Perl.

:
sed-. b t.
b []
.
.

,
,
( ).
.
t []


, ,
, sed.
, ; .
, . ,
.

590 13. sed


.
.
g


.
G

.
.
h


.
H


lines

lines:

$ cat lines
Line one.
The second line.
The third.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.

.
.
.
.
.
.
.
.

, sed .
n, sed ,
, (p).
,
line ( ). , n ,
sed . sed ,
line, .
$ sed '/line/ p' lines
Line one.
The second line.
The second line.
The third.
This is line four.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
This is line seven.
Eighth and last.

/line/, , . sed ,

591

. (p)
.
n, sed :
$ sed -n '/line/ p' lines
The second line.
This is line four.
This is line seven.

sed
. 3 6.
$ sed -n '3,6 p' lines
The third.
This is line four.
Five.
This is the sixth sentence.

, sed . sed
, , head 5.
$ sed '5 q' lines
Line one.
The second line.
The third.
This is line four.
Five.

sed ,
_. print3_6
, . f sed , .
_

$ cat print3_6
3,6 p
$ sed -n -f print3_6 lines
The third.
This is line four.
Five.
This is the sixth sentence.

2
, AFTER.
. n, sed lines.

$ cat append_demo
2 a\
AFTER.
$ sed -f append_demo lines
Line one.
The second line.

592 13. sed


AFTER.
The third.
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.

insert_demo ,
This, BEFORE. .

$ cat insert_demo
/This/ i\
BEFORE.
$ sed -f insert_demo lines
Line one.
The second line.
The third.
BEFORE.
This is line four.
Five.
BEFORE.
This is the sixth sentence.
BEFORE.
This is line seven.
Eighth and last.

.
, ,
.

$ cat change_demo
2,4 c\
SED WILL INSERT THESE\
THREE LINES IN PLACE\
OF THE SELECTED LINES.

SED \
\
.

$ sed -f change_demo lines


Line one.
SED WILL INSERT THESE
THREE LINES IN PLACE
OF THE SELECTED LINES.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.

.
sed , . subs_demo

593
line sentence. p ,
. sed n, sed -

, .
$ cat subs_demo
s/line/sentence/p
$ sed -n -f subs_demo lines
The second sentence.
This is sentence four.
This is sentence seven.

, w (temp) , sed
temp. n, , , temp. cat
temp. Line ( L)
.
$ cat write_demo1
s/line/sentence/w temp
$ sed -f write_demo1 lines
Line one.
The second sentence.
The third.
This is sentence four.
Five.
This is the sixth sentence.
This is sentence seven.
Eighth and last.
$ cat temp
The second sentence.
This is sentence four.
This is sentence seven.

bash
REPORT, report, FILE file, PROCESS process.
, sed, sub (. chmod:
. 292). for (.
for . 430) ,
. ,
sed, . sed-, .
( ), sed ( ). g (global ),
.
$ cat sub
for file

594 13. sed


do
echo $file
mv $file $$.subhld
sed 's/REPORT/report/g
s/FILE/file/g
s/PROCESS/process/g' $$.subhld > $file
done
rm $$.subhld
$ sub file1 file2 file3
file1
file2
file3


(temp2). 2 4, , , sed. .
$ cat write_demo2
2,4 w temp2
$ sed -n -f write_demo2 lines
$ cat temp2
The second line.
The third.
This is line four.

write_demo3 write_demo2,
(!), sed ,
.
$ cat write_demo3
2,4 !w temp3
$ sed -n -f write_demo3 lines
$ cat temp3
Line one.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.

(n).
( 3) sed
, 3.

(n)

$ cat next_demo1
3 n
p
$ sed -n -f next_demo1 lines
Line one.
The second line.

595
This is line four.
Five.
This is the sixth sentence.
This is line seven.
Eighth and last.

. the , (n) sed .


$ cat next_demo2
/the/ n
p
$ sed -n -f next_demo2 lines
Line one.
The second line.
The third.
This is line four.
Five.
This is line seven.
Eighth and last.

, ,
(n) (N). (N) ,
the. lines sed 7 6,
.
. ,
, ,
.

(N)

$ cat Next_demo3
/the/ N
s/\n/ /
p
$ sed -n -f Next_demo3 lines
Line one.
The second line.
The third.
This is line four.
Five.
This is the sixth sentence. This is line seven.
Eighth and last.

sed compound.in.
$ cat compound.in
1. The words on this
2. The words on this
3. The words on this
4. The words on this

page
page
page
page

596 13. sed


words
text 13 text TEXT 24.
3. text
1, TEXT 2, 3 words 4.
sed 2 3: text words, TEXT
text. sed 3.
$ cat compound
1,3 s/words/text/
2,4 s/text/TEXT/
3 d
$ sed -f compound compound.in
1. The text on this page
2. The TEXT on this page
4. The words on this page

sed- .
,

.
$ cat compound2
2,4 s/text/TEXT/
1,3 s/words/text/
3 d
$ sed -f compound2 compound.in
1. The text on this page
2. The text on this page
4. The words on this page

compound3 . sed ,
n. 3
.
$ cat compound3
2 a\
This is line 2a.\
This is line 2b.
3 p

2a
2b

$ sed -f compound3 compound.in


1. The words on this page
2. The words on this page
This is line 2a.
This is line 2b.
3. The words on this page
3. The words on this page
4. The words on this page

, sed .
2, -

597

, 2. , n.
$ cat compound4
2 a\
This is line 2a.\
This is line 2b.
2 d
$ sed -f compound4 compound.in
1. The words on this page
This is line 2a.
This is line 2b.
3. The words on this page
4. The words on this page

.
(^.) . (
) -,
(\t), (&).
, .
$ sed 's/^./\t&/' lines
Line one.
The second line.
The third.

,
.

s/^/\t/. ,
,
, .
sed- ( )
, , . ind
chmod.
$ cat ind
sed 's/^./\t&/' $*
$ chmod u+rx ind
$ ind lines
Line one.
The second line.
The third.

:
, , , sed.
, , ,

598 13. sed


#!/bin/sed f (. #!
. 294), sed.
, .
$ cat ind2
#!/bin/sed -f
s/^./\t&/

sed- ( ,
*$) .
,
, vim.
$ cat cleanup
sed 's/ *$//' $*

cleanup2 sed-, cleanup,


, sed , .
$ cat cleanup2
#!/bin/sed -f
s/ *$//

$
h
n
p
g
p

sed- , .

cat s1
# ( ) .
# .
# .
# .
# ( ).

$ sed -nf s1 lines


The second line.
Line one.
This is line four.
The third.
This is the sixth sentence.
Five.
Eighth and last.
This is line seven.

s1 .
,
, .
, .
sed-
( ).
$ sed 'G' lines
Line one.
The second line.

599
The third.
This is line four.
$

G
. -
, . G
, sed ()
.
s2 , ,
tac.
$ cat s2
2,$G # , ,
# .
h
# .
$!d
# , , .
$ sed -f s2 lines
Eighth and last.
This is line seven.
This is the sixth sentence.
Five.
This is line four.
The third.
The second line.
Line one.

: 2,$G, h $!d.
, , : $
, ! . , , .
1!G:
, , .
s2 :
1. sed (Line one.) .
1) 2,$G .
G .
2) h Line one. .
3) $!d . , sed .
2. sed (The second line.) .
1) 2,$G , (Line one.)
. The second line.
_Line one..
2) h , , .

600 13. sed


3) $!d . , sed
.
3. sed (The third.) .
1) 2,$G , (The second
line._Line one.) .
The third. _The second line. _Line
one..
2) h , , .
3) $!d . , sed .

4. sed () .
1) 2,$G , ,
. lines, .
2) h , , . , , .
3) $!d .
d .
4) sed .

sed (
stream
editor

) () . , ,
. sed ,
.
sed ,
:
[ [,]] [_]
. , sed
. , . (),
, . _ .
, sed , . sed
. sed-
.

601

1. sed-, ,
, Today.
2. sed-, , Today.
3. sed-, ,
(, ).
4. sed- ins, , cat dog
:.
5. sed- div, , first
last.
6. sed-, , , , (0) ,
(09). :
abcabc
abc abc
85c085c
55b55b
0000000

7. sed (
)?

14.
rsync
rsync (
remote

synchronization
)
.

OpenSSH

,
OpenSSH
, , OpenSSH. ,
rsync .
rsyncd, .

rsync :
rsync [] [[@]_:] - [[@]_
:][-]
rsync , ,
.

_ , ,
_ , . , rsync , .
,
; @
. scp rsync
.
- , ,
- .
, .
,
. - ,
recursive archive.
-, .
- , - ; , - (
).

603
(/) -

- , -

rsync
.
/*; rsync
. rsync . .
(/) - . 606.

RSUNC, MAC OS X,
rsync, ( ),
Mac OS X, Linux.

acls A

ACL- (.
(ACL-) . 121).

, , ,
, . devices, specials, group, links, owner, perms, recursive
times. acls, hardlinks
xattrs. , archive .
--archive
(/) - . 606.
archive a

,
. rsync , (~).
rsync , backupdir=dir. . linkdest=dir . 604.
backup b

backup ,
dir. dir
- -.
dir , . dir ,
-.
backupdir=dir

( ). , , -, ,
, . rsync
, , .
copy-unsafelinks

, devices specials.

604 14. rsync


-, -.
(. - . 607).
delete

devices

( ,
root-).

rsync . verbose dry-run


, rsync,
. delete.
dry-run

group g

hard-links
H

( l ; ). , , , , , ,
, -.
links l

rsync , -, -,
dir . dir
, rsync dir -.
, rsync -. . link-dest=dir . 604.
dir , -. dir
, -.
linkdest=dir

owner o

(
root-).

perms p

, -, . .
. 605.
recursive
r

specials

. , rsync , -, -. .
(/) - . 606.
times t

update u

, - .

, rsync.
dryrun. .
(/) - . 606.
verbose v

605
xattrs X

.
rsync.

compress z

rsync .
. man- rsync.
rsync ,
OpenSSH.
OpenSSH-. ssh , rsync
. ssh , rsync . .
. 608. OpenSSH
V.
OpenSSH

(:)
(::), rsync
rsyncd ( , ,
OpenSSH). man- rsync.
rsyncd


man-: rsync

rsync: www.samba.org/rsync
: www.mikerubel.org/computers/rsync_snapshots
: www.rsnapshot.org, backuppc.sourceforge.net
: alliance.seas.upenn.edu/~bcpierce

rsync
recursive verbose. .
recursive
verbose

$ ls -l memos
total 12
-rw-r--r-- 1 max max 1500 Jun
-rw-r--r-- 1 max max 6001 Jun

6 14:24 0606
8 16:16 0608

$ rsync --recursive --verbose memos memos.copy


building file list done

created directory memos.copy
memos.copy
memos/
memos/0606
memos/0608
sent 7671 bytes received 70 bytes 15482.00 bytes/sec

606 14. rsync


total size is 7501

speedup is 0.97

$ ls -l memos.copy
total 4
drwxr-xr-x 2 max max 4096 Jul 20 17:42 memos

rsync memos memos.copy.


, ls, rsync
:
$ ls -l memos.copy/memos
total 12
-rw-r--r-- 1 max max 1500 Jul 20 17:42 0606
-rw-r--r-- 1 max max 6001 Jul 20 17:42 0608

(/) -
,
.
(/) - rsync , /*, , rsync
. - .

memos, times. memos , memos.copy2 ,
.
times

$ rsync --recursive --verbose --times memos/ memos.copy2


building file list done
created directory memos.copy2
./
0606
0608
sent 7665 bytes received 70 bytes 15470.00 bytes/sec
total size is 7501 speedup is 0.97
$
$ ls -l memos.copy2
total 12
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608

archive rsync , ( ,
, ), , , ..
;
hardlinks. archive
. 603. , :
archive

$ rsync --archive --verbose memos/ memos.copy2


$ rsync -av memos/ memos.copy2

607


delete rsync - ,
-.
dryrun verbose , ,
dryrun, rsync .
dry-run verbose delete
, . ,
. archive.
delete
dry-run

$ ls -l memos memos.copy3
memos:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610
memos.copy3:
total 16
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 1200 Jul 21 10:17 notes
$ rsync --archive --verbose --delete --dry-run memos/ memos.copy3
building file list done
deleting notes
notes
./
0610
sent 125 bytes received 32 bytes 314.00 bytes/sec
total size is 13412 speedup is 85.43

rsync , dryrun,
notes. , 0610.
, ,


- (/) ,
delete .
rsync, delete dry-run
verbose.

,
. rsync
( delete ):
$ rsync -avn --delete memos/ memos.copy3

rsync, dryrun.
ls rsync: delete
rsync notes - (memos.copy3), - (memos). , rsync 0610.
$ rsync --archive --verbose --delete memos/ memos.copy3
building file list done

608 14. rsync


deleting notes
./
0610
sent 6076 bytes received 48 bytes 12248.00 bytes/sec
total size is 13412 speedup is 2.19
$ ls -l memos memos.copy3
memos:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610
memos.copy3:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610

.

.
,
. , memos
/backup :
$ rsync --archive --verbose --delete memos/ /backup



OpenSSH- - ,
rsync.
. 605.
.
.
. 4.
Max memos holdfiles coffee.
ssh coffee ls,
rsync. rsync ssh , Max

OpenSSH coffee (.
V
, ssh, : . 834).
$ rsync --archive memos/ coffee:holdfiles
$ ssh coffee 'ls -l holdfiles'
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610

609

-:
$ rsync --archive coffee:holdfiles/ ~/memo.copy4
$ rsync --archive coffee:holdfiles/ /home/max/memo.copy5

holdfiles
Max coffee .
Mac OS X /home /Users.


rsync .
, .
OpenSSH- (
OpenSSH- ssh).
crontab (.
V
,
crontab
. 662),
OpenSSH
( ; .
V
, ssh, : . 834).
rsync- compress
update. compress rsync , . , , ,
, .
update rsync .
,
mirror , . ,
. , ,
(\). , ,
rsync
.
compress
update

$ cat mirror
rsync \
--archive \
--verbose \
--compress \
--update \
--delete \
~/mirrordir/ coffee:mirrordir
$ ./mirror > mirror.out

mirror
irror.out. , ,
m
, verbose . rsync mirror
mirrordir
( ).
coffee. update rsync

610 14. rsync



, .
, , .
delete rsync ,
.


rsync
,
. () ,
;
rsync. , rsync
, ,
.
link-dest=dir rsync
.
() ,
. dir , -.
dir , . . 603.
rsync,
link-dest=dir:
linkdest=dir

$ rsync --archive --link-dest=../backup source/ destination

rsync
source, . source rsync
destination .
destination, rsync -

.
destination, rsync
backup.
yy backup, rsync
backup destination.
yy backup, rsync
source destination.
, ,
rsync link-dest=dir

611

. ,
.
rsync, bkup, rsync
memos bu.0. link-dest=dir rsync , , bu.1.
, rsync , ,
bu.1.
bkup
bu.0, bu.1 bu.2 rsync. bu.2, bu.1
bu.2, bu.0 bu.1. rsync
memos, bu.0 bu.1.
$ cat bkup
rm -rf bu.2
mv bu.1 bu.2
mv bu.0 bu.1
rsync --archive --link-dest=../bu.1 memos/

bu.0

bkup bu.0, bu.1 bu.2 .


f rm bu.2. bkup bu.0 bu.1, mv
No such file or directory (
).
ls bkup memos.
bkup ls memos bu.0,
, memos. rsync
- bu.1,
.
$ ls -l *
-rwxr-xr-x 1 max max

87 Jul 22 11:23 bkup

memos:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610
$ ./bkup
mv: cannot stat 'bu.1': No such file or directory
mv: cannot stat 'bu.0': No such file or directory
$ ls -l *
-rwxr-xr-x 1 max max

87 Jul 22 11:23 bkup

bu.0:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610

612 14. rsync


memos:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610

memos ls , 0610
, newfile :
$ ls -l memos
total 20
-rw-r--r-- 1 max max 2100 Jul 22 14:31 0606
-rw-r--r-- 1 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5251 Jul 22 14:32 newfile

bkup bu.0 ,
memos, bu.1 , bu.0
bkup. 0608 , rsync link-dest=dir , bu.1 bu.0, 2,
ls max.
$ ./bkup
mv: cannot stat 'bu.1': No such file or directory
$ ls -l bu.0
bu.0:
total 20
-rw-r--r-- 1
-rw-r--r-- 2
-rw-r--r-- 1

bu.1
max max 2100 Jul 22 14:31 0606
max max 6001 Jun 8 16:16 0608
max max 5251 Jul 22 14:32 newfile

bu.1:
total 20
-rw-r--r-- 1 max max 1500 Jun 6 14:24 0606
-rw-r--r-- 2 max max 6001 Jun 8 16:16 0608
-rw-r--r-- 1 max max 5911 Jun 10 12:02 0610

, ,
. , ,
, . ,
.
, bkup, ,
. rsync
, ,
crontab (. V).

613

rsync
. openSSH ,
openSSH; , openSSH.
rsync , .

1. rsync.
2. rsync, backmeup
/tmp coffee, , . ,
coffee, ,
.
3. rsync, delete. ?
4. archive? ?)
5. , bkup (.
. 610),
() ?
6. (/) -?

616 V.


, ,
.
(, ),
( ). sample
,
.


aspell

. 622

bzip2

. 630

cat

. 633

cmp

. 647

comm

. 649

cp

. 653

cpio

,
. 657

cut

. 665

dd

. 669

diff

. 674

ditto

, . 683

emacs

. 220

find

. 699

fmt

. 708

grep

. 729

gzip

. 734

head

. 737

less

. 744

ln

. 748

lpr

. 750

ls

. 753

man

. 767

mkdir

. 771

mv

. 778

od

. 782

open

, URL- . 786

otool

, . 788

paste

. 790

pax

,
. 792

plutil

. 798

617
pr

. 799

rm

( ) . 810

rmdir

. 811

sed

. 816

sort

() . 820

split

. 829

strings

. 839

tail

() . 846

touch

()
. 859

uniq

. 868

vim

. 168

wc

, . 872


ftp

. 715

rcp


. 805

rlogin

. 809

rsh

. 812

rsync

. 814

scp


. 814

ssh

. 830

telnet

. 850

,
cd

. 635

chgrp

, . 636

chmod

() . 640

chown

() , . 645

date

. 667

df

. 672

dscl

Directory Service . 686 (OS X)

dmesg

. 685

du

()
. 689

file

. 697

finger

. 706

GetFileInfo

. 727

618 V.
kill

PID . 738

killall

. 740

nice

. 780

nohup

,
. 782

ps

. 801

renice

. 807

SetFile

. 817

sleep

,
. 818

stat

. 838

stty

. 840

sysctl

. 845

top

. 852

umask

, . 867

. 870

which

PATH . 873

who

. 875

,
awk

. 552

configure . 651
gawk

. 722

gcc

C C++ . 723

make

. 761

mawk

. 552


at

. 626

cal

. 632

crontab

crontab . 662

diskutil

, . 680

echo

. 691

expr

. 694

fsck

. 710

launchctl launchd . 741


mkfs

. 772

Mtools

DOS- . 775

619
tee


. 849

test

. 852

tr

. 861

tty

. 864

tune2fs

ext2, ext3 ext4 .864

xargs

. 877


, .V.1.
. , 5K 5210. , 1. ,
, .
V.1.

KB

1000 (10 )

PB

1015

1024(2 )

250

MB

1000 000 (106)

EB

1018

1048 576 (2 )

260

GB

1000000 000 (10 )

ZB

1021

1073741 824 (230)

270

TB

10

YB

1024

280

10

20

12

40

Mac OS X,
BLOCKSIZE . BLOCKSIZE , ,
, K, M G. ,
BLOCKSIZE, .
BLOCKSIZE


GNU- , .V.2.
, , .
V.2.

, , ,

.
, .
, , ,

620 V.
V.2 ()

help

.
. , less, . , ,
ls help | less. , ,
, , grep. ,
d ls: ls help |
grep d.

version

-
sample () ,
. ,
man- (. man: . 53
man ); ,
.

.
man- info- ,
help, .

sample
(
OS

X)
, , Mac OS X.
:
sample []
. ,
. , ([]), . ,
, , . ,
,
. ,
, (, -)
(, -).

, . , ,
.

sample 621

, .
, .
, (. . 138).
, ( ) .
( ),
.
:
.
, . , , -
( , ). , (, delimiter d),
.
delimiter=
dchar
d dchar

. , . (Linux), ,
Linux. , Linux OS X,
. (Linux)
make-dirs
m

t
(). ,
. . ,
, , .
(OS X), , OS X. ,
Linux OS X, . (OS X)

,
.

, , .

, .
, .

622 V.

aspell
.
aspell check [] _
aspell list [] < _
aspell config
aspell help
aspell .
: , , aspell
,
, . aspell
,
. Linux. (Linux)

- ASPELL
, aspell ,
_. aspell

aspell .
aspell . ,
. . .
check c

aspell , . less grep (, aspell config | grep backup).


config

.
less.
help ?

aspell () , , , .
list l

_ , .
aspell check (c).
list (l), .

aspell .
, ,

aspell 623

man-, aspell.
aspell (. config).
,
ASPELL_CONF (~/.aspell.conf).
,
root
-, (/etc/aspell.conf).
, ASPELL_CONF
(;). , ASPELL_CONF, , , ,
.
:
. ( )
( ). ,
dont. , ignorecase
, dontignore-case
.
, , - . , , ignore=4.
ASPELL_CONF
(ignore-case dont-ignore-case).
ASPELL

-, : ,

ASPELL_CONF .
(, ignore-case dontignore
-
case
).
ASPELL
_
CONF

(, ignore-case dont-ignore-case).

_.bak (
, check, backup).

dont-backup

n (
1).
ignore=n

(
dont-ignore-case).

ignore-case

(cc).
LC_MESSAGES (. 8, .8.5).
lang=cc

. mod url (
), none, sgml . : url: URL, ; none: ;
sgml: SGML, HTML, XHTML XML.
mode=mod

stripaccents


( dont-strip-accents).

624 V.

aspell : .
list l.
aspell ,
, .
check c.
aspell , , .
. 625. (. .V.3),
, .
, , , .
V.3. aspell

aspell

i I ( i)

. I ( I
) ; i
,

l ( l

r R

,
. R ;
r

,
aspell

aspell aspell.net /usr/share/doc/aspell.


aspell
. ,
(, red read).
aspell emacs ,
~/.emacs (. .emacs . 223) . emacs
ispell aspell:

emacs

aspell 625
(setq-default ispell-program-name "aspell")

vim

aspell vim,
~/.vimrc (. .vimrc
.204):

map ^T :w!<CR>:!aspell check %<CR>:e! %<CR>

~/.vimrc vim ^T
CONTROL+V CONTROL+T (.
. 189). ~/.vimrc CONTROL+T
aspell vim
.

aspell
memo.txt:
$ cat memo.txt
Here's a document for teh aspell utilitey
to check. It obviosly needs proofing
quiet badly.

aspell check .
teh.
.
:
$ aspell check memo.txt
Here's a document for teh aspell utilitey
to check. It obviosly needs proofing
quiet badly.
============================================================
1) the
6) th
2) Te
7) tea
3) tech
8) tee
4) Th
9) Ted
5) eh
0) tel
i) Ignore
I) Ignore all
r) Replace
R) Replace all
a) Add
l) Add Lower
b) Abort
x) Exit
============================================================
?

; aspell

( ). 1 () teh
the.

626 V.
list
. quiet : ,
.
$ aspell list < memo.txt
teh
aspell
utilitey
obviosly

list.
.
aspell list, seperate temperature aspell ( ).
CONTROL+D ( ), aspell ( ):
$ aspell list
seperate temperature
CONTROL+D
separate

at
.
at [] [ | +]
atq
atrm _
batch [] []
at batch .
, f, . , at batch. ,
,
at batch, .
, at. batch at ,
.
atq , ; atrm , at.

, at .
, , .
, .
:. am pm (
), 12- , at ,
24- . now (),
midnight (), noon () teatime ( , 17:00).

at 627

, .
, at ,
. , at .
.
today () tomorrow ().
, . .
,
( , ): minutes (), hours (),
days () weeks (). at .
.
atrm _ . at
l atq.

batch
OS X. c, d l at,
.
c _

(cat ). , , _.

(delete ). , at. _
,
, .
, l atq,
. at
, atrm. OS X , r.
d _

(file ). , ,
.
, .
f

(list ). at.
at , atq.
l

(mail ).
,
. - , at , ,
.
m

r _

(remove ). , d .
(OSX)

628 V.

at /bin/sh. Linux
bash dash.

at, .
, root-, at
. , at, Linux /etc/at.allow (OS X /var/at/
at.allow) /etc/at.deny (OS X /var/at/at.deny), root- ( 600).
at.deny, , at
. , at.deny, at,
at.allow.
Linux , at, atd. /var/spool/at /var/
spool/cron/atjobs, /var/spool/at/spool /var/spool/
cron/atspool. 700
daemon.
Mac OS X , at, atrun, 30 launchd.
atrun /var/at/jobs, /var/at/spool,
700
daemon.
OS X 10.4 atrun
. atrun
:
at.allow,
at.deny

# launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist


# launchctl unload -w /System/Library/LaunchDaemons/com.apple.atrun.plist

, launchctl.

long_file 2 (2:00AM)
.
; pr_tonight,
at. at
.
$ at 2am
at> pr long_file | lpr
at>CONTROL+D<EOT>
job 8 at 2009-08-17 02:00
$ cat pr_tonight
#!/bin/bash
pr long_file | lpr

at 629
$ at -f pr_tonight 2am
job 9 at 2009-08-17 02:00
$ at 2am < pr_tonight
job 10 at 2009-08-17 02:00

,
, CONTROL+D.

, at ,
job(), , at .
atq, :
$ atq
8
9
10

2009-08-17 02:00 a
2009-08-17 02:00 a
2009-08-17 02:00 a

9:
$ atrm 9
$ atq
8
2009-08-17 02:00 a
10
2009-08-17 02:00 a

cmdfile 15 30 (3:30 PM)


:
$ at -f cmdfile 1530 +1 week
job 12 at 2009-08-23 15:30

at 19:00 (7 PM) (Thursday).


find , , , .
$ at 7pm Thursday
at> find / -name core" -print >report.out 2>report.err
at> lpr report.out
at>CONTROL+D<EOT>
job 13 at 2009-08-18 19:00

,
c , at . ,
:
$ at -c 13
#!/bin/sh
# atrun uid=500 gid=500
# mail
sam 0
umask 2
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.;
export PATH
PWD=/home/sam/book.examples/99/cp; export PWD
EXINIT=set\ ai\ aw; export EXINIT
LANG=C; export LANG

630 V.
PS1=\\\$\ ; export PS1

cd /home/sam/book\.examples/99/cp || {
echo 'Execution directory inaccessible' >&2
exit 1
}
find / -name "core" -print >report.out 2>report.err
lpr report.out

bzip2

bzip2 [] [_]
bunzip2 [] [_]
bzcat [] [_]
bzip2recover []
bzip2 , bunzip2
, bzip2, bzcat ,
bzip2.

_
, ( ), . _
, bzip2 . stdout bzip2 .

Linux bzip2, bunzip2 bzcat , . V.2.

BZIP2 MAC OS X
bzip2, ( ), Mac OSX,
Linux.

stdout c

decompress
d

, bzip2. bzip2
bunzip2.

. n
1 9, 1 (fast) best
100, 9 (best) 900. 9. fast best gzip
(fast) (best) .
fast

bzip2 631

, , .
bunzip2.
force f

keep k

quiet q

.
. , .
test t

, , .
verbose v

bzip2 bunzip2 , gzip gunzip;


, gzip. bzip2 ;
,
force.

tar
tar: . 82.
bzip2 - bzip.org.
bzip2 gzip.
bzip2, tar
bzip2.
bzcat _

cat, ,
_ bunzip2
.

bzip2recover

, bzip2.

bzip2 ,
.bz2. v .
$ ls -l
total 728
-rw-r--r-- 1 sam sam 737414 Feb 20 19:05 bigfile
$ bzip2 -v bigfile
bigfile: 3.926:1, 2.037 bits/byte, 74.53% saved, 737414 in, 187806 out
$ ls -l
total 188
-rw-r--r-- 1 sam sam 187806 Feb 20 19:05 bigfile.bz2

touch ,
; bunzip2
bigfile.bz2. force bunzip2 .

632 V.
$ touch bigfile
$ bunzip2 bigfile.bz2
bunzip2: Output file bigfile already exists. bigfile .
$ bunzip2 --force bigfile.bz2
$ ls -l
total 728
-rw-r--r-- 1 sam sam 737414 Feb 20 19:05 bigfile

cal
.
cal [] [[]]
cal .

, cal .
1 12,
.
cal .
.

(Julian ). , 1 (1) 31 (365 366).


j

m
(Monday ).
. . (Linux)
y

(year ). . (Linux)

( ). , .
(Linux)

. 05 2005.
ncal (new cal ) .

2011:
$ cal 6 2011<U>vim:
June 2011
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11

cat 633
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

1949:
$ cal -j 1949
Su

Mo

2
9
16
23
30

3
10
17
24
31

1949

January
Tu We Th

Fr

4
11
18
25

7
14
21
28

5
12
19
26

6
13
20
27

Sa
1
8
15
22
29

Su

Mo

37
44
51
58

38
45
52
59

February
Tu We Th
32 33 34
39 40 41
46 47 48
53 54 55

Fr
35
42
49
56

Sa
36
43
50
57

cat
.
cat [] [_]
cat . .

_
, cat.
(), cat .

Linux cat ,
. V.2. , (), Linux. , ,
, Linux,
OS X.
show-all A

, vET. (Linux)

numbernonblank
b

(end ). , vE.

show-ends
E

. (Linux)

number n

squeezeblank s

,
.

634 V.
t (tab)

, vT.

show-tabs
T

^I. (Linux)

(^M)
(META-)
, M- (. :
. 231).
. ,
^I , T (show-tabs ).
,
.
shownonprinting
v

cat,
. 144.
, (, ), od (. od).
tac.
info-, tac.
cat , catenate,
.


NOCLOBBER
, cat,
cat (letter):
$ cat memo letter > letter
cat: letter: input file is output file

(letter: )

,
noclobber (. 5, noclobber: . 148
9, .9.7).

memo:
$ cat memo

all:
$ cat page1 letter memo > all

cat
. , ,
, CONTROL+D:
$ cat > new_file

cd 635
()

CONTROL+D

cat (), ( ) .
CONTROL+D EOF (end of file)
cat .
who
cat. cat output;
, output header (), who footer
( ). cat
header footer .
$ who | cat header - footer > output

cd
.
cd [] []
cd .

, . cd .
.

bash dash.
( ). , cd
( ).
L

( ). , cd
, , .
P

cd bash, dash tcsh.


cd 4,
cd
: . 108.
cd ; , HOME (bash;
. HOME: . 311) home
(tcsh; . , , home
HOME . 391).

636 V.
, cd .
OLDPWD (bash) owd (tcsh).
CDPATH (bash) cdpath (tcsh) ,
, cd .
(::) (:.:) .
CDPATH cdpath , cd .
( ), cd
; , cd . CDPATH 8,
CDPATH: cd.

cd . cd Max,
pwd.
$ pwd
/home/max/literature
$ cd
$ pwd
/home/max

Mac OS X
/Users, /home.

/home/max/literature:
$ cd /home/max/literature
$ pwd
/home/max/literature

cd :
$ cd memos
$ pwd
/home/max/literature/memos

, cd .. (.
. .. . 108),
:
$ cd ..
$ pwd
/home/max/literature

chgrp
, .
chgrp [] _
chgrp [] reference=rfile _ (Linux)

chgrp 637

chgrp , .
Linux.

(ID) . _ , .
rfile , ,
, _.

, (), Linux. , ,
, Linux, OS X.
changes c

, . (Linux)

, ,
, , .
Linux . (Linux)
dereference

quiet f
silent
nodereference
h

, ID .
, ,
, , .

( ). ,
, , ,
. , , ,
. R. ,
, .
H L . 638.
H

( ). ,
, , , . . R. , , .
H L . 638.
L

( ). ,
, , ,
( ). R. , , . -P . 640.
P

638 V.
recursive
R

, _, ID .

reference=
rfile

, _, rfile. (Linux)


, .
verbose v

, ,
root-.
root- ID
- , .
, chown, , , ,
.

, manuals,
pubs.
$ chgrp pubs manuals

H
L

H L. zach:

$ ls -lR
.:
total 12
-rw-r--r-- 1 zach zach 102 Jul
drwxr-xr-x 2 zach zach 4096 Jul
drwxr-xr-x 2 zach zach 4096 Jul

2 12:31 bb
2 15:34 dir1
2 15:33 dir4

./dir1:
total 4
-rw-r--r-- 1 zach zach 102 Jul
lrwxrwxrwx 1 zach zach
7 Jul

2 12:32 dd
2 15:33 dir4.link -> ../dir4

./dir4:
total 8
-rw-r--r-- 1 zach zach 125 Jul
-rw-r--r-- 1 zach zach 375 Jul

2 15:33 gg
2 15:33 hh

chgrp R H ( H R )
chgrp , , , , . , chgrp
, .

chgrp 639

, ,
. dir1 chgrp
dir4.link, dir4, , dir4.link.
$ chgrp -RH pubs bb dir1
$ ls -lR
.:
total 12
-rw-r--r-- 1 zach pubs 102 Jul
drwxr-xr-x 2 zach pubs 4096 Jul
drwxr-xr-x 2 zach pubs 4096 Jul

2 12:31 bb
2 15:34 dir1
2 15:33 dir4

./dir1:
total 4
-rw-r--r-- 1 zach pubs 102 Jul
lrwxrwxrwx 1 zach zach
7 Jul

2 12:32 dd
2 15:33 dir4.link -> ../dir4

./dir4:
total 8
-rw-r--r-- 1 zach zach 125 Jul
-rw-r--r-- 1 zach zach 375 Jul

2 15:33 gg
2 15:33 hh

H MAC OS X

Mac OS X H chgrp

, Linux. OS X, chgrp RH
, ,
, , . (
.) OS X
dir4 ,
dir4.link.

H

OS X, , ,
.

chgrp R L ( L R )
chgrp : ,
, , .
. ,
dir4.link:
$ chgrp -RL pubs bb dir1
$ ls -lR
.:
total 12
-rw-r--r-- 1 zach pubs 102 Jul
drwxr-xr-x 2 zach pubs 4096 Jul
drwxr-xr-x 2 zach pubs 4096 Jul
./dir1:
total 4
-rw-r--r-- 1 zach pubs 102 Jul

2 12:31 bb
2 15:34 dir1
2 15:33 dir4

2 12:32 dd

640 V.
lrwxrwxrwx 1 zach zach
7 Jul
./dir4:
total 8
-rw-r--r-- 1 zach pubs 125 Jul
-rw-r--r-- 1 zach pubs 375 Jul

2 15:33 dir4.link -> ../dir4


2 15:33 gg
2 15:33 hh

chgrp R P ( P R
) chgrp . , .
P

$ ls -l bb*
-rw-r--r-- 1 zach zach 102 Jul
lrwxrwxrwx 1 zach zach
2 Jul

2 12:31 bb
2 16:02 bb.link -> bb

$ chgrp -PR pubs bb.link


$ ls -l bb*
-rw-r--r-- 1 zach zach 102 Jul
lrwxrwxrwx 1 zach pubs
2 Jul

2 12:31 bb
2 16:02 bb.link -> bb

chmod
() .
chmod [] _

chmod [] _

chmod [] reference=rfile _
(Linux)
chmod ,
, , () .
. Linux ( ).
Mac OS X chmod
ACL-.

, . rfile ,
, _.


( ), .
chmod ,
. ,
.V.4.
.V.5 , .
,
.V.6.

chmod 641
V.4.

ugo

V.5.

V.6.

ID

ID

( ) . (
4, setuid setgid . 118.)

sticky bit. (
, root-,
, u)


.
.V.7.
(OR),
. ( .V.8.)
V.7.

4000

ID ( setuid
setgid . 118)

2000

ID ( setuid setgid)

642 V.
V.7 ()

1000

(sticky bit)

0400

0200

0100

0040

0020

0010

0004

0002

0001

.V.8 .
V.8.

0777

, ,

0755

, ,

0711

, ,

0644

0640

, (), Linux. , ,
, Linux, OS X.
, . (Linux)
changes c

quiet f
silent

,
chmod .

( ). ,
, , ,
. ,
, ,
. R.
, , .
H

chmod 643

chgrp,
H L. (OS X)
( ). ,
, , ,
. . R. ,
, .
chgrp, H L. (OS X)
L

( ). ,
, , ,
( ). . R. ,
, .
chgrp, . (OS X)
P

recursive
R

, _, .

reference=
rfile

, _
, rfile. (Linux)

,
( ), . , , changes.
verbose v

( )
, root-.
=, , . .
.
Linux chmod .
Linux chmod ,
. , chmod , , , ,
.
Mac OS X H.
chmod chmod:
. 116.

chmod
temp. temp

644 V.
ls. , ls,
, .
$ ls -l temp
-rw-rw-r-- 1 max pubs 57 Jul 12 16:47 temp

, chmod
.
, :
$ chmod go= temp
$ ls -l temp
-rw------- 1 max pubs 57 Jul 12 16:47 temp

(,
), .
$ chmod a=rw temp
$ ls -l temp
-rw-rw-rw- 1 max pubs 57 Jul 12 16:47 temp

a=rw 666.
, :
$ chmod 666 temp

.
pubs , :
$ chmod o-w temp
$ ls -l temp
-rw-rw-r-- 1 max pubs 57 Jul 12 16:47 temp

:
$ chmod 664 temp

:
$ chmod a+x temp
$ ls -l temp
-rwxrwxr-x 1 max pubs 57 Jul 12 16:47 temp

temp ,
. (

, .)
, , :
$ chmod 775 temp

, , . ,
. .
$ chmod ug=rwx,o=rx temp

chown 645

chown
() , .
chown [] _
chown [] : _
chown [] : _
chown [] : _
chown [] reference=rfile _ (Linux)
chown () ,
. , root. , , , root-, , .
Linux.

ID. _
,
. ,
ID. rfile ,
() () ,
_. .V.9 ,
() .
V.9. ()

, _;

, , _

, _; ,
, _,
,

, , _;

Linux chown , . V.2. , (),


Linux. ,
, ,
Linux, OS X.
,
. (Linux)
changes c

646 V.

, , .
Linux . (Linux)

dereference

quiet f
silent

,
chown .

( ). ,
, ()
, , .
, ,
, .
R. , ,
.
chgrp, H L.
H

nodereference
h

, ,
() , ,
, .

( ). ,
, () , , ,
, .
. R. , , .
chgrp, H
L.
L

( ). ,
, () , ,
, .
. R. , , . chgrp, .
P

_ , , ()
, .
recursive
R

() , _ () ,
rfile. (Linux)

reference=rfile

verbose v
, ()
, , .

chown setuid
setgid.

cmp 647

chapter1 manuals.
Sam.
# chown sam manuals/chapter1

Max , ,
, , /home/max/literature
.
# chown -R max: /home/max/literature

Mac OS X
/home, /Users.


literature max, , , pubs:
# chown max:pubs /home/max/literature/*

, manuals,
pubs . , , pubs.
$ chown :pubs manuals/*

cmp
.
cmp [] 1 [2 [1 [2]]]
cmp
. , cmp . , cmp
.

1 2 , cmp. 2 , cmp
. () 1 2 cmp
.
1 2 ,
,
. 1 2 (. .V.1).

Linux OS X cmp
, .V.2. , (),
Linux. ,
, Linux

, OS X.

648 V.
printbytes
b

, , ,
ASCII- .

ignore
initial=
n1[:n2]
i n1[:n2]

n2, n1
. n1 n2,
n1 1 n2 2. n1 () n2
(. .V.1).

verbose l
( l ). ,
.
,
. ,
EOF.
silent
quiet
s

cmp, (.
).

1.
cmp , , . 0,
, 1, .
.
1 ( 2) cmp
, .
Mac OS X cmp (data
forks). (resource forks),
..namedfork/rsrc .
diff (. ), cmp , ASCII.

a b.
. , lazy a
b lasy. : b (TAB).
$ cat a
The quick brown fox jumped over the lazy dog.
$ cat b
The quick brown fox jumped over the lasy dog.TAB

cmp . , ,
, :
$ cmp a b
a b differ: char 39, line 1

: 39, 1

comm 649

b (printbytes), ,
:
$ cmp --print-bytes a b
a b differ: char 39, line 1 is 172 z 163 s
: 39, 1 172 z 163 s

l ,
. , .
, a b.
b . , a
(CONTROL+J), b (CONTROL+I).
, , , b
a.
$ cmp -lb a b
39 172 z
163 s
46 12 ^J
11 ^I
cmp: EOF on a


ignoreinitial. cmp .
7, 46- b (7
39 ).
$ cmp --ignore-initial=39 a b
a b differ: char 7, line 1

ignoreinitial 1 2:
$ cmp a b 39 39
a b differ: char 7, line 1

comm
.
comm [] 1 2
comm
. ,
1, ,
2, , .

1 2 ,
comm. () 1
2 comm ,
.

650 V.

. comm ,
.
( ,
1).

( ,
2).

( ,
).
3

, comm .
,
. (0) comm ( 0).

: c d. comm,
:
$ cat c
bbbbb
ccccc
ddddd
eeeee
fffff
$ cat d
aaaaa
ddddd
eeeee
ggggg
hhhhh

, sort.
comm ,
. ,
c, , d, , , c d:
$ comm c d
aaaaa
bbbbb
ccccc

ddddd
eeeee
fffff
ggggg
hhhhh

configure

651

1 2.

$ mm -12
ddddd

3, ,

d:

configure
.

/configure

configure

GNU- .

,
,


, .

configure.
configure

. , , ,
make (. )
.

configure

J1
rJ

CONFIGURE 05

configure, (- -),
OS , Linux.

--d4 sabl

, -- l -, ,

-- -

. , -

, -

Z Shell

conf4 gure

-- l e-zsh-mem
,

zsh,

. ,
,

README,

--hel

configure.

--pref4x=

, jusrj

configure

652

v.

local ( make l nsta ll).


.

--w4th-

. ,

conflgure --wlth-dll Windows wine,

, n,
README, .

Windows.

confl gure --he l .

GNU

configure,
. configure
, ,

. configure
make

make l nsta ll.

configure , -
( gcc) , .

CFLAGS. .

- .

GNU,

configure,
GNu-I autoconf (www.gnu.orgjsoftwarejautoconf).
README INSTALL,

.
configure ,
. - configure

.
,

configure

configure cd
,
, :

$ ./configure

653

. /, ,
eonfigure,
. , eonfigure ,
gee -Wa II -02, bash :
$ CFLAGS="-Wall -02" ./configure

tesh, :

tcsh $ env CFLAGS="-Wall -02" ./configure

J
J

[u -uu -nuu
[u n_ _ uu -nuu

.
( ), (
).

-R

-uu
n

, .

.
nu__uu

, . -nuu

, .

, -.

-R

n _ _ uu -nuu.

Linux ,
V.2. ,
(--), Linux. ,
, , Linux, OS .
.

- - rch4 ve -

, , ,

()

.
-dR.

--backup -

(Linux)

, ,

. , -

654

v.

, (~).

- -backup - - force

if-,

(core utils). (Linux)

, ,

-d

, , .

-nuu,
u-uuu.

--no-dereference

-- preserve= l4 nk s. (Linux)

--force -f

-nuu

, -nuu
-uu. ,
, , ,

, -nuu.
- ,

- .

( ). ,

, , ,
. ,
, ,

OS - R.

, ,
.
-

-chgrp-,

- L-

--4 nteract4 ve

-4

, . - ,
.

--dereference

( ). ,

-L

, , ,

. .

OS

- R.

, ,
.
- -

-chgrp-,

- L-.

--

( ). ,

dereference

, , ,

. .

OS

-R.

, ,
.

-chgrp-,

- .
--preserve

- , ,

[=J

, ,

655

-uu. - .
attr

--preserve

attr ,

- ownershlp

- mode

( L-),

( ),

( ),
)

- II nks

- tlmestamps

II ( ).

--parents

. . --.

(Linux)

--recurs4ve

-R

-r

--no-dereference (-d):

Linux

-R, -r

--recursl ve,

( , ). -r

--recurslve

Linux.

, -nuu

--update -u

-uu

( -nuu).

(Linux)
.

--verbose -v

Linux ,
-R, -r, --recurslve, -,

-d --no-dereference.

-- ,

Linux -

, .

OS -R ,

-R

( -),

- L.
Linux

, if-,

(core utils).

-nuu ,
, , ,
, .

-nuu , ,
-uu. , ,
-n, , ,
, -nuu.

-
, , , I
-uu.
In (. ), -n,

eM , -uu.

656

v.

OS , 10.4,

letter .

letter.sav.

1etter 1etter.sav

, . ,

archives, .
,
. - (--preserve) , , archives, , , ,

, -.

- *.

arehives

Sam

-sam/memo .

Linux,

memojthursdayjmax dir dirjmemojthursdayjmax


- -parents. find .

$ --parents memo/thursday/max dir


$ find dir
dir
dir/memo
dir/memo/thursday
dir/memo/thursday/max

letter .

, (

letter), .
jhomejsamjmemo jhomejsamj

letter.

1etter /home/sam

- f (- - force).


(),
. ,
, , .

- f (- - force)

,
.

$ ls -ld
drwxrwxr-x
$ 1s -1
-rw-r--r--rw-rw-r--

4096 Oct 21 22:55 .

1 root root 3555 Oct 21 22:54


1 1222 Oct 21 22:55

epio
$

657

one

eannot ereate regular flle

'':

PermlSSlOn denled

$ -f one
$ 1s -1

-rw-r--r--rw-rw-r--

1
1

1222 Oet 21 22:58


1222 Oet 21 22:55

'' :

(--baekup),

- f (-- foree)

e~.

4, - - . 11 g.

CPIO
,
.

cpio - -createl-o []
cpio - -extractl-i [] [_]
cpio - -pass-throughl-p [] -

- create (copy-out),
- extract (copy-in),
, - pass-through (copy-pass),
. epio
epio

,
.

(create)


. .

(extract)

. ,
, .

(pass-through)

, .

epio

(extract)

. ,
_.
_,

epio

.
(.

5,

epio

- .

157), , n_
(1) (.).
(pass-through) epio

-n.

658

v.

cpio:

(extract)

(create),

(pass-through).


. ,

(- - ),

Linux.

, ,

--extract -4

Linux,

- copy-in).

OS

. _, cpio
. _ cpio
, ,

_. ,

jdevjsde1, , . :
$ cpio -; \*. < /dev/sdel

--create -

* ,

cpio.

- copy-out).

.
, .
.

, cpio, find.
,

jd evjsd 1 :

$ find / -depth -print

cpio

>/dev/sdel

-depth find , ,

,
. . ,
--.

--pass-through

. , ,

- copy-pass).

cpio - (

cpio). ,
,
, , .


~maxjcode:

$ find . -depth -print

cpio -pdm -max/code


I cpio.
.

659

cpio
,

Linux.

(--),

OS

, ,

Linux,

--resetaccess-t4me

, .

-8

(block -

).

512

5120

Linux

,
.

OS

--k-s4z=

512

).

(compatible -

(Linux)

ASCII,

( )

cpio

.
.

--maked4 rector4 es

-d

, ,

find -depth.

-4 (--extract)

(--pass -through).

_ _.

--patternf41 =

_ .

--f41 =

. -

-F

, .

, .

- f ( - - f4l )

tar.

). ,

--nonmatch4 ng

(flip -

-f

_ . -

--he1p

(Linux)

, ,

dereference
-L

, ( ).

, .

--14nk -1

, .

660

--preservemod4 f4 cat4 t4me

v.

. ,
.

, .

--no-abso1 utef41 enames

, ,

--rename -r

cpio.

(Linux)

cpio

. .

cpio

(table of contents -

--14 st -t

). .

-4 (--extract),
-v (--verbose)

l s - l.

uncond4t4ona1
-u

cpio

--verbose -v

- t (-- l4 st)

l s -l.

- u (--uncond4 t 4 l) cpio

, .
,
. ,
:
,
.

,
, . ,
-

(--preserve-modlf4cat4on-t4me).

, ,
,
.

,
,

.
,

, .

cpio

661

-depth, find ,
.
,
. (. .)

-d (--make-dl rectorles) cpio


, -

(--preserve-modlflcatlon-tlme),

, .

.
.

cpio , -d (--make-dl rectorl es),


.
( ),
.

Sam,

US-, jdevjsde1.

$ find /home/sam -depth -print

cpio

>/dev/sdel

find , cpio
-depth
, cpio

(. --).

(. )

-d (--make-directorles)

5120

(--preserve-modlflcatlon-tlme).

jhome,

OS

jUsers.

$ cpio -itv < /dev/sdel


,

Sam:

$ cpio -idm /home/sam/memo/\* < /dev/sdel


-d (- -make -dl rectorl es)
, . -

catlon-tlme)

(--preserve-modl fl-

,
.

Linu- --no-abso l ute- fll enames


. ,
,

cpio .

662

v.

$ pwd
/home/sam/memocopy
$ cpio -idm --no-absolute-filenames home/sam/memo/\* < /dev/sdel
-f,

, , , :

$ cpio -ivmdf /home/sam/memo/\* < /dev/sdel


-v
cpio, .

crontab
nt-.

crontab
crontab

J
J

[- u _ u _
[- u _

nt- (,

14:00

) . cron .

, crontab

, ,

crontab-

. , t-, nt-
.

u_

( nt-) nt- ,
, , u_n

. nt-, ,

nt-, . uu_

(-), crontab

, nt-.

: -,

- 1 - r. ,

t-, -u.

(edit -

). ,

vISUAL EDIOR nt-,

, .

nt- .
). nt-.

-1

(list -

-r

(remove -

). nt-.

-u _

(user -

). nt-, -

, t-.

crontab

663

cron, crontab nt-,


(Paul Vixie), - cron


POSIX -

Vixie cron.

Vixie cron, ,

S VR3.

nt- jvar jspooljcron

jvar jspooljcronjcrontabs. ,

cron nt- .
nt- ,
, ,

MAI L crontab-

- jetcjcron.hourly, - jetcjcron.daily, jetcjcron.


weekly - jetcjcron. month Iy, nt-,
run-parts, , ,
jetcjcrontab.

, . ,

root-

nt-. jetcjcrontab :

$ cat /etc/crontab
SHELL =/ n/bash
PATH=/sbln:/bln:/usr/sbln:/usr/bln
MAILTO=root
=/

# run-parts
01 * * * * root
02 4 * * * root
22 4 * * root
42 4 1 * * root

run-parts
run-parts
run-parts
run-parts

/etc/cron.hourly
/etc/cron.dally
/etc/cron.weekly
/etc/cron.monthly

nt- , ,
(, , , ).

cron ,

, .

cron ,

, ,

.
,
, , .

cron ( )

nt- . cron

664

v.

, , nt-,
, , .
,

cron.allow,
cron.deny

crontab,

, t- -

cron.deny.

Linux

cron .allow

jetc, OS jvarjat (
jusrjlibjcron). cron.deny
cron.allow crontab
. cron .allow, crontab
, ,

cron.deny. cron.

allow , crontab,
cron.deny ,
. ( cron.deny
, cron.allow ,
crontab.)

nt-

Sam

(jvarjspooljcronjsam) crontab -l. ,

Sam,

~jbin.

MAI L , ,
nt-

Sam,

sat.job ( 6) 2:05 (), twice.week


12:02 () ( 4), twice.
day 10:05 () 4:05 ().

$ who
sam

$ crontab -1
MAILTO=max

05 02 * * 6 $HOME/bln/sat.job
02 00 * * 0,4 $HOME/bln/twlce.week
05 10,16 * * * $HOME/bln/twlce.day
nt-, crontab
-

(edit).

Linu- crontab,

-. ,

nt-, ,
crontab , .
nt-.

$ crontab -1 > newcron


$ vim newcron
$ crontab newcron

-l (list)

cut

665

cut
.

cut [J [ _J
cut

(-), cut

Linux cut

V.2.

Linux.

OS

(--),

Linux,

--characters=

c/ist

clist. 3clist , -

- clist

, .

, .
n-,

n-

-n

n- .

- -d 14 4 te r=

dc har .

dchar
-d dchar

dchar

, -

- -output- de l4 4 ter. -

. ,
.
--f4 1 ds=fIist

, flist. 3flist

-f f1ist

. -

, .
n-,

n-

-n

n- .

--output de14 m4ter=


ochar

- -de l4 4 ter.

ochar

--de l4 mlter.

, .
--1

, .

de14m4ted -s

cut

, , -

666

v.

, cut

,
. cut
paste (. ).

1s -l

$ 1s -1
total 2944
-rwxr-xr-x
-rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--

1
1
1
1
1
1
1

zach
zach
zach
zach
zach
zach
zach

pubs
259 Feb 1 00:12 countout
pubs
9453 Feb 4 23:17 headers
pubs 1474828 Jan 14 14:15
pubs 1474828 Jan 14 14:33 memos save
pubs
7134 Feb 4 23:18 tmp1
pubs
4770 Feb 4 23: 26 tmp2
pubs
13580 Nov 7 08:01 typescrlpt

cut - .
.

$ 1s -1

I cut - 2 -10

otal 2944
rwxr-xr-x
rw-rw-r-rw-rw-r-rw-rw-r-rw-rw-r-rw-rw-r-rw-rw-r- .
- f .

-d

cut , .

tr (. )

-s

, cut
.

I tr -s
259 countout
9453 headers
1474828
1474828 memos save
7134 tmp1
4770 tmp2
13580 typescrlpt
$ 1s -1

, , , I cut - f5 . 9

-d' ,

,
jetcjpasswd. -d , -

667

date

OS ,

jetcjpasswd

Open Directory.

$ cat /etc/passwd

root:x:O:O:Root:/:/bln/sh
sam:x:401:50:Sam the Great:/home/sam:/bln/zsh
::402:50: Wlld:/home/max:/bln/bash
zach: : 504: 500: Zach Brlll : /home/zach: / n/tcsh
hls:x:505:500:Helen Slmpson:/home/hls:/bln/bash
$ cut -d: -f5 /etc/passwd

Root
Sam the Great
Wlld
Zach Brlll
Helen Slmpson

date
.

date
date

[] [+]
[] [_]

date . ,
t-, date .

+ . ,
,

(+).

, .

V.10.

V.10.

emI

J I

Sun Sat

( )

Sunday Saturday (

J anuary

%8

, JI

%d

%0

//

%1

%j

01

J an Dec ( )
December ( )

31

00 23
00 12
-

001

366)

12
,

668

V.10

v.

()

00 59

I ( I )

%r

%s

I ,

%S

%t

AMjPM
1 1970

00 60 (60 rn )

::

%w

(,

%Z

(,

6 ( = Sunday
-

[])

00 99
2009)

PDT)

date .

),

.
,

date

(-),

(%),

date

date , ,
,

, ,
.
(,
:). ,
, ,
.

, t-, 1l0_

, , -

. 1l0_ :

nnddhhmm[[ ]yyJ[.ss ]

nn - (01-12), dd (00-23) mm - (00-59).

(01-31), hh -

24-

.
(
.

mm .

1),

, date , .

.ss , I.

Linux date ,
V.2. ,

669

dd

(--),

Linux.

, ,

Linux,

OS

--date=

, , _.

-.

-d

(Linux)

--reference=

(Linux)

-r

--utc
--un4 versal

-u

UTC

- Universal Coordinated Time (UTC).


- Greenwich Mean

Time (GMT).

, date
, .

14:07:30 19 ),

2:07:30

August 19

# date 08191407.30
Frl Aug 19 14:07:30

2009

date :

$ date '+Today is %h %d.


Today lS Aug 19, 2009

%'

dd
.

dd []
dd (

device-to-device -

.
dd ,

.
. dd
,

.
.

V.

670

Lin ux dd
V.2. dd

, .
.

s=

).

(block size -

s=

l bs

n .

obs.

(conversion block size - ).


n .

v=

,
n ,

[ , .. .]

. .

V.11.

count=

, dd,

u_.

4s=

(input block size -

4 f=_

(input file -

bs l bs.

).

n .

). ,

_. - u_
.
s=

(output block size -

f=_

(output file -

).

n .

). ,

u_. - uu_
.

seek=

u_

obs.

bs

sklP=

u_

asc44

V.11.

bs

l bs.

amI

EBCDIC ASCII,
IBM

block

( , -

) . :
,

bs obs

ebcd4c

amI

ASCII EBCDIC,
IBM -

dd 671

lcase

noerror

dd . dd ,

notrunc

ucase

unblock

Linux

,
.V.1. Mac OS X
,
OS X , ,
OS X dd b (,
512) w (, ,
).

dd , .
$ dd if=/dev/urandom of=randfile2 bs=1 count=100

/dev/urandom ( )
randfile. 1, count 100,
randfile 100. ,
/dev/random.
man- urandom random. OS X urandom
random .
dd .

/dev/sdb1.
# dd if=/dev/sda1 of=/dev/sdb1

, .
,
.
ls secret,
dd 1 count,

672 V.
secret, . conv=notrunc
dd , , -
.
$ ls -l secret
-rw-rw-r-- 1 max max 2494 Feb 6 00:56 secret
$ dd if=/dev/urandom of=secret bs=1 count=2494 conv=notrunc
2494+0 records in
2494+0 records out
$ rm secret

dd .

, .
.
, DOS. (
, .)
mount, ls, umount ,
. floppy.copy.

# mount -t msdos /dev/fd0H1440 /mnt


# ls /mnt
abprint.dat bti.ini
setup.ins
supfiles.z
wbt.z
adbook.z
setup.exe
setup.pkg
telephon.z
# umount /mnt
# dd if=/dev/fd0 ibs=512 > floppy.copy
2880+0 records in
2880+0 records out
# ls -l floppy.copy
-rw-rw-r-1 max speedy 1474560 Oct 11 05:43 floppy.copy
# dd if=floppy.copy bs=512 of=/dev/fd0
2880+0 records in
2880+0 records out
# mount -t msdos /dev/fd0H1440 /mnt
# ls /mnt
abprint.dat bti.ini
setup.ins
supfiles.z
wbt.z
adbook.z
setup.exe
setup.pkg
telephon.z
# umount /mnt

df
.
df [] [__]
df (
disk

free
) , .

df ,
.

df 673

__
, , . Mac OS X Linux-.

, .

, (), Linux.
, ,
, Linux, OSX.
0 ,
/dev/proc. df .

all a

sz ,
( 1),
.V.1. . h (human-readable)
H (si). (Linux)
blocksize=sz
B sz

(). . (OS X)

si H
, , K (), M () G () . 1000.
humanreadable
h

, , K (), M () G () . 1024.

inodes i

i-.

(). .

local l

(). . (OS X)

fstype,
DOS NFS.
. (Linux)
type=fstype
t fstype

excludetype=fstype

,
fstype. (Linux)

x fstype

Mac OS X df
BLOCKSIZE (. BLOCKSIZE )
512 .
Mac OS X i- ( i )
HFS+ .
.

674 V.

df ,
:
$ df
Filesystem
/dev/hda12
/dev/hda1
/dev/hda8
/dev/hda9
/dev/hda10
/dev/hda5
/dev/hda7
/dev/hda6
zach:/c
zach:/d

1k-blocks
1517920
15522
1011928
1011928
1130540
4032092
1011928
2522048
2096160
2096450

Used Available Use% Mounted on


53264
1387548
4% /
4846
9875 33% /boot
110268
850256 11% /free1
30624
929900
3% /free2
78992
994120
7% /free3
1988080
1839188 52% /home
60
960464
0% /tmp
824084
1569848 34% /usr
1811392
284768 86% /zach_c
1935097
161353 92% /zach_d

df l h,
(human-readable) .
.
$ df -lh
Filesystem
/dev/hda12
/dev/hda1
/dev/hda8
/dev/hda9
/dev/hda10
/dev/hda5
/dev/hda7
/dev/hda6

Size
1.4G
15M
988M
988M
1.1G
3.8G
988M
2.4G

Used Avail Use% Mounted on


52M 1.3G
4% /
4.7M 9.6M 33% /boot
108M 830M 11% /free1
30M 908M
3% /free2
77M 971M
7% /free3
1.9G 1.8G 52% /home
60k 938M
0% /tmp
805M 1.5G 34% /usr

, Linux,
/free2 :
$ df -BM /free2
Filesystem
/dev/hda9

1M-blocks
988

Used Available Use% Mounted on


30
908
3% /free2

, Linux,
NFS :
$ df -ht nfs
Filesystem
zach:/c
zach:/d

Size
2.0G
2.0G

Used Avail Use% Mounted on


1.7G 278M 86% /zach_c
1.8G 157M 92% /zach_d

diff
.
diff [] 1 2
diff [] 1
diff [] 2
diff [] 1 2

diff 675

diff .
diff ,
, ,
.

1 2 ,
diff. 2 , diff , 1.
, 1.
, diff 1 ,
2.

diff , .V.2,
: ,
, .

DIFF MAC OS X
diff, ( ), Mac OS X,
Linux.

ignoreblank-lines
B

, .

ignorespace-change
b

( ) .

,
( 3) C []
. 1, 2,
(), 2 (+), ,
,
(!). , , , .
context
[=]

ed e
ed,
1, 2. , ed, w
(Write ) q (Quit ). ed
diff : , , , ,
,
ed. , , .
ignore-case
i

676 V.
,
( ).
new-file N

show-cfunction

, C,
bash, Perl , .

p
brief
q

. diff , .

recursive
r

diff .

. diff diff: . 74.


,
.
unified
[=]
U

ignore-allspace
w

(whitespace ).
.

, diff
, n .
side-by-side. sdiff (. )
w :
width=n
W n

w n.

.
, sdiff.
width=n.
side-by-side
y

sdiff diff, .
side-by-side diff , sdiff.
man- info-, diff sdiff.
diff3.
() cmp
(. ).

diff , ,
Add (a), Delete (d) Change (c).
, , ,
. (<) 1.
(>) 2. diff

diff 677

, .
V
.12. , , , .
diff , 1 2.
a, c d 1, 2. 2
1, diff, .
V.12. diff

( 1 2)

1 a 2,3
> 2

2 3
2 1 1

1,2 d 3
< 1

, 1 2
1

1,2 c 3,4
< 1

> 2

1 2
1 3 4
2

, diff ,
:
$ cat m
aaaaa
bbbbb
ccccc
$ cat n
aaaaa
ccccc
$ diff m n
2d1
< bbbbb

m n , m (bbbbb)
n. , diff (2d1), ,
1 (m)
2 (n), .
diff (<), ,
1. ,
, .
30 ,
side-by-side, sdiff
.
m; diff (sdiff) n,
, .
$ diff --side-by-side --width=30 m n
aaaaa
aaaaa
bbbbb
<

678 V.
ccccc

ccccc

$ sdiff -w 30 m n
aaaaa
aaaaa
bbbbb
<
ccccc
ccccc

m p, diff, a (Append):
$ cat p
aaaaa
bbbbb
rrrrr
ccccc
$ diff m p
2a3
> rrrrr

diff 2a3, ,
m 2, , p.
diff , , p ( >, 2).
, rrrrr.
, diff , , m, r:
$ cat r
aaaaa
-q
ccccc
$ diff m r
2c2
< bbbbb
--> -q

2: m bbbbb,
r q. diff 2c2, ,
2. diff , 2 m (bbbbb), 2 r (q),
. m,
, r, .
(y W) .
(|) ,
, :
$ diff -y -W 30 m r
aaaaa
aaaaa
bbbbb
| -q
ccccc
ccccc

diff 679

q v:
$ cat q
Monday
Tuesday
Wednesday
Thursday
Saturday
Sunday

$ cat v
Monday
Wednesday
Thursday
Thursday
Friday
Saturday
Sundae

diff ,
v Tuesday, q Thursday
( v ), Friday q. q Sunday, v Sundae: diff ,
. q, v, Tuesday, Thursday Friday
Sundae v Sunday q.
v, q, Tuesday,
Thursday Friday Sunday q
Sundae v.
$ diff -y -W 30 q v
Monday
Monday
Tuesday
<
Wednesday
Wednesday
Thursday
Thursday
> Thursday
> Friday
Saturday
Saturday
Sunday
| Sundae

context ( context diff )


diff , ,
. , q
, v . , , 1,6
. , ,
q , 1
6 ( q;
). , Tuesday. ,
Sunday v. ,
1,7 , , , v, 1 7,
q. Thursday
Friday Sunday q Sundae ( v).
Context diff

$ diff --context q v
*** q
Mon Aug 24 18:26:45 2009
--- v
Mon Aug 24 18:27:55 2009

680 V.
***************
*** 1,6 ****
Monday
- Tuesday
Wednesday
Thursday
Saturday
! Sunday
--- 1,7 ---Monday
Wednesday
Thursday
+ Thursday
+ Friday
Saturday
! Sundae

diskutil (OS X)
, .
diskutil []
diskutil , ().
.

, diskutil. .V.13
, .
V.13. diskutil

eraseVolume

-
. ;
, .

HFS+, UFS MS-DOS.
. ,
FAT32 ( Windows 98
) MS-DOS FAT32.
HFS+
Case-sensitive Journaled HFS+

info

list

[]

.
, .

mount

diskutil (OS X) 681

mountDisk

reformat

repairVolume

unmount

unmountDisk

verifyVolume

diskutil Disk
Management framework, , Disk
Utility
. , .
, , root-.
fsck
Linux- verifyVolume repairVolume
diskutil fsck. OSX fsck , -

, .
, diskutil, disktool.

disktool

, :
$ diskutil list
/dev/disk0
#:
type
0: Apple_partition_scheme
1:
Apple_partition_map
2:
Apple_HFS
3:
Apple_HFS
/dev/disk1
#:
type
0: Apple_partition_scheme
1:
Apple_partition_map
2:
Apple_Driver43
3:
Apple_Driver43
4:
Apple_Driver_ATA

name
Eva01
Users
name

size
*152.7 GB
31.5 KB
30.7 GB
121.7 GB

identifier
disk0
disk0s1
disk0s3
disk0s5

size
*232.9 GB
31.5 KB
28.0 KB
28.0 KB
28.0 KB

identifier
disk1
disk1s1
disk1s2
disk1s3
disk1s4

682 V.
5:
6:
7:
8:
9:
10:

Apple_Driver_ATA
Apple_FWDriver
Apple_Driver_IOKit
Apple_Patches
Apple_HFS Spare
Apple_HFS House

28.0 KB
256.0 KB
256.0 KB
256.0 KB
48.8 GB
184.1 GB

disk1s5
disk1s6
disk1s7
disk1s8
disk1s9
disk1s10

:
$ diskutil info disk1s9
Device Node:
/dev/disk1s9
Device Identifier: disk1s9
Mount Point:
/Volumes/Spare
Volume Name:
Spare
File System:
HFS+
Owners:
Enabled
Partition Type:
Apple_HFS
Bootable:
Is bootable
Media Type:
Generic
Protocol:
FireWire
SMART Status:
Not Supported
UUID:
C77BB3DC-EFBB-30B0-B191-DE7E01D8A563
Total Size:
Free Space:

48.8 GB
48.8 GB

Read Only:
Ejectable:

No
Yes

/dev/disk1s8
HFS+ Extended (HFSX), Spare2.
:
# diskutil eraseVolume 'Case-sensitive HFS+' Spare2 disk1s8
Started erase on disk disk1s10
Erasing
Mounting Disk
Finished erase on disk disk1s10


verifyVolume:
$ diskutil verifyVolume disk1s9
Started verify/repair on volume disk1s9 Spare
Checking HFS Plus volume.
Checking Extents Overflow file.
Checking Catalog file.
Checking Catalog hierarchy.
Checking volume bitmap.
Checking volume information.
The volume Spare appears to be OK.
Mounting Disk
Verify/repair finished on volume disk1s9 Spare

ditto (OS X) 683

ditto (OS X)
.
ditto [] - -
ditto [] _- -
ditto c [] - -
ditto x [] _- -
ditto , ,
.
, cpio zip, . .

- , ditto. , ditto .
_-
, ditto . -
, ditto . , ditto ,
-.
- , ditto . -,
.
_- , ditto -.
() ditto ,
.

c x .
c

(create archive ). .

help

k
(pkzip). zip- cpio (. ) . zip 3, gzip zip
gzip: . 82.

(no resource ). .
ditto data forks (
Mac OS X 10.3 ).
norsrc

684 V.
(resource ). , resource forks ( Mac OS X 10.3 ). rsrc
rsrcFork.
rsrc

(very verbose ).
, , ditto.
V

(verbose ).
ditto .
v

(exclude ). ditto , , , ,
.
X

(extract archive ). -

.
z
(compress ). gzip (. ) gunzip cpio.

ditto .
ACL-.
ditto cpio (.
).
ditto ,
.
cpio pax cpio, zip- unzip l.

, ( ,
) .
Zach ( ),
Backups, zach.0228:
$ ditto /Users/zach /Volumes/Backups/zach.0228

Zach
cpio Backups:
$ ditto -c /Users/zach /Volumes/Backups/zach.0228.cpio

Zach zip-:
$ ditto -c -k /Users/zach /Volumes/Backups/zach.0228.zip

Zach

, :

dmesg 685
$ ditto /Volumes/Backups/zach.0228 /Users/zach
$ ditto -x /Volumes/Backups/zach.0228.cpio /Users/zach
$ ditto -x -k /Volumes/Backups/zach.0228.zip /Users/zach

Scripts ScriptsBackups bravo. - - _ :


$ ditto -c Scripts - | ssh bravo ditto -x - ScriptsBackups

( )
Backups.root.
,
root-. X ditto ( )
/.
# ditto -X / /Volumes/Backups.root

dmesg
.
dmesg []
dmesg , .

dmesg. (Linux)

core ( ), /dev/kmem. (OS X)

M core

N kernel

kernel (

/mach). , kernel , -

core. (OS X)

, .
.

Mac OS X , root.
, , .

686 V.
, ,
:
$ dmesg > dmesg.boot

dmesg.boot.

.

Linux
- /var/log/messages
, dmesg.

, serial, :
$ dmesg | grep -i serial
Apple16X50PCI2: Identified 4 Serial channels at PCI SLOT-2 Bus=5 Dev=2 Func=0

dscl (OS X)
(Directory Service).
dscl [] [_ []]
dscl (
Directory

Service

command

line
) .
dscl .

_ Mac OS X Server,
IP-. (.) .

(prompt ). ,

.
q

(quiet ) .

,
.
() .
list
[]

( ls). . ,

dscl (OS X) 687


read [
[]]

( cat .). .

readall
[
[]]

search

, .

( ), .
. -,
. dscl
, , ,
. , .
, dscl ,
.
Mac

OS

X
Mac

OS

Server
Open

Directory
XML-, /var/db/dslocal.
dscl
NetInfo Manager ( Mac OS X, 10.5)
Workgroup Manager Mac OS X Server.

dscl list, /, :
$ dscl . -list /
AFPServer
AFPUserAliases
Aliases
AppleMetaRecord
Augments
Automount

SharePoints
SMBServer
Users
WebServer

, dscl, . Users:
$ dscl . -list /Users
_amavisd
_appowner
_appserver
_ard

688 V.
_www
_xgridagent
_xgridcontroller
daemon
max
nobody
root

dscl read
:
$ dscl . -read /Users/root
AppleMetaNodeLocation: /Local/Default
GeneratedUID: FFFFEEEE-DDDD-CCCC-BBBB-AAAA00000000
NFSHomeDirectory: /var/root
Password: *
PrimaryGroupID: 0
RealName:
System Administrator
RecordName: root
RecordType: dsRecTypeStandard:Users
UniqueID: 0
UserShell: /bin/sh

dscl readall ID . RecordName UniqueID /Users . dscl


.
$ dscl . -readall /Users RecordName UniqueID
RecordName: _amavisd amavisd
UniqueID: 83
RecordName: _appowner appowner
UniqueID: 87

RecordName: daemon
UniqueID: 1
RecordName: sam
UniqueID: 501
RecordName: nobody
UniqueID: -2
RecordName: root
UniqueID: 0

, RecordName
sam, dscl search:
$ dscl . -search / RecordName sam
Users/sam
RecordName = (
sam
)

du 689

du
() .
du [] [_]
du (disk usage )
, . du
1024- , .

du . _ ,
.

, (), Linux. , ,
, Linux, OS X.
du , _. du

, .
all
a

, ,

.
blocksize=sz
B sz

sz , .
.V.1. .
H (si) h (human-readable). (Linux)

total c

.
( ) ,
, ,
, . , , ,
. , , . (Linux).
dereferenceargs
D

,
. (OS X)

(human readable ). ,
, K (), M () G () .
1000. (Linux)
si H

( ). ,
, , ,
H

690 V.
. ,
, , . , , .
chgrp, H L. H
D. (OS X)
, , K (), M () G () . 1024.

humanreadable
h

1- .

, ,
, , . .
, , .
P (no-dereference).
chgrp, H L.
dereference
L

1- .

, ,
, , .
.
, , .
.
chgrp, .
nodereference
P

summarize
s

,
,

.
one-filesystem x

,
, .

du
.
.
$ du
26
4
47
4
12
105

./Postscript
./RCS
./XIcon
./Printer/RCS
./Printer
.

(105) ,
, .

, , du .

echo 691

,
du,
. , s (summarize ),
du /usr,
:
$ du -s /usr/*
4
/usr/X11R6
260292 /usr/bin
10052
/usr/games
7772
/usr/include
1720468 /usr/lib
105240 /usr/lib32
0
/usr/lib64
du: cannot read directory '/usr/local/lost+found': Permission denied
du: '/usr/local/lost+found':

130696 /usr/src

c (total) du
, :
$ du -sc /usr/*
4
/usr/X11R6
260292 /usr/bin

130696 /usr/src
3931436 total

s (summarize), h (human-readable)
c (total):
$ du -shc /usr/*
4.0K
/usr/X11R6
255M
/usr/bin
9.9M
/usr/games
7.6M
/usr/include
1.7G
/usr/lib
103M
/usr/lib32

128M
/usr/src
3.8G
total

, /usr.
/dev/null
.
$ du -hs /usr 2>/dev/null
3.8G
/usr

echo
.
echo []

692 V.
echo , , . Bourne

Again


TC
Shells

, echo.

,
, , .
.
. ,
, .

tcsh echo
-, , n.
echo_style man-, tcsh.
tcsh n, -, , E e.
-, , \n.
bash.
E

-, , \n.
bash.
e

echo.
-, ,
echo. echo
. (Linux)
help

, .

-,
, bash- echo echo.
echo
. echo
5, *
. 158.
echo
- ,
(.V.14). -, ,
echo bash echo,
-e. tcsh echo e
.

echo 693
V.14. -,

\a

\c

\n

\t

\v

\\

, echo. echo (/bin/echo), echo,


bash tcsh, , tcsh e .
$ echo " ."
.
$ echo -n " ."
.$ echo hi
hi
$ echo -e " \v ."

.
$

-,
\c. echo. \c, \c c.
, \c, echo,
. bash
e.
tcsh, .
$ echo -e .\c
.c
$ echo -e ' .\c'
.$
$ echo -e " .\c"
There is no newline after this line.$
$ echo -e .\\c
.$
$ tcsh
tcsh $ echo -e ' .\c'
.$

e \c n.

694 V.

expr
.
expr
expr .
, ,
. , .

, .
, . , (, *), .
expr
.
.
.
(). ,
.
(^)
. expr ,
. expr , .
:

()
()
%
( )
, 0 9 . ,

.
*
/

()

()
, .
( )
<
<=
( )
()
= ==
!=
( )
( )
>=
>
( )
, .
, ( ASCII-).
+

expr 695

, . expr
1 (), , 0 ()
.
()
. 0 , expr .
0 (). .
&

()
. 0 ,
expr .
. .
|

expr 0 (),
-, 0, 1, 0, 2,
.
expr
,
( ). , expr
(, +). ,
0 9 ,
expr . ,
, expr . , . ,
.

expr , .
. -
:
$ expr 17 + 40
57
$ expr 10 - 24
-14
$ expr -17 + 20
3
$ expr 5.3 + 4
expr: non-numeric argument

expr:


(*), (/) (%).
( ), (

696 V.
). ,
.
$ expr 5 \* 4
20
$ expr 21 / 7
3
$ expr 23 % 7
2

, . ,
:
$ expr 2 \* 3 + 4
10
$ expr 2 \* \( 3 + 4 \)
14


.
, expr 0,
, 1, .
$ expr fred == sam
0
$ expr sam == sam
1

, , .
, , expr 1.
$ expr fred \> sam
0
$ expr fred \< sam
1
$ expr 5 \< 7
1

5 m. ,
expr , expr ,
. , m
, expr 5 .
ASCII ( ) m 5. ASCII m 109, 5 53, expr
.
$ expr 5 \< m
1

,
. expr
(4).
$ expr abcdefghijkl : abcd
4

file 697

& 0, ; :
$ expr '' \& book
0
$ expr magazine \& book
magazine
$ expr 5 \& 0
0
$ expr 5 \& 6
5

| , ;
:
$ expr '' \| book
book
$ expr magazine \| book
magazine
$ expr 5 \| 0
5
$ expr 0 \| 5
5
$ expr 5 \| 6
5

file
.
file [] _
file .

_
, file. _ ,
, .

FILE, MAC OS X,

file, ( ), MacOSX,
Linux.

698 V.
filesfrom= f

,
. .

, ,
, , . , , . , POSIXLY_CORRECT ( )
.
nodereference
h

help
mime I
mime i

.
MIME. (OS X)
MIME. (Linux)

, ,
, , . , ,
.
, POSIXLY_CORRECT.
dereference
L

uncompress
z

(zip). , .

file 5000 . , Linux-,


file :
archive
ascii text
c program text
commands text
core file
cpio archive
data
directory
ELF 32-bit LSB executable
empty
English text
executable

file :
, ( ) . , stat( ), ,
.
. ,
, , . file
man-. file
.

find 699

:
/etc/Muttrc:
ASCII English text
/etc/Muttrc.d:
directory
/etc/adjtime:
ASCII text
/etc/aliases.db:
Berkeley DB (Hash, version 9, native byte-order)
/etc/at.deny:
writable, regular file, no read permission
/etc/bash_completion:
ASCII Pascal program text
/etc/blkid.tab.old:
Non-ISO extended-ASCII text, with CR, LF line terminators
/etc/brltty.conf:
UTF-8 Unicode C++ program text
/etc/chatscripts:
setgid directory
/etc/magic:
magic text file for file(1) cmd
/etc/motd:
symbolic link to `/var/run/motd'
/etc/qemu-ifup:
POSIX shell script text executable
/usr/bin/4xml:
a python script text executable
/usr/bin/Xorg:
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), for GNU/
Linux
2.6.8, dynamically linked (uses shared libs), stripped
/usr/bin/debconf:
perl script text executable
/usr/bin/locate:
symbolic link to `/etc/alternatives/locate'
/usr/share/man/man7/hier.7.gz: gzip compressed data, was "hier.7", from Unix, last
modified: Thu Jan 31 03:06:00 2008, max compression

find
, .
find [_] [] []
find , .

_ , find
. _ , find
.

find . find
( , ,
).
Mac
OS

x, find ,
_. Linux
xdev.
, .
find _
, . ,
print.
, , :
, . or o ,

700 V.
, : , , ( ).
, .
, find
.
,
find.
find , , .
. .
, , .

( ). ,
, , ,
. ,
, , . , , .
chgrp, H L.
H

, , , , .
. , , . chgrp, H L.
(dereference)
L

( ). ,
, , , .
. ,
, .
. chgrp, .
P

find ,
( ), _. Linux xdev. (OS X)
x

. n ,
+n ( n), n ( n) n ( n).
anewer _
(
accessed
newer

). , _.

find 701
atime n

(access time ). ,
n .
find
.
depth

,
find
. find
cpio, depth cpio
( , cpio preservemodificationtime).
. ,
cpio.
exec \;
,
0 (, [true]).
. ({}) . exec
, ,
. .
, xargs.
group
,
. ID .
inum n
, i- n.
links n
, n .
mtime n
(modify time ).
, n .
name _
, _ . _ (*, ? []),
.
newer _
,
_.
nogroup

,
, .

702 V.
nouser

, , .
ok \;
exec, ,
, , , ,
y Y.
perm []
, ,
. (),
. ,
644, 755 .
(+),
. ,
.
,
(. , chmod).
print

. , find
. print , find , _.
, find
, .
, ,
print. (. .)
size n[c|k|M|G]
,
n 512- .
n c , k , M G .
type _
, , _. _ :
b ;
c ;
d ;
f ;
l ;
p FIFO ( named pipe);
s

find 703
user

, , .
ID .
xdev

,
find , _. mount. Mac
OS X x. (Linux)

, x y
, , y , x. ( ),
find x, , find .
( , ) x [ ]
y.
$ find dir x y

y,
x. ,
find . (
, ) x y. x,
y, .
$ find dir x -or y

find
. , find . find ,
, .
print find .
dir ( ), ,
x:

$ find dir -print x

dir,
x:
$ find dir x -print

print , .
, :
$ find dir x

704 V.

, a.
, , , .
cpio pax (.
).

find , :
$ find

,
a. .
a*
, .
$ find . -name 'a*'

print. _, find .
, , :
$ find -name 'a*'

cpio,
, /dev/sde1.
,
(>). find
( /), (typef),
(mtime 1), , .o (! name ' *.o'). ( .o ,
, .)
$ find / -type f -mtime -1 ! -name '*.o' -print |
> cpio -oB > /dev/sde1

,
core junk :
$ find . \( -name core -o -name junk \) -print -exec rm {} \;

, exec, ,
.
. find
(.)
core (name core) (o) junk (name junk) [ ,
] () (print) () (exec rm
{ }).

find 705

find grep , .

. finder , ,
. type f find grep
, .
$ cat finder
find . -type f -exec grep -l "$1" {} \;
$ finder Executive Meeting"
./january/memo.0102
./april/memo.0415

Executive Meeting finder


, : ./january/memo.0102 ./april/memo.0415.
(.) , january april . grep recursive
, finder.
, 100 (size +100)
, (atime +5).
find , (okrm{}).
y () n (). rm ,
.
$ find /home/max /home/hls -size +100 -atime +5 -ok rm {} \;
< rm /home/max/notes >? y
< rm /home/max/letter >? n

/home/max/memos Sam /home/sam/memos.


find follow ,
.
$ ls -l /home/max
lrwxrwxrwx 1 max
-rw-r--r-- 1 max

pubs
pubs

17 Aug 19 17:07 memos -> /home/sam/memos


5119 Aug 19 17:08 report

$ find /home/max -print


/home/max
/home/max/memos
/home/max/report
/home/max/.profile
$ find /home/max -follow -print
/home/max
/home/max/memos
/home/max/memos/memo.817
/home/max/memos/memo.710
/home/max/report
/home/max/.profile

706 V.

finger
.
finger [] [_]

finger , ,
. finger , _ ,
finger. finger ,
.

finger (s) , . _
finger (l) .
_ .
at (@), finger ,
@ , .
@, finger , .

(long ). (
_).
l

m
(match ). _,
, ,
_.
_ , .
p
( plan, project pgpkey). .plan, .project .pgpkey. -, ,
, .
, ,
.
s
(short ). ( ,
_).

, finger,
, , , ,

finger 707

,
.
finger ~/.plan, ~/.project ~/.pgpkey, . ,
(,
, , , PGP-),
.
, finger, ,
w, , ,
, ,
,
.
, finger .

Linux finger .
finger
, . Linux-,
( ,
).
- , .
, ,
. , ,
. . finger: ,
. 88.
~/.nofinger finger
, . ,
finger , ,
fingerd .nofinger (

).

,
:
$ finger
Login
max
hls
sam

Name
Max Wild
Helen Simpson
Sam the Great

Tty
Idle
tty1
13:29
*pts/1 13:29
pts/2

Login
Jun 25
Jun 25
Jun 26

Time Office Office Phone


21:03
21:02 (:0)
07:47 (bravo.example.com)

(*) Helen (TTY), ,


-

708 V.
(. mesg: . 91).
,
.
finger kudos:
$ finger
[kudos]
Login
max
roy

@kudos
Name
Max Wild
Roy Wong

Tty
tty1
pts/2

Idle Login Time


Office
23:15 Jun 25 11:22
Jun 25 11:22

Office Phone

$ finger watson@kudos
[kudos]
Login: max
Name: Max Wild
Directory: /home/max
Shell: /bin/zsh
On since Sat Jun 25 11:22 (PDT) on tty1, idle 23:22
Last login Sun Jun 26 06:20 (PDT) on ttyp2 from speedy
Mail last read Thu Jun 23 08:10 2005 (PDT)
Plan:
For appointments contact Sam the Great, x1963.

fmt
.
fmt [] [_]
fmt
.

fmt _ .
(), fmt .

, (), Linux. , ,
, Linux, OS X.
split-only
s

. (Linux)

. (OS X)

taggedparagraph
t

, . (Linux)

n , . . (OS X)
t n

fmt 709
uniformspacing
u

,
, . (Linux)

n . fmt
75.
n.
width=n
w n

fmt . .

, , , vim. , vim ,
!}fmt 60. ,
fmt,
60. , u.

, fmt
. w 50 50.
$ cat memo
One factor that is important to remember while administering the dietary
intake of Charcharodon carcharias is that there is, at least from
the point of view of the subject,
very little
differentiating the prepared morsels being proffered from your digits.
In other words, don't feed the sharks!
$ fmt -w 50 memo
One factor that is important to remember while
administering the dietary intake of Charcharodon
carcharias is that there is, at least from the
point of view of the subject, very little
differentiating the prepared morsels being
proffered from your digits.
In other words, don't feed the sharks!

split-only.
, 50. fmt .
$ fmt -w 50 --split-only memo
One factor that is important to remember while
administering the dietary
intake of Charcharodon carcharias is that there
is, at least from
the point of view of the subject,

710 V.
very little
differentiating the prepared morsels being
proffered from your digits.
In other words, don't feed the sharks!

fsck
.
fsck [] [__]
fsck , . ,
.
fsck
Linux
, OS

diskutil. (Linux)

A __ fsck
() , /etc/fstab.
A __ fsck
, , , /etc/fstab.
s.
__ , . ,
(, /dev/hda2), ,
/etc/fstab, , (, /usr). _
_
/etc/fstab (, LABEL=home).

fsck , ,
, fsck (, ext2, ext3, msdos,
reiserfs). ,
.


A
(all ).
, /etc/fstab.
s. __ ;
t. fsck

fsck 711

(
), a, p n.
(no ). ,
.
, , fsck
- .
N

(root-skip ). A, .
,
-.
R

(serial ). fsck
. fsck ,
. fsck
. ,
. , a, p N ( n,
).
s

(title ). fsck .

(filesystem type ).
, () .
A fsck ,
/etc/fstab fstype. ext2, ext3, ext4, msdos reiserfs.
NFS .
t fstype

(verbose ). ,
, .
V

,
,
. i- (.
ls i- . 129).
$ ls -i /sbin/*fsck*
63801 /sbin/dosfsck 63856 /sbin/fsck.cramfs
63763 /sbin/e2fsck
63763 /sbin/fsck.ext2
63780 /sbin/fsck
63763 /sbin/fsck.ext3

63801 /sbin/fsck.msdos
63801 /sbin/fsck.vfat

, , man :
$ /sbin/fsck.ext3
Usage: /sbin/fsck.ext3 [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]
[-I inode_buffer_blocks] [-P process_inode_size]
[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]

712 V.
[-E extended-options] device
Emergency help:
-p
Automatic repair (no questions)
( )
-n
Make no changes to the filesystem
-y
Assume "yes" to all questions
yes
-c
Check for bad blocks and add them to the badblock list

-f
Force checking even if filesystem is marked clean
,

, ext2
ext3:
(automatic ). p,
.
a

(force ). fsck .
fsck
.
fsck, .
, ext2, ext3
ext4 tune2fs.
f

(no ). , N. .
n

(preen ). ,
.
, fsck . fsck , , ,
. Linux p A.
p

(interactive ). .
. .
r

(yes ). , fsck
. ,
fsck ,
.
y

fsck fsck_hfs
Mac OS X 10.5 . Apple
diskutil (. ).
fsck - .

fsck 713

, fsck ,
:
# fsck -f /dev/sdb1
fsck 1.40.8 (13-Mar-2008)
e2fsck 1.40.8 (13-Mar-2008)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 710/4153408 files (10.1% non-contiguous), 455813/8303589 blocks

fsck ,
. a, p, y n,
fsck .
fsck , , . ,
.
, fsck , , .
.

fsck /etc/fstab,
, . (0) ,
. (1) , ; . (2)
, ,
.

mkfs (. ), fsck ,
. , fsck
e2fsck ext2 ext3.
e2fsck man-.
, fsck, fsck.,
.
,

fsck.
fsck

fsck , .
Linux ,
fsck. fsck
, (

714 V.
remount mount) -, fsck
A, R p.
lost+found
, , fsck , .
, lost+found, -

, .
i-. fsck
, lost+found . ,
/, /usr /home, lost+found: /lost+found, /usr/
lost+found /home/lost+found. lost+found , fsck i-
. ext2/ext3/ext4 mkfs (. ) lost+found
. , ,
ext2/ext3/ext4 mklost+found.
. 500
lost+found touch (. )
500 lost+found, rm
.

.V.15 .
fsck ,
. , ,
. ,
,
.
V.15. fsck
()

fsck

1 Checking inodes, blocks, and sizes


(- i-
i-, )
2 Checking

directory structure
( )

,
i-, 1

3 Checking directory connectivity (


)

, , lost+found

4 Checking reference counts


( )

, ,
lost+found, ,
,
i-

ftp 715
()

fsck

5 Checking group summary information


( )

.

, 6

6 Salvage free list (


)

5 ,
6


fsck , :
*****File System Was Modified*****

ext2/ext3/ext4, fsck
:
: / ( ), /
, ,
. .

ftp
.
ftp [] [_]
ftp
File Transfer Protocol (FTP),
, .
FTP- (,
) .
FTP
FTP
.
ftp

, .
OpenSSH, ftp sftp.
FTP-, ,
scp (. ). scp


, .

_ ,
FTP- (, ftpd, vsftpd sshd)
.

716 V.

i
(interactive ). mget mput. . prompt.

(no automatic login )


.
n

(passive mode ). ftp .


. passive.
p

(verbose ). ftp.
_ .
v

ftp . . ftp> ,
.
![]
( ) . ftp CONTROL+D exit. ,
, ftp ftp>. , ftp
, , ftp, , ftp,
. , ,
, ftp, ftp-
lcd, : cd . . lcd ( cd).

ASCII.
, , .
, , DOS MS Windows. ascii , cr ON.
ascii

binary
. , -ASCII ( )
. ASCII-,
.
bye

ftp. quit.
cd
_

_.

ftp 717
close

ftp.

(carriage return ).
ASCII-. .
ascii.
cr

.
, .
, .
dir [
[]]

get
_

[_

_
_. _ ftp _.
_ _ .

glob
mget
mput (Globbing on Globbing off).
help

, ftp
.

(local change directory ).


; _.
, ( , cd,
).
lcd
[_
]

ls [
[]]

dir, .

(multiple get ). get


.
(. glob). . prompt.
mget _
_

(multiple put ). mput .


(. glob). . prompt.
mput _
_

. ,
.
open

(PORT )
(PASV) . .
. 718.
passive

mget mput ftp ( )


.
(Interactive mode off Interactive mode on).
prompt

718 V.
put
_

[_
]

_
_. _ ftp _.
_ _ .

ftp . !pwd.
pwd

ftp.
bye.
quit

. get,
ftp . reget .
reget
_

ftp , _. _, ftp .

user [_
]

Linux Mac OS X ftp


,
FTP
-. FTP- ,

- (, ftp://ftp.ibiblio.org/pub/Linux http://www.ibiblio.org/pub/Linux).
FTP- .
ftp
, ftp ,
UNIX/Linux (
).
, ,
,
. , , ftp anonymous.

, , .
. , .
, , pub.

FTP

FTP-
PASV (, ), PORT () . -
.
FTP- , , ,

ftp 719

. ( 20), ( ).
- .
, , NAT, , , .
FTP-
, FTP-. ~/.netrc . FTP- ftp ~/.netrc ,
. ~/.netrc
:
machine login _ password
, _ , . default
machine, , ~/.netrc. default .
~/.netrc :

$ cat ~/.netrc
machine bravo login max password mypassword
default login anonymous password max@example.com

, .netrc,
,
. netrc
man-.

ftp,
Max
FTP- bravo
. Max ftp bravo, ftp-
, .
max ftp
max. max Max .
bravo watson,
Name(bravo:max): watson. Password:
Max
() , FTP-
, (Using binary
mode to transfer files). ftp Max
ASCII-, .
$ ftp bravo
Connected to bravo.
220 (vsFTPd 2.0.7)
530 Please login with USER and PASS.

720 V.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (bravo:max): watson
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
Max ftp- ls

,
bravo. memos .
ls cd
ftp> ls
227 Entering Passive Mode (192,168,0,6,79,105)
150 Here comes the directory listing.
drwxr-xr-x
2 500
500
4096 Oct 10 23:52 expenses
drwxr-xr-x
2 500
500
4096 Oct 10 23:59 memos
drwxrwxr-x
22 500
500
4096 Oct 10 23:32 tech
226 Directory send OK.
ftp> cd memos
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,0,6,114,210)
150 Here comes the directory listing.
-rw-r--r-1 500
500
4770 Oct 10
-rw-r--r-1 500
500
7134 Oct 10
-rw-r--r-1 500
500
9453 Oct 10
-rw-r--r-1 500
500
3466 Oct 10
-rw-r--r-1 500
500
1945 Oct 10
226 Directory send OK.

23:58
23:58
23:58
23:59
23:59

memo.0514
memo.0628
memo.0905
memo.0921
memo.1102

Max ftp- get memo.1102


.
,
ASCII
. , . Max
memo.1114 .
memos.
get put
ftp> get memo.1102
local: memo.1102 remote: memo.1102
227 Entering Passive Mode (192,168,0,6,194,214)
150 Opening BINARY mode data connection for memo.1102 (1945 bytes).
226 File send OK.
1945 bytes received in 7.1e-05 secs (2.7e+04 Kbytes/sec)
ftp> put memo.1114

ftp 721
local: memo.1114 remote: memo.1114
227 Entering Passive Mode (192,168,0,6,174,97)
150 Ok to send data.
226 File receive OK.
1945 bytes sent in 2.8e-05 secs (6.8e+04 Kbytes/sec)


Max
, , memos bravo, .
ls, , ,
ftp . ftp
ftp , Max ftp open bravo
. ftp- cd
memos.
ftp> ls
421 Timeout.
Passive mode refused.
ftp> open bravo
Connected to bravo (192.168.0.6).
220 (vsFTPd 1.1.3)

ftp> cd memos
250 Directory successfully changed.

open

Max ,
, . ftp-
mkdir, , Max
.
(!), mkdir memos.hold mkdir
, memos.hold
. (
!pwd.) , memos.hold , Max
. !cd memos.
hold, Max,
, cd
, ,
ftp. ftp
lcd ( cd), ftp
.

lcd

(
cd)

ftp> !mkdir memos.hold


ftp> lcd memos.hold
Local directory now /home/max/memos.hold

memos memos.
hold Max ftp- mget (
multiple

get
), (*). ftp
, Max ,
, n

722 V.
CONTROL+C . ,
mget.
Max ftp- prompt, ( , , , ).
, mget *, ftp , .
, Max quit
, ftp .
ftp> mget *
mget memo.0514? n
mget memo.0628? CONTROL+C
Continue with mget? n
ftp> prompt
Interactive mode off.

mget
prompt

ftp> mget *
local: memo.0514 remote: memo.0514
227 Entering Passive Mode (192,168,0,6,53,55)
150 Opening BINARY mode data connection for memo.0514 (4770 bytes).
226 File send OK.
4770 bytes received in 8.8e-05 secs (5.3e+04 Kbytes/sec)
local: memo.0628 remote: memo.0628
227 Entering Passive Mode (192,168,0,6,65,102)
150 Opening BINARY mode data connection for memo.0628 (7134 bytes).
226 File send OK.

150 Opening BINARY mode data connection for memo.1114 (1945 bytes).
226 File send OK.
1945 bytes received in 3.9e-05 secs (4.9e+04 Kbytes/sec)
ftp> quit
221 Goodbye.

gawk
.
gawk [] [] [_]
gawk [] f _ [_]
AWK
, ( ),
. ,

. , AWK
, : , ,
AWK, , .

GAWK 12
AWK awk, gawk mawk 12.

gcc 723

gcc
C C++.
gcc [] _ []
g++ [] _ []

Linux

Mac
OS

, , , C, GNU
C- gcc. , , g++,
C++. gcc g++
,

.
, .
o, a.out.
gcc g++ GCC, GNU
Compiler Collection,

C
,
C
++,
Objective

C
,
Fortran
,
Java
Ada

, , . gcc.gnu.org.

GCC G++
gcc,
g++.

_ , gcc.

Gcc ,

C, , ,
.V.16. gcc
, , ,
a.out. gcc
,
.o
. gcc , .
.
, , gcc .
(compile ). . () , .o.
c

D
[=]

#define .

724 V.
, . , DLinux
#define Linux,
DMACH=i586 , #define MACH i586.
(everything ).
, ( ) . , .i
C, .ii C++.
E

gcc - , .
fpic

GNU C-
, .
( )
. gcc,
.
fwritablestrings

(gdb). .
, gdb.
,
.
g


.
.
I

larg
( l ). /lib /usr/lib libarg.a. , gcc
. arg
, . , lm libm.a.
: , ,
, . ,
. ,
, . libarg.a
, L.

,
, l. ,
L, , gcc .
L

(output ). ,
, a.out.
o

(optimize ). () , . n On

gcc 725

0, 1, 2 3 ( 06, Linux). n 1. n, ,
gcc. O0.
,
gcc O. info, gcc.
GNU C- C , ANSI- C.
gcc ,
C.
pedantic

, gcc.
, .
Q

(suppress ).
.
.s.
S

traditional
gcc C, C,
(Kernighan Ritchie). , C ( ANSI- C).

gcc
. .
Wall


, GNU C-.
info-, gcc. o
, ,
,
. o , gcc, acode, pgm.s,
:
$ gcc -S -o acode pgm.c

lint, UNIX-, Linux Mac OS X .


Wall lint.
. V

.16 ,
C
- .

726 V.
V.16.

.a

.c

.C, .cc .cxx

C++

.i

C,

.ii

C++,

.o

.s

.S


C- compute.c.
a.out. gcc .
$ gcc compute.c

C ( O).
. o gcc
compute.
$ gcc -O -o compute compute.c

,
C

, . progo.
$ gcc -o progo procom.c profast.s proout.o

gcc , /lib/libm.a himath a.out:


$ gcc himath.c -lm

C- topo.c , (
Wall), ANSI- C ( pedantic). g
, ,
topo o topo. O3
.
, C-,
. pedantic, Wall.
$ gcc -Wall -g -O3 -pedantic -o topo topo.c
In file included from topo.c:2:
/usr/include/ctype.h:65: warning: comma at end of enumerator list
topo.c:13: warning: return-type defaults to 'int'
topo.c: In function 'main':

GetFileInfo (OS X) 727


topo.c:14: warning: unused variable 'c'
topo.c: In function 'getline':
topo.c:44: warning: 'c' might be used uninitialized in this function

, X11,
I L, gcc, . , gcc
X11:
$ gcc -I/usr/X11R6/include plot.c -L/usr/X11R6/lib -lX11

GetFileInfo (OS X)
.
GetFileInfo []
GetFileInfo , ,
, .

, GetFileInfo .

GetFileInfo SetFile (. ). GetFileInfo , , ,


. . GetFileInfo
, .
, .
(attribute ).
. 1, , 0,
. a, .
a

(creator ). .
,
.
c

d
(date ). 24- // ::.

(modification ).
// :: 24- .
m

( ). ,
, , ,
P

728 V.
. . , ,
. chgrp, .
(type ). .
, .
t

GetFileInfo avbstclinmedz,
, .

SetFile (. ). Mac OS X (.
. 114) chmod
chown, ,
ls stat, .
. GetFileInfo
, .

GetFileInfo .
$ GetFileInfo picture.jpg
file: "/private/tmp/picture.jpg"
type: "JPEG"
creator: "GKON"
attributes: avbstClinmedz
created: 07/18/2005 15:15:26
modified: 07/18/2005 15:15:26

C,
, . c Finder .
a, :
$ GetFileInfo -a /Applications/Games/Alchemy/Alchemy
avBstclInmedz

, b i.
GetFileInfo .
bash-
for (. forin . 429).
echo , GetFileInfo
:

grep 729
$ for i in *
> do echo -n "$i: "; GetFileInfo -c "$i"
> done
Desktop: Desktop is a directory and has no creator
Desktop: Desktop
Documents: Documents is a directory and has no creator
Documents: Documents

aa: ""
ab: ""

grep
.
grep [] [_]
grep ,
, -
. grep ,
, , ,
. , ,
.

, , , . , .
_
, grep. r _
.

grep , , .
, grep ,
, .


.
, grep G, grep.

730 V.
(extended ). . grep E egrep. . .
E

(fixed ). . grep F fgrep.


F

(grep). . ,
.
G


grep , .V.2.

GREP MAC OS X
grep, (- -),
Linux, OS X.

,
.
count c

context=n
C n

n , , .

file=
f

, , , .

no-filename
h

, .


, .
, (
, ).
ignore-case
i

( l ; list ).
, .
,
.
files-withmatches
l

max-count=n
m n


n , .

line-number
n

.
.

quiet

,
.

silent
q
recursive
r R

, _, , .

grep 731

(silent ). ,
_
.

no-messages
s

,
.
grep , .
invert-match
v

. , .
word-regexp
w

line-regexp
x

grep 0, , 1, , 2, grep .
, grep, .
egrep ( grep E) , , . fgrep ( grep F )
, , .
GNU grep, Linux Mac OS X,
. egrep
, grep. info-, grep.
egrep fgrep

, :
testa, testb testc.
testa
aaabb
bbbcc
ff-ff
cccdd
dddaa

testb
aaaaa
bbbbb
ccccc
ddddd

testc
AAAAA
BBBBB
CCCCC
DDDDD

grep , . testa,
, bb:
$ grep bb testa
aaabb
bbbcc

732 V.
v .
testa, bb:
$ grep -v bb testa
ff-ff
cccdd
dddaa

n :
$ grep -n bb testa
1:aaabb
2:bbbcc

grep . , . ,
, .
$ grep bb *
testa:aaabb
testa:bbbcc
testb:bbbbb

bb w, grep ,
bb :
$ grep -w bb *
$

, grep, .
bb, , grep
testc BBBBB, . i
, ,
:
$ grep -i bb *
testa:aaabb
testa:bbbcc
testb:bbbbb
testc:BBBBB
$ grep -i BB *
testa:aaabb
testa:bbbcc
testb:bbbbb
testc:BBBBB

c , :
$ grep -c bb *
testa:2
testb:1
testc:0

f , . gfile ,
, grep , gfile:
$ cat gfile
aaa

grep 733
bbb
$ grep -f gfile test*
testa:aaabb
testa:bbbcc
testb:aaaaa
testb:bbbbb

text2 , , st,
(.* ),
ing:
$ grep 'st.*ing' text2

^, , . n,
^ , :
$ grep -n '^' testa
1:aaabb
2:bbbcc
3:ff-ff
4:cccdd
5:dddaa


#include C,
. h grep .
sort *.c, #include.
sort , . uniq c -

, ,
.
$
9
2
1
6
2
2
2
3

grep -h '#include' *.c | sort | uniq -c


#include "buff.h"
#include "poly.h"
#include "screen.h"
#include "window.h"
#include "x2.h"
#include "x3.h"
#include <math.h>
#include <stdio.h>

vim , , Sampson. $() (. . 358),


grep vim ,
:
$ vim $(grep -l 'Sampson' *)

734 V.
, , , ,
.
, ,
.

gzip
.
gzip [] [_]
gunzip [] [_]
zcat [_]
gzip , gunzip , gzip, zcat , gzip.

_ , ,
. _
recursive, gzip gunzip
. recursive gzip
gunzip
. _ (), gzip . stdout gzip gunzip
.
gunzip, gzip.

gzip, gunzip zcat ,


. V.2.

GZIP, GUNZIP ZCAT MAC OS X



gzip, gunzip zcat, ( ),
Linux, OS X.

,
_.gz.

stdout c

decompress

uncompress
d

, gzip. , gzip, gunzip.

force f

(-

) .

gzip 735

_ , . verbose.
list l

.
n 1 9; 1 , 9
. 6. fast best 1 9 .
fast n

best

quiet q

recursive
r

, _, () .

. ,
.

test t

, .

verbose v

, ,
, .
gzip , .gz. ,
fname fname.gz ,
stdout (c), .
fname gunzip fname.gz.
gzip
. - .
, ( n);
6070 .
, , , ,
gzip gunzip.
, gzip . , gzip
. force
.

stdout (c) gzip , _.


bzip2 (. ) ,
gzip.
gzip, gunzip ,
, compress.

736 V.
,
gzip, , ,
, gzip . gzip ,
.gz,
.
tar z (. , tar),
gzip.
.
.
, cat, , gunzip _
.
zcat _

, diff (. ), , 1 2 , , gunzip. zdiff , diff.


2, zdiff 1 .
zdiff
[]
1
[2]

zless _

, less, , gunzip _

gzip . gunzip
. , ,
:
$ ls -l
total 175
-rw-rw-r-- 1 max group 33557 Jul 20 17:32 patch-2.0.7
-rw-rw-r-- 1 max group 143258 Jul 20 17:32 patch-2.0.8
$ gzip *
$ ls -l
total 51
-rw-rw-r-- 1 max group 9693 Jul 20 17:32 patch-2.0.7.gz
-rw-rw-r-- 1 max group 40426 Jul 20 17:32 patch-2.0.8.gz
$ gunzip patch-2.0.7.gz
$ ls -l
total 75
-rw-rw-r-- 1 max group 33557 Jul 20 17:32 patch-2.0.7
-rw-rw-r-- 1 max group 40426 Jul 20 17:32 patch-2.0.8.gz

,
Sam, cpio (. ).
, /dev/sde1,
gzip.
$ find ~sam -depth -print | cpio -oBm | gzip >/dev/sde1

head 737

head
.
head [] [_]
head () .
, .

_
, , head. , head
. , head .

Linux head , . V.2. ,


(), Linux

. , , ,
Linux, OS X.
n () .
Linux u ,
, , head
(1024 ) k b 512 .
, head , .
bytes=n[u]
c n[u]

n . n n lines n.
n , head ,
n .
lines=n
n n


. (Linux)
quiet q

head .

:
$ cat eleven

738 V.

head :
$ head

eleven

(n 3) :
$ head

-n 3 eleven

:
$ head

-3 eleven

(c 8) :
$ head -c 8 eleven
$

, :
$ head

-n -7 eleven

kill
PID.
kill [] PID-
kill l [_ | _]

kill 739

kill .
.
kill , , kill , : ,
root-, . l (
l
) .

PID-
(
PID
), kill.

l
(list ). . _, _.
_, _.

, _ _ PID-. _
SIG (, SIGKILL KILL). kill (SIGTERM; 15).
_
| _

. killall.
. . 10.5. kill l
.
kill,
Bourne
Again


TC
Shells

kill. , .
/bin/kill, kill.
, , .

PID
-. PID-, ps.
, KILL ( 9).
, KILL.
( ) kill PID-
. (%), , ,
.
, , ,
kill 9 0.
ROOT-, KILL 9 0 KILL 0

root- kill 9 0, .

740 V.

, compute
, kill, :
$ compute &
[2] 259
$ kill 259
$
[2]+ Terminated

compute

ps,
PID- , xprog,
kill, xprog TERM:
$ ps
PID TTY
7525 pts/1
14668 pts/1
14699 pts/1

TIME CMD
00:00:00 bash
00:00:00 xprog
00:00:00 ps

$ kill -TERM 14668


$

killall
.
killall [] _
killall , . .
killall , , killall , : ,
root-, .

_
, . .

, (), Linux. , ,
, Linux, OS X.
interactive
i

, . (Linux)

( kill l ).
killall _.
list l

launchctl (OS X) 741


quiet q

, killall -

. (Linux)
, _ _ _. _
SIG (, SIGKILL KILL). kill (SIGTERM; 15).
_
| _

. kill.
. . 10.5. kill l
.
, KILL ( 9).
, KILL.
, ,
ps (. ).

killall, :
$ sleep 60 &
[1] 23274
$ sleep 50 &
[2] 23275
$ sleep 40 &
[3] 23276
$ sleep 120 &
[4] 23277
$ killall sleep
$
[1]
Terminated
[2]
Terminated
[3]- Terminated
[4]+ Terminated

sleep
sleep
sleep
sleep

60
50
40
120

, , root-,
Firefox:
# killall firefox

launchctl (OS X)
(, ) launchd.
launchctl [[] []]
launchctl launchd.

742 V.

, launchctl launchd. .V.17


, , . launchctl ,
. ,
, launchctl .
V.17. launchctl

help

list

, launchd

load [w]

shutdown

start

stop

unload [w]

load unload.
(write ). Disabled, .
Disabled .
w

launchctl launchd,
( ). ,
, , man-,
launchd.plist.
, root-,
launchd, PID 1.
, OS X
launchd. launchd
.

launchctl launchd Mac OS X 10.4.


10.3 init, xinetd cron.

launchctl (OS X) 743

, , root-,
list ,
:
# launchctl list
PID
Status Label
51479
0x109490.launchctl
50515
0x10a780.bash
50514
0x10a680.sshd
50511
0x108d20.sshd
22
0x108bc0.securityd
0
com.apple.launchctl.StandardIO
37057
[0x0-0x4e84e8].com.apple.ScreenSaver.Engine
27860
0x10a4e0.DiskManagementTo
27859
[0x0-0x3a23a2].com.apple.SoftwareUpdate

ntalk. ntalk.plist
launchctl , launchctl ,
Disabled.
# cat /System/Library/LaunchDaemons/ntalk.plist

<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>com.apple.ntalkd</string>

# launchctl load -w /System/Library/LaunchDaemons/ntalk.plist


# cat /System/Library/LaunchDaemons/ntalk.plist

<dict>
<key>Label</key>
<string>com.apple.ntalkd</string>

launchctl . launchctl, quit


CONTROL+D. , root, launchctl , ,
airportd:
# launchctl
launchd% list
PID
Status
8659
1

0
0
0
0

Label
0x10ba10.cron
0x10c760.launchd
com.apple.airport.updateprefs
com.apple.airportd
com.apple.AirPort.wps
0x100670.dashboardadvisoryd

744 V.
0
com.apple.launchctl.System
launchd% stop com.apple.airportd
launchd% quit

less
.
less [] [_]
less .

_ , . _ , less .

less , .V.2.

LESS MAC OS X
less, ( ), Mac OS X,
Linux.

clear-screen
c

(exit ). less
q. .
QUIT-AT-EOF
E

(exit ). E, , less
.
quit-at-eof
e

quit-if-onescreen
F

,
.

, , . , ,
.
ignore-case
i

, , , .
IGNORE-CASE
I

. less ,
, more.

long-prompt
m

less 745

, less , less
.
LINE-NUMBERS
N

( ) .
, .
, less
. , %f less . man-. less , - -
, .
.
prompt=

.
less ,
,
.
squeezeblank-lines
s

n .
.
tabs=n xn

n . .

n . z more
.
window=n
[z]n

less , ,

(+). . . ,
, , less ,
.
+

++
+, , _, .

less more .
more Berkeley UNIX (
Linux). less
more, . ( OS X less more .)
less .
, ,
.

746 V.
, less,
v .
less ,
LESS. ,
less x4 s, bash :
$ export LESS="-x4 -s"

LESS ~/.bash_profile, bash, ~/.login, tcsh.


LESS, less
. , , LESS. LESS
less ,
, man. less
, man ,
PAGER less. , bash
~/.bash_profile :
export PAGER=less

less
. , . man-, less.
n
1. .
nb
nCONTROL+B

(backward ). n . n .

(down ). n . n
. n
n .
nd
nCONTROL+D

(forward ). .
, , .
less , tail f (. ), , less
.
F

(go ). n.
,
. n 1.
ng

(help ). .
less,
.
h H

less 747
n nj

(jump ). n .
n 1.

q :q

less.

n . n
. n
.
nu
nCONTROL+U


. less ,
EDITOR. EDITOR , less vi (,
vim).
v

, nb, ,
n .

nw

ny nk

n . n 1.

n n, , n, ,
z .

nz

n . .

,
_. _
(!), , _. _ (* ), _.
( .) _ at (@), _
.

/_

, ( _). (*) , _ , . at(@)


, _ .

? _


,
, . , { less }.

{ ( [

, ,
, .

} ) ]

. ,
.
CONTROL+L

748 V.
_. n,
n- _.

[n]:n

,
SHELL, sh (
bash ) . (%)
.
, less .
![_
]

memo.txt.
, less,
, :
$ less memo.txt

memo.txt


N .
less ,
procedure.
$ less -Ps" , q " -N +/procedure ncut.icn
28
29
30
31

45
46
47
48
49

procedure main(args)
local filelist, arg, fields, delim
filelist:=[]

# Check for real field list


#
if /fields then stop("-fFIELD_LIST is required.")
# Process the files and output the fields

, q

ln
.
ln [] _ [_]
ln [] __
ln .
,
.

_ ,
. _ .
, _ . _ ,

ln 749
ln _
, _.
__
, . ln
.

, __.

, (), Linux. , ,
, Linux, OS X.
backup b
ln ,
(~) . force. (Linux)

ln , _ .
_. force backup
( Linux), ln _
.
force f

_ ,
_ . y yes, ln _.
n no, ln _
.
interactive
i

symbolic s
. _ _ . . . 130.

. 126. ls l
(. .4.9).
ln .
.
.
. 128.

ln
. ,
. .
. 130.
_ , ln
, force option ( Linux)
i (
interactive).

750 V.

memo2 literature
Zach .
memo2 ( ):
$ ln ~zach/literature/memo2 .

, , .
ln , .
. new_memo:
$ ln ~zach/literature/memo2 new_memo

,
Sam:
$ ln ~zach/literature/memo2 ~sam/new_memo

, .
, chmod.
. ls ld :
$ ln -s /usr/local/bin bin
$ ls -ld bin
lrwxrwxrwx 1 zach zach 14 Feb 10 13:26 bin -> /usr/local/bin


memo1 memo2. memo1 , ln
. i (interactive) ln ,
memo1 .
y yes, ln memo1 .
$ ls -l memo?
-rw-rw-r-1 zach group
-rw-rw-r-1 zach group
$ ln -s memo2 memo1
ln: memo1: File exists
$ ln -si memo2 memo1
ln: replace 'memo1'? y
$ ls -l memo?
lrwxrwxrwx
1 zach group
-rw-rw-r-1 zach group

224 Jul 31 14:48 memo1


753 Jul 31 14:49 memo2
ln: memo1:
ln: 'memo1'?
5 Jul 31 14:49 memo1 -> memo2
753 Jul 31 14:49 memo2

Linux, ln ,
force.

lpr
.
lpr [] [_]
lpq [] [_]
lprm [] [_]

lpr 751

lpr ,
. , .
lprm ,
lpq . . .

_ , lpr .
, , lpr , PostScriptPDF-. _ , lpr .
_
. , lpq.

, , , .
(no header ). () .
, , .
h

( l ). , lpr () .
, .
l


. , , .
Linux- /etc/printcap lpstat t. .
P

r
(remove ). , _
lpr.
# n
n . #, , .

lpr , ,
.
. -

752 V.
. lpq , lpr.
lprm.
lpq ,
. lpq , , .
, , , lpq P lpr. l lpq .
, lpq
, .
lpq

, lpq,
, . ,
lprm. root-, . ,
root-,
. lprm , ( , ),
.
lprm

,
, lpr , ,
PRINTER. , bash,
ps
~/.bash_profile :
export PRINTER=ps

UNIX :
BSD Line Printer Daemon (LPD) System V
Line Printer system (LPR). Linux , UNIX,
Linux ,
. Linux, OS X CUPS.
LPD LPR

CUPS - ,
Internet Printing Protocol (IPP),
, , HTTP. CUPS
,
PostScript-. CUPS System
V BSD IPP, , ,
LPD/LPR, HTTP, SMB JetDirect ().

LPD
, CUPS,
.

CUPS

ls 753

memo2 ,
:
$ lpr memo2

ls deskjet:
$ ls | lpr -Pdeskjet


memo:
$ pr -h "Today's memo" memo | lpr

,
, . Max, ( ).
635 639 lpr; 638
ncut.icn lpr.
.
$ lpq
deskjet is ready
Rank
Owner
active max
1st
max
2nd
max

and printing
Job Files
635 (stdin)
638 ncut.icn
639 (stdin)

Total Size
38128 bytes
3587 bytes
3960 bytes

638 , :
$ lprm 638

ls
.
ls [] [_]
ls . , ,
.

, ls (
) . _ ,
,
. .
_ , ls .
, ,
. , ls
.

754 V.

, (), Linux. , ,
, Linux, OS X.
ls ,
. , ls ,
.
almost-all
A

, a,
.

( , . . 102).
ls ,
_. *
(. * . 158), (
).
all a

-, , , C (.V.18). ,
.
escape b

V.18. -,

\b

BACKSPACE

\n

(NEWLINE)

\r

(RETURN)

\t

(HORIZONTAL TAB)

\v

(VERTICAL TAB)

\\

(BACKSLASH)

ls , (
,
none).
(always), ls .
(auto), ls ,
.
. (Linux)
color
[=]

. ; , , , , .
directory
d

ls 755
e

ACL-. (OS X)

ls ,
. ,
: ( , x), ( , m), ( , l)
( , 1). (Linux)

format=

(/) , (*)
at (@) .

classify F

( ). , , ,
, . , , ,
. , , . chgrp,
H L.
dereferencecommand-line
H

l ,
, K (), M () G ()
. l.
1024. Mac OS X, B (). . si.
humanreadable
h

inode i
i- . l
i- 1,
.

(). , , , ,
. .
, , .
chgrp, H L.
dereference
L

( l ).
. , ,
, , , . , ,
, ,
. ,
h. .
format=long
l

format=
commas
m

, ,
.

( ). ,
, , ,
P

756 V.
. . , ,
. chgrp, . (OS X)
. ,
. ,
, .
hidecontrol-chars
q

recursive
R

reverse r

1024- (Linux) 512- (Mac OS X)


, . . l 1
.
, , , , . , h. Mac OS X
, , BLOCKSIZE (. V).
size s

l ,
, K (), M () G () .
l. 1000. . human-readable. (Linux)
si

sort=time
t

,
.

ls , ASCII , .
:
(X; Linux), (U; Linux), (S),
(u)
(t). time. (Linux)
sort=

time= ls l
.
, atime (u), , ctime (t). ,
sort=time. (Linux)
sort=access
u

, .

ls 757
format=
extension
X

,
. ,
. (Linux)

format=
across x

, ( ).

format=
single-column
1

(). . ls , .

ls ( l format=long) , .4.9. , 10 11 ,
, .
, .V.19.
V.19. , ls

FIFO ( )

,
. .
, .
,
r. , .
.
w, ,
x, . s
, setuid,
. ,
, .
, . s ,
setgid, , S
, setgid , .
. t ,
(sticky bit).
chmod.

758 V.
ACL

- (. (ACL-) . 121) ACL-,


ls l (+),
.
.

4.9, .
. 126.

, .
, , .
, ,
, . ( , ,
du.) , ,
h.

.

ls : , , ls ,
, .
( , ,
) l d.
( l),
, ls ,
. , ls
.
ls
. 157.
color ls
. , , ,
.
/etc/DIR_COLORS. , ls .
, /etc/DIR_COLORS.
/etc/DIR_COLORS ~/.dir_colors . ~/.dir_colors
, /etc/DIR_COLORS.
man- dir_colors dircolors.

ls 759

, ls
, .
():
$ ls
bin calendar
c
execute

letters
shell

ls x, :
$ ls -x
bin
execute

c
letters

calendar
shell

F (/) , ,
(*) at (@) , :
$ ls -Fx
bin/
execute*

c/
letters/

calendar
shell@

l (long ) .
- :
$ ls -l
total 20
drwxr-xr-x
drwxr-xr-x
-rw-r--r--rwxr-xr-x
drwxr-xr-x
lrwxrwxrwx

2
2
1
1
2
1

sam
sam
sam
sam
sam
sam

pubs 4096 May 20


pubs 4096 Mar 26
pubs 104 Jan 9
pubs
85 May 6
pubs 4096 Apr 4
sam
9 May 21

09:17
11:59
14:44
08:27
18:56
11:35

bin
c
calendar
execute
letters
shell -> /bin/bash

a (all ) , ,
:
$ ls -a
.
..
.profile

bin
c
calendar

execute
letters
shell

a l
, . - :
$ ls -al
total 32
drwxr-xr-x
drwxrwxrwx
-rw-r--r-drwxr-xr-x
drwxr-xr-x
-rw-r--r--rwxr-xr-x

5
3
1
2
2
1
1

sam
sam
sam
sam
sam
sam
sam

sam
sam
sam
pubs
pubs
pubs
pubs

4096
4096
160
4096
4096
104
85

May
May
May
May
Mar
Jan
May

21
21
21
20
26
9
6

11:50
11:50
11:45
09:17
11:59
14:44
08:27

.
..
.profile
bin
c
calendar
execute

760 V.
drwxr-xr-x 2 sam pubs 4096 Apr 4 18:56 letters
lrwxrwxrwx 1 sam sam
9 May 21 11:35 shell -> /bin/bash

r (reverse ) ls
:
$ ls -ral
total 32
lrwxrwxrwx
drwxr-xr-x
-rwxr-xr-x
-rw-r--r-drwxr-xr-x
drwxr-xr-x
-rw-r--r-drwxrwxrwx
drwxr-xr-x

1
2
1
1
2
2
1
3
5

sam
sam
sam
sam
sam
sam
sam
sam
sam

sam
pubs
pubs
pubs
pubs
pubs
sam
sam
sam

9
4096
85
104
4096
4096
160
4096
4096

May
Apr
May
Jan
Mar
May
May
May
May

21
4
6
9
26
20
21
21
21

11:35
18:56
08:27
14:44
11:59
09:17
11:45
11:50
11:50

shell -> /bin/bash


letters
execute
calendar
c
bin
.profile
..
.

, , t l:
$ ls -tl
total 20
lrwxrwxrwx
drwxr-xr-x
-rwxr-xr-x
drwxr-xr-x
drwxr-xr-x
-rw-r--r--

1
2
1
2
2
1

sam
sam
sam
sam
sam
sam

sam
9 May 21 11:35
pubs 4096 May 20 09:17
pubs
85 May 6 08:27
pubs 4096 Apr 4 18:56
pubs 4096 Mar 26 11:59
pubs 104 Jan 9 14:44

shell -> /bin/bash


bin
execute
letters
c
calendar

r t ,
, :
$ ls -trl
total 20
-rw-r--r-drwxr-xr-x
drwxr-xr-x
-rwxr-xr-x
drwxr-xr-x
lrwxrwxrwx

1
2
2
1
2
1

sam
sam
sam
sam
sam
sam

pubs 104 Jan 9 14:44


pubs 4096 Mar 26 11:59
pubs 4096 Apr 4 18:56
pubs
85 May 6 08:27
pubs 4096 May 20 09:17
sam
9 May 21 11:35

calendar
c
letters
execute
bin
shell -> /bin/bash

ls, . ls :
$ ls bin
c e lsdir

,
d (directory ).
:
$ ls -dl bin
drwxr-xr-x 2 sam pubs 4096 May 20 09:17 bin


( ),

make 761

.
(), .
$ ls -d ~/.*
/home/sam/.
/home/sam/..
/home/sam/.AbiSuite
/home/sam/.Azureus
/home/sam/.BitTornado

(+) ,
ACL-:
$ ls -l memo
-rw-r--r--+ 1 sam pubs 19 Jul 19 21:59 memo


Mac

OS

X

ACL
- le:
$ ls -le memo
-rw-r--r-- + 1 sam pubs 19 Jul 19 21:59 memo
0: user:jenny allow read

make
.
make [] [_] []
GNU- make
, , .

_ , (make-). , =,
, . .

f , make
GNUmakefile, makefile Makefile ( ),
. makefile.
Makefile, .

MAKE, MAC OS X,

make, ( ),
Mac OS X, Linux.

762 V.
directory=
C

debug d

, make , .

file=
f

( ).
.

(jobs ). n
. .
n, make .
jobs[=n] j
[n]

keep-going
k

_
.

just-print

dry-run
n

( ). , , make _ .

silent
quiet

_
- . . touch.

touch t

make
, .
_
. _

. , , ,
make _. .
make
www.gnu.org/software/make/manual/make.html, info-, make.
make ,
. ,
,
make.
make , . , ,

, Bourne Again Shell. ,


,
SHELL, .

make 763

:
SHELL=/bin/sh

/bin/sh SHELL
. Linux- /bin/sh
/bin/bash /bin/dash. Mac OS X /bin/sh
bash,
Bourne Shell. SHELL ,
make SHELL. SHELL
, ,
SHELL , .
make .
,
at (@). , make
help :
help:
@echo " :"
@echo " "
@echo "libbuf.a
-- "
@echo "Bufdisplay -- "
@echo "Buf2ppm
-- pixmap"

@ make
echo. , help. make
. ,
help, make help
.
make ,
(). ,
make , /bin/rm
( /bin/rm , libbuf.a ):
-/bin/rm libbuf.a
, make

, . , , ,
, , . , $?, , , . , :
list: .list
.list: Makefile buf.h xtbuff_ad.h buff.c buf_print.c xtbuff.c
pr $? | lpr
date >.list

.list , . pr $? | lpr

764 V.
, .list. date > .list .list,
.
make list .
,
. make
Make.config ,
, ,
:
include Make.config

Mac OS X make
Developer Tools.

make _ analysis
cc. GNUmakefile,
makefile Makefile, .
$ make analysis
cc -c analy.c
cc -c stats.c
cc -o analysis analy.o stats.o

analysis,
analysis.mk, :
$ make -f analysis.mk analysis
'analysis' is up to date.
analysis' .

, make , _ credit.
n (no-execution ) make .
$ make -n credit
cc -c -O credit.c
cc -c -O accounts.c
cc -c -O terms.c
cc -o credit credit.c accounts.c terms.c

_ credit t.
make credit .
$ make -t credit
$ make credit
'credit' is up to date.

credit' .

Makefile
. morning ( ). , -

make 765

, morning morning.c. :
, morning, C- gcc.
, , .
$ cat Makefile
morning: morning.c
TAB gcc -o morning morning.c

make, make morning.c,


, morning.

ff. cc, ff,
, ff ,
cc .
$ cat Makefile
# ff fastfind.c
SHELL=/bin/sh
ff:
gcc -traditional -O2 -g -DBIG=5120 -o ff fastfind.c myClib.a
$ make ff
gcc -traditional -O2 -g -DBIG=5120 -o ff fastfind.c myClib.a


compute. make ( #);
. , compute
: compute.o calc.o.
make compute. , compute.o
C, compute.h. compute.o
C- ( O3). . , make
, calc.o calc.c, ,

.
$ cat Makefile
#
# compute
#
compute: compute.o calc.o

gcc -o compute compute.o calc.o

compute.o: compute.c compute.h


gcc -c -O3 compute.c

calc.o: calc.c
gcc -c calc.c

clean:

rm *.o *core* *~

766 V.
clean .
, .o.
,
, . ,
.
$ cat Makefile
###########################################################
##
###########################################################
SHELL=/bin/sh
###########################################################
## . XLDLIBS
#
, .
#
CCFLAGS .
CC=gcc
CCFLAGS=-O2 $(CFLAGS)
XLDLIBS= -lXaw3d -lXt -lXmu -lXext -lX11 -lm
BUFLIB=libbuf.a
###########################################################
##
INCLUDES=buf.h
XINCLUDES=xtbuff_ad.h
OBJS=buff.o buf_print.o xtbuff.o
###########################################################
## 'make'
help: Help
@echo " :"
@echo " "
@echo " libbuf.a
-- "
@echo " bufdisplay
-- "
@echo " buf2ppm
-- pixmap"
###########################################################
##
libbuf.a: $(OBJS)
-/bin/rm libbuf.a
ar rv libbuf.a $(OBJS)
ranlib libbuf.a
###########################################################
## ,
bufdisplay: bufdisplay.c libbuf.a
$(CC) $(CCFLAGS) bufdisplay.c -o bufdisplay $(BUFLIB) $(XLDLIBS)

man 767
buf2ppm: buf2ppm.c libbuf.a
$(CC) $(CCFLAGS) buf2ppm.c -o buf2ppm $(BUFLIB)
###########################################################
##
buff.o: $(INCLUDES) buff.c
$(CC) -c $(CCFLAGS) buff.c
buf_print.o:$(INCLUDES) buf_print.c
$(CC) -c $(CCFLAGS) buf_print.c
xtbuff.o: $(INCLUDES) $(XINCLUDES) xtbuff.c
$(CC) -c $(CCFLAGS) xtbuff.c

make , .
,
IP- ,
namedhosts.tab.
- hosts.html, .
:
$ cat makefile
#
SHELL=/bin/bash
#
hosts.html: hosts.tab
@echo "<HTML><BODY>" > hosts.html
@awk '{print $$2, "<br>"}' hosts.tab >> hosts.html
@echo "</BODY></HTML>" >> hosts.html

man
.
man [] []
man k _
man (manual ) -

Linux
Mac

OS

X
. , , , Linux
OS X. Linux OS X
GNU, info (. info:
. 56) .
, , , ,
. ,
,
man-.

768 V.

man (. man: ,
. 54). man
man-.
man, k, _ , man-; man ,
_. man k ,
apropos (. apropos: . 55).

, (), Linux. ,
Linux. ,
, Linux, OSX.
man- .
man . , all a

, .
_ man-. ,
, . Linux .
K _

,
_. .
apropos (.
apropos: . 55).
apropos k
_

man- , ,
, . . .
manpath=
M

PostScript-.
.
troff t

,

Linux
. , , ,
. , ,
.
man: ,
. 54.

man ,
, less.

man 769


PAGER . ,
~/.bash_profile less
more:
export PAGER=/usr/bin/less

OS X less more
. - .
MANPATH
MANPATH , man, man-. , bash,
Linux, ~/.bash_profile
, man /usr/man, /usr/local/man /usr/
local/share/man:

export MANPATH=/usr/man:/usr/local/man:/usr/local/share/man


man /etc/manpath.config
/etc/man.config (Linux) /etc/man.conf (OS X).
man-, man.

man
man
: . 53.
man . ,
man ascii ASCII- ,
man k postscript man-,
PostScript.
man- . man- . man-
man .
, , , .
,
man-. man-,
, man-,
.
man- , . , write(2) man- write 2
(. man: . 53).
col,
man-, .
PostScript- man-.
$ man ls | col -b > ls.txt
$ man -t ls > ls.ps

770 V.
Linux PostScript- PDF ps2pdf.

man write, :
$ man write
WRITE(1)
BSD General Commands Manual
WRITE(1)
NAME
write - send a message to another user
SYNOPSIS
write user [ttyname]
DESCRIPTION
The write utility allows you to communicate with other users, by copying
lines from your terminal to theirs.
When you run the write command, the user you are writing to gets a message of the form:
Message from yourname@yourhost on yourtty at hh:mm

man-

man,
:
$ man man
MAN(1)
Manual pager utils
MAN(1)
NAME
man - an interface to the on-line reference manuals
SYNOPSIS
man [-c|-w|-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-adhu7V] [-i|-I]
[-m system[,]] [-L locale] [-p string] [-C file] [-M path] [-P
pager] [-r prompt] [-S list] [-e extension] [--warnings [warnings]]
[[section] page ]

DESCRIPTION
man is the system's manual pager. Each page argument given to man is
normally the name of a program, utility or function. The manual page
associated with each of these arguments is then found and displayed. A

man man-,
. man k man, latex. apropos ,
man k.
$ man -k latex
elatex (1) [latex]
latex (1)
mkindex (1)
pdflatex (1)

structured text formatting and typesetting


structured text formatting and typesetting
script to process LaTeX index and glossary files
PDF output from TeX

mkdir 771
pod2latex (1)
Pod::LaTeX (3perl)

- convert pod documentation to latex format


- Convert Pod data to formatted Latex

, _, k,
. _, ,
, ,
LaTeX ( ). 3perl
, man- 3 Section 3
(Subroutines) Linux
, Perl Perl Programmers Reference Guide (
Perl,
Perl 11).

mkdir
.
mkdir [] _
mkdir .

_ ,
mkdir.

Linux mkdir , .V.2. ,


(), Linux. ,
, Linux,
OS X.
, .
,
(. .V.7), (. .V.4).
mode=
m

parents p

, ,

.
.
p.
verbose v

, , () , .
mkdir , (. ..).

772 V.

accounts,
, prospective accounts:
$ mkdir -p accounts/prospective

, ,
accounts:
$ mkdir accounts/existing

accounts :
$ cd accounts
$ mkdir closed

, .
mode
:
$ mkdir -m go= accounts/past_due

mkfs
.
mkfs []
mkfs ,
. ,
, . mkfs Linux.
MKFS

mkfs :
.

, .
/etc/fstab,
, ,
(, /home /dev/sda2).

mkfs , ,
, mkfs (, ext2, ext3,
ext
4,
msdos
,
reiserfs
). , .

mkfs 773


(type ). fstype
, , ext2, ext3, msdos reiserfs. .
t fstype

(verbose ). .
, ,
V.
v

,

, , ,
ext
2 ext

3. , :
$ ls /sbin/mkfs.*
/sbin/mkfs.bfs
/sbin/mkfs.cramfs

/sbin/mkfs.ext2
/sbin/mkfs.ext3

/sbin/mkfs.minix
/sbin/mkfs.msdos

/sbin/mkfs.reiserfs
/sbin/mkfs.vfat

/sbin/mke2fs /sbin/mkfs.ext2. ,
,
man- ,
.
$ /sbin/mkfs.ext3
Usage: mkfs.ext3 [-c|-l filename] [-b block-size] [-f fragment-size]
[-i bytes-per-inode] [-I inode-size] [-J journal-options]
[-N number-of-inodes] [-m reserved-blocks-percentage] [-o creator-os]
[-g blocks-per-group] [-L volume-label] [-M last-mounted-directory]
[-O feature[,]] [-r fs-revision] [-E extended-option[,]]
[-T fs-type] [-jnqvFSV] device [blocks-count]

(block ). . ext2, ext3 ext4 1024, 2048 4096.


b

(check ).
. ,
- .
c


, .
, . , , .
4.


Mac

OS

X
diskutil (. ).

774 V.
ext

2
ext
3, tune2fs (. ) j. (.
.) tune2fs
fsck (. ).
fsck, mkfs ,

. , ext2 ext3 mkfs mke2fs (
mkfs.ext2 mkfs.ext3).
man-, mke2fs. ,
mkfs, mkfs., .
mkfs ,
mkfs
.
mkfs

mkfs /dev/hda8. ext2.


# /sbin/mkfs /dev/sda5
mke2fs 1.41.4 (27-Jan-2009)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
122880 inodes, 489948 blocks
24497 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
60 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

tune2fs ext2 ext3:


# /sbin/tune2fs -j /dev/sda5
tune2fs 1.41.4 (27-Jan-2009)
Creating journal inode: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Mtools 775

Mtools
DOS .
mcd []
mcopy [] _
mdel _
mdir [w]
mformat []
mtype [] _
DOS Linux,
Mac OS X DOS. mcopy
Linux
/
OS
X


DOS
-. /dev/fd0 A:.

.V.20 Mtools.
V.20. Mtools

Mcd

DOS-

Mcopy

DOS-

Mdel

DOS-

Mdir

DOS-

Mformat

DOS-

Mtype

DOS-

, mcd mdir, DOS-. _, mcopy


mtype, , , . , mcopy, .
mcopy _, ,
. ,
mformat, DOS

-, , (, A:).

mcopy
.
mcopy .
n

776 V.
p

(preserve ). -

.
s

(recursive ). .

(text ). DOS Linux/OS X, . DOS ( )


RETURNNEWLINE; Linux/OS X
( ). DOS- Linux-.
t

mdir
(wide ). ,
. mdir , ,
.
w

mformat
f 1440

3,5- (HD)

1440.
f 2880

3,5- (HD)

2880.
(label ). DOS-.
v

mtype
(text ). t mcopy.

DOS- .
t

DOS-,
. ,

DOS
, . , , (*), ( ,
Linux

/
OS

X
), ,
DOS
*.*.

DOS- DOS- ,
DOS ().
Linux Mtools, Mtools (www.gnu.org/software/mtools).

Mtools 777

Mac OS X Mtools
:
1. MacPorts.
2. .
3. sudo port install Mtools.
DOS,
DOS- Linux/OS X ,

Linux
/
OS
X

. Mtools
DOS DOS-
.
, ,
.
Mtools .
, .

.
Mtools DOS (mdir) DOS
Linux (mcopy). DOS-
, DOS: A: , C:
. .
(/) Linux, (\) DOS.
, .
Mtools 0, , 1 2 .

mdir DOS,
/dev/fd0:
$ mdir
Volume in drive A is DOS UTY
Directory for A:/
ACAD
LIF
419370
5-10-09
CADVANCE LIF
40560
2-08-08
CHIPTST EXE
2209
4-26-09
DISK
ID
31
12-27-09
GENERIC LIF
20983
2-08-08
INSTALL COM
896
7-05-09
INSTALL DAT
45277
12-27-09
KDINSTAL EXE
110529
8-13-09
LOTUS
LIF
44099
1-18-09
PCAD
LIF
17846
5-01-09
READID
EXE
17261
5-07-09
README
TXT
9851
4-30-09

1:29p
10:36a
4:22p
4:49p
10:37a
10:23a
4:49p
10:50a
3:36p
3:46p
8:26a
10:32a

778 V.
UTILITY
WORD
WP
15

LIF
LIF
LIF
File(s)

51069
16817
57992
599040

5-05-09
9:13a
7-01-09
9:58a
8-29-09
4:22p
bytes free

*.TXT DOS
mcopy. .TXT , .
DOS .TXT , t
(RETURN) .
*, ,
c

mcopy. mcopy *.txt, README.TXT, DOS


, .
$ mcopy -t a:\*.txt .
Copying README.TXT

DOS mformat,
.
, , fdformat:
$ mformat a:

mdir ,
:
$ mdir a:
Can't open /dev/fd0: No such device or address
Cannot initialize 'A:'

mv
.
mv [] _ __
mv [] __
mv [] _ _
mv,
, . .

. . , mv
( ,
).

_ , ,
. __ .

mv 779

__ ,
, ,
. , ,
, __
, .
_
_. ,
_ t.

Linux mv ,
.V.2. ,
(), Linux

. , , ,
Linux, OS X.
( ~) , . (Linux)
backup b

mv , , . , .
force f


mv. y
Y, mv , mv .
interactive
i

,
, mv
. ( ), mv . (Linux)
update u

verbose v

GNU- mv ,
cp ( a) rm: _
__, _ . __ , mv .
rm, _,
.
, , mv
. y Y, mv ,
. f, mv , , .

780 V.
mv
, mv ,
.

letter, ,
letter.1201:
$ mv letter letter.1201

,
~/archives:
$ mv letter.1201 ~/archives

, memo, /p04/backup:
$ mv memo* /p04/backup

u mv . mv u (memo2)
. mv u
( memo2 ,
memo1).
$ ls -l
-rw-rw-r-- 1 sam sam
-rw-rw-r-- 1 sam sam
$ mv -u memo1 memo2
$ ls -l
-rw-rw-r-- 1 sam sam
-rw-rw-r-- 1 sam sam
$ mv memo1 memo2
$ ls -l
-rw-rw-r-- 1 sam sam

22 Mar 25 23:34 memo1


19 Mar 25 23:40 memo2
22 Mar 25 23:34 memo1
19 Mar 25 23:40 memo2
22 Mar 25 23:34 memo2

nice
.
nice [] [_]
nice .
. ,
root
-. TC Shell nice .
.

_ ,
. nice , nice.

nice 781

nice , 10,
10: 0 10. , , .
, (), Linux. , ,
Linux, OS X.
( )
. 20 ( ) 19
n
( ). , .
, root-. .
adjustment=


renice (. ) r top (.
).
( ) ,
. ( )
, .
, root-,
,
, .
nice .
TC Shell nice.
tcsh , _, .
4. .
nice [] _
tcsh nice , nice: nice 5 , _. nice n 5 ,
_.

find
. ps l nice-
NI:
# nice -n 19 find / -name core -print > corefiles.out &
[1] 422
# ps -l
F S
UID
PID PPID C PRI NI ADDR SZ WCHAN TTY
4 R
0
389 8657 0 80
0 - 4408 pts/4

TIME CMD
00:00:00 bash

782 V.
4 D
0 R

0
0

422
433

389 28
389 0

99
80

19 0 -

1009 1591 -

pts/4
pts/4

00:00:04 find
00:00:00 ps


(15):
# nice -n -15 find / -size +50000k

nohup
, .
nohup _
nohup , . , nohup
SIGHUP.
. TC

Shell
nohup. .

_ , .

Linux nohup , .V.2.


,
nohup, ,
nohup.out, .
nohup ~/nohup.out.
nohup, TC Shell nohup nohup.out. , tcsh,
.

, nohup:
$ nohup find / -name core -print > corefiles.out &
[1] 14235

od
.
od [] [_]
od (octal dump ) .
() -

od 783

. , , . od
Linux.

_ , od. _ , od
.

od , .V.2.
, ,
. A
. d (deci
mal ), o (octal ), x (hexadecimal )
n ( ).
addressradix=

skip-bytes=n
j n

n .

read-bytes=n
N n

n
.

, n ASCII-,
(NULL).
strings=n
S n

. t [n]
, .
.V.21.
od . , od ,
, n. ,
a c. n .V.23.

format=[n]

V.21.

.
ASCII-. ,
(FORMFEED) ff

ASCII-. , (. .
V
.22)

784 V.
V.21 ()

( )

V.22. -,

\0

(NULL)

\a

(BELL)

\b

(BACKSPACE)

\f

(FORMFEED)

\n

(NEWLINE)

\r

(RETURN)

\t

(HORIZONTAL TAB)

\v

(VERTICAL TAB)

V.23.
n


( d, o, u x)

C (character )

S (short integer )

I (integer )

L (long )

4 32-
8 64-
( f)

F (float )

D (double )

L (long double )

, 8

od, POSIX, od ,
.V.24,
.
V.24.

t a

t oC

od 785

t c

t u2

t fF

-h

t x2

t d2

t d4

t o2

t x2

ac, , ASCII

-. .

. .
$ od -t
0000000
0000020
0000040
0000060
0000100
0000120
0000140
0000160
0000200
0000220
0000240
0000260
0000300
0000320
0000340
0000360
0000400
0000401

a ac
nul soh stx etx eot enq ack bel bs ht nl vt ff
dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc
sp ! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ 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 [ \ ] ^ _
` 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 { | } ~ del
nul soh stx etx eot enq ack bel bs ht nl vt ff
dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc
sp ! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ 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 [ \ ] ^ _
` 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 { | } ~ del
nl

cr so si
fs gs rs us

cr so si
fs gs rs us

, ASCII-
, (. .V.22):
$ od -t
0000000
0000020
0000040
0000060
0000100
0000120
0000140
0000160
0000200
0000220

c ac
\0 001 002 003 004 005 006 \a \b \t
020 021 022 023 024 025 026 027 030
! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ 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 [ \ ] ^ _
` 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 { | } ~ 177
200 201 202 203 204 205 206 207 210
220 221 222 223 224 225 226 227 230

\n \v \f \r 016 017
031 032 033 034 035 036 037

211 212 213 214 215 216 217


231 232 233 234 235 236 237

786 V.
0000240
0000260
0000300
0000320
0000340
0000360
0000400
0000401

240
260
300
320
340
360
\n

241
261
301
321
341
361

242
262
302
322
342
362

243
263
303
323
343
363

244
264
304
324
344
364

245
265
305
325
345
365

246
266
306
326
346
366

247
267
307
327
347
367

250
270
310
330
350
370

251
271
311
331
351
371

252
272
312
332
352
372

253
273
313
333
353
373

254
274
314
334
354
374

255
275
315
335
355
375

256
276
316
336
356
376

257
277
317
337
357
377

/usr/bin/who ,
(, ) .
strings. , .
$ od -A

0028455
0028473
0028487
0028503
0028520
0028525
0028539
0028543
0028558
0028573
0028585
0028602
0028606
0028612
0028617
0028625
0028629

d -S 3 /usr/bin/who
/usr/share/locale
Michael Stone
David MacKenzie
Joseph Arceneaux
6.10
GNU coreutils
who
abdlmpqrstuwHT
%Y-%m-%d %H:%M
%b %e %H:%M
extra operand %s
all
count
dead
heading
ips
login

open (OS X)
, URL-.
open [] [_]
open , URL-.

_ URL

-, open.

open _
Finder.

open (OS X) 787

_ .
_
Finder.

_ . ,
, , .
, com.apple.TextEdit TextEdit.
b

(edit ). _

TextEdit.

(file ).
, . _.
f

(text ). _ , (. ).
t

, . ,
. open
, .
LaunchServices
, , . ,
. LaunchServices ,
t f.

,
(GUI), open
Terminal ,
GUI. .

backups.dmg.
/Volumes , .
$ ls /Volumes
House
Spare
Leopard
$ open backups.dmg
$ ls /Volumes
Backups
House
Spare

Leopard

picture.jpg.
(, Terminal).
.

788 V.
, open ,
. open .jpg .
$ open picture.jpg

Finder /usr/bin. /usr Finder


, . open ,
, Finder.
$ open /usr/bin

otool (OS X)
, .
otool _
otool ,
.

_ , otool.

_,
otool, L, M, t T.
(libraries ). , .
L

(module ). .

(print ). .
t v V.
p

(table of contents ).
.
T

(text ). TEXT .

(very verbose ). ( v)
. otool , .
V

(verbose ). .
otool , .
v

otool . . ,

otool (OS X) 789

chroot otool ,
.
.
, T
- .

otool
Developer Tools.
, ELF, otool L
, ldd.

C:
$ cat myname.c
#include <stdio.h>
int main(void) {
printf("My name is Sam.\n");
return 0;
}

otool , :
$ otool -L myname
myname:
/usr/lib/libmx.A.dylib (compatibility version 1.0.0, current version 92.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.0.0)

, , . , ,
otool -L:
$ otool -L /usr/lib/libmx.A.dylib /usr/lib/libSystem.B.dylib
/usr/lib/libmx.A.dylib:
/usr/lib/libmx.A.dylib (compatibility version 1.0.0, current version 92.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.0.0)
/usr/lib/libSystem.B.dylib:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.0.0)
/usr/lib/system/libmathCommon.A.dylib (compatibility version 1.0.0, current

main.
.
, main, ,
_main.
$ otool -Vt -p _main myname
myname:
(__TEXT,__text) section
_main:
00002ac0
mfspr
r0,lr
00002ac4
stmw
r30,0xfff8(r1)

790 V.
00002ac8
00002acc
00002ad0
00002ad4
00002ad8
00002adc
00002ae0
00002ae4
00002ae8
00002aec
00002af0
00002af4
00002af8
00002afc
00002b00

stw
stwu
or
bcl
mfspr
addis
addi
bl
li
or
lwz
lwz
mtspr
lmw
blr

r0,0x8(r1)
r1,0xffb0(r1)
r30,r1,r1
20,31,0x2ad8
r31,lr
r2,r31,0x0
r3,r2,0x4b8
_printf$LDBLStub
r0,0x0
r3,r0,r0
r1,0x0(r1)
r0,0x8(r1)
lr,r0
r30,0xfff8(r1)

paste
.
paste [] [_]
paste , _,
. .

_ . _ ,
paste .

Linux paste , .V.2. ,


(), Linux. ,
, Linux,
OS X.
dlist , . dlist , paste
. dlist , , , ,
.
delimiter=
dlist d dlist

. .
.

serial s

paste 791

paste . ,
cut, , paste
.

fnames acctinfo.
, cut (. ) /etc/
passwd. paste fullname, .
.
Mac OS X, /etc/passwd .
$ cat fnames
Sam the Great
Max Wild
Zach Brill
Helen Simpson
$ cat acctinfo
sam:x:401:50:/home/sam:/bin/zsh
max:x:402:50:/home/max:/bin/bash
zach:x:504:500:/home/zach:/bin/tcsh
hls:x:505:500:/home/hls:/bin/bash
$ paste fnames acctinfo
Sam the Great
sam:x:401:50:/home/sam:/bin/zsh
Max Wild
max:x:402:50:/home/max:/bin/bash
Zach Brill
zach:x:504:500:/home/zach:/bin/tcsh
Helen Simpson
hls:x:505:500:/home/hls:/bin/bash

p1, p2, p3 p4.


d paste :
$ cat
1
one
ONE
$ cat
2
two
TWO
extra
$ cat
3
three
THREE
$ cat
4

p1

p2

p3

p4

792 V.
four
FOUR
$ paste
4
four
FOUR

p4 p3 p2 p1
3
2
three
two
THREE
TWO
extra

1
one
ONE

$ paste -d="+-=" p3 p2 p1 p4
3+2-1=4
three+two-one=four
THREE+TWO-ONE=FOUR
+extra-=

serial, :
$ paste --serial p1 p2 p3 p4
1
one
ONE
2
two
TWO
extra
3
three
THREE
4
four
FOUR

pax
, .
pax [] [_]
pax w [] [-]
pax r [] [_]
pax rw [] [-] -
pax :
, ,
. , pax,
, .
, pax.
( ) , . (w
) ,
, . (r )

. ,
. (rw) ,
.

pax 793

pax ,
- . - , pax , .
pax , , . ,
_. _, pax ,
. ,
pax, (.
. 157), ,
(/) (.). pax
man-, fnmatch ( OS X).
-. pax .

pax: , .


pax .
.
pax .
(read ).
. _ , pax . _ pax
, _. root.pax, ,
, .c:
r

$ pax -r \*.c < /Volumes/Backups/root.pax

* pax.
(write ). , .
, .
, . . , pax,
find. /Users
Users.pax /Volumes/Backups:
w

# find -d /Users -print | pax -w > /Volumes/Backups/Users.pax

794 V.
d find , ,
,
. find .
(copy ). ,

. , ,
, pax - (
pax).
, , .
/Users/
max/code. code .
, ~max/code ,
.
rw

$ pax -rw . ~max/code


pax. .
c
(complement ). , _.
, _.
f
(file ). . ,
. ,
.
. file tar (. ).
H
( ). , , , ,
. ,
, , . , , . chgrp, H L.
L
(). , , , ,
. .
, , .
chgrp,
H L

pax 795

(link ). , ,
.
l

( ). ,
, , , .
. ,
, .
pax .
chgrp, .
P

, _, ,
.V.25. pax ,
.
.
p _

V.25.


. :
s/ _ /_/[gp]
pax _
_. g , pax , _.
p ( print ) pax
.
vim, . 197, , .
s

(verbose ). ,
, ls l.
.
v

pax
, ,
-.

, .V.26.
, pax tar- POSIX (
ustar).
x

796 V.
V.26.

cpio

, POSIX cpio-

sv4cpio

, cpio-
UNIXSystem V, 4

tar

Berkeley tar-

ustar

POSIX tar- ( )

(gzip).

gzip. gunzip.

pax tar, cpio . pax


. , _ , pax
.

pax . pax . POSIX tar-.


pax .V.26.
pax ,
( x) .
Mac OS X 10.4 pax
.

pax corres.0901.pax.
corres.
$ pax -w corres > corres.0901.pax

w pax , , , (
corres ) . pax
.
corres.0901.pax.
, pax , ,
:
$ pax < corres.0901.pax
corres
corres/hls
corres/max
corres/max/0823

pax 797
corres/max/0828
corres/max/0901
corres/memo1
corres/notes

f , :
$ pax -f corres.0901.pax

pax ,
. ( ).
, Mac OS X,
/etc tar. pax , root-,

. pax
( ) MacOSX
/etc , pax , ,
. , ,
v, , :
# pax -w -f /tmp/etc.tar /etc
# pax -f /tmp/etc.tar
/etc
# pax -v -f /tmp/etc.tar
lrwxr-xr-x 1 root
admin
0 May 21 01:48 /etc => private/etc
pax: ustar vol 1, 1 files, 10240 bytes read, 0 bytes written.

L ( ). pax ,
/etc:
# pax -wLf /tmp/etc.tar /etc
# pax -f /tmp/etc.tar
/etc
/etc/6to4.conf
/etc/AFP.conf
/etc/afpovertcp.cfg
/etc/aliases
/etc/aliases.db
/etc/amavisd.conf
/etc/amavisd.conf.personal
/etc/appletalk.cfg

pax
memos, . -
, pax .
$ mkdir memos.0625
$ pax -rw -p e memos memos.0625
$ ls memos.0625
memos

798 V.
memos -.
pax
. s pax memos . ( )
memos.0625:
$ pax -rw -p e -s /memos/./ memos memos.0625

,
memo ,
find. pax cpio-. pax
,
bzip2, .
$ find . -type f -name "memo*" | pax -w -x cpio | bzip2 > memos.cpio.bz2

plutil (OS X)
.
plutil [] _
plutil .

_
, plutil.

convert

_ ,
xml1, binary1.

help

lint

( ).

(output ).

.
(silent ).
.
s

plutil
XML
- , . plutil ,
.

pr 799

plutil () . .

java.plist:
$ plutil java.plist
java.plist: OK

plutil :
$ plutil broken.plist
broken.plist:
XML parser error:
XML-:
Encountered unexpected element at line 2 (plist can only include one object)
2 (plist
)
Old-style plist parser error:
, :
Malformed data byte group at line 1; invalid hex
1 ;

StartupParameters.plist
, :
$ plutil -convert binary1 StartupParameters.plist

loginwindow.plist,
,
XML
- o
/tmp/lw.p:
$ plutil -convert xml1 -o /tmp/lw.p loginwindow.plist

pr
.
pr [] [_]
pr , , .
, ,
.
pr , . pr , .

_
, pr . _, pr
.

800 V.

Linux pr ,
.V.2. ,
(),
Linux
. , , , Linux, OS X.
_. , .
showcontrol-chars
c

(^; , ^H).
, . (Linux)

col , .

m (merge).

columns=col
col

double-space
d

(FORMFEED)

(NEWLINE).

form-feed
F

header=
h

. , .

length=
l

merge m

.
66.

.
col (columns).
. pr c ,
(
n[c[num]]
). num ( 5).
numberlines=[c[num]]

indent=
o
separator=c
s[c]

( ).

c ( ,
c, ). pr -
, w, .
pr ,
. , pr,
, , . .
omit-header
t

ps 801
width=num
w num

num .
( m
(merge) col (columns)).

pages=
_
[:_
]

_ _.
_ pr
. ,
. Mac OS X _;
.

+ _

[:_
]

col (columns) pr ( ).

pr,
memo lpr :
$ pr memo | lpr

memo , . .
$ pr -h 'MEMO RE: BOOK' memo | lpr &
[1] 4904

pr memo ,
3:
$ pr -t +3 memo

ps
.
ps [] [_]
ps , .

_

PID
-. _, ps ,
.

802 V.

Linux ps
, . . man-, ps.
: GNU () .
: UNIX98 () .
: BSD .
, ()
, Linux

. , , Linux,
OS X
A

(all ). . e.

(everything ). . A.

(full ). ,
.

forest f

( f ). (Linux)

(long ). ,
. ,
, .
l

. ,
. (Linux)
no-headers

user
_

, ,
UID- _ .
, .

(wide ). ps
. 132,
, , .
;
, ls .
w

ps ,
. .V.27
, ps.
V.27. , I

PID

TTY (terminal)

ps 803

TIME

, ,

CMD

, . ,
.
, w

, ps, . .V.28
.
V.28. , II. (
. UNIX98
( ).)

%CPU

, . - , Linux

/
OS
X

, ,
%CPU 100%

%MEM ()

(RAM)

COMMAND CMD

, .
.
, w.

F ()

NI ( nice)

nice (. nice)

PID

PPID (

PID)
PRI ()

RSS (resident set size


)
SIZE SZ

STIME START

STAT S ()

,
:
<
D
L (
-)
N
R ( )
S
T ,
W
X
Z -, ,

804 V.
V.28 ()

TIME

TTY ()

USER UID

WCHAN (wait channel

, , . ,
,

top (. ).

ps , .
(bash), ps.
$ ps
PID TTY
2697 pts/0
3299 pts/0

TIME CMD
00:00:02 bash
00:00:00 ps

C l (long ) ps :
$ ps -l
F S
UID
000 S
500
000 R
500

PID
2697
3300

PPID
2696
2697

C PRI
0 75
0 76

NI ADDR
0
0
-

SZ WCHAN
639 wait4
744 -

TTY
pts/0
pts/0

TIME CMD
00:00:02 bash
00:00:00 ps

u , :
$ ps -u root
PID TTY
1 ?
2 ?
3 ?
4 ?
5 ?

TIME
00:00:01
00:00:00
00:00:00
00:00:01
00:00:00

CMD
init
kthreadd
migration/0
ksoftirqd/0
watchdog/0

forest ps , man-
ASCII