SECOND EDITION
Mark G. Sobell
..
PRENTICE
HALL
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
ISBN 978-0131367364 .
ISBN 978-5-459-00450-2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
504
512
520
530
534
535
538
8
CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
544
546
550
550
551
552
553
553
554
554
562
583
584
584
585
585
585
586
590
600
601
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
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
UNIX/Linux , ,
.
, . 42,
,
. 52, ,
Linux, .
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
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
, :
.
,
, .
, ,
.
, .
,
()
, ,
.
.
, ,
, .
. .
-
,
()
.
,
(), ,
.
-
,
,
.
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
, (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
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
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
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,
, .
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
, :
$ 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
.
, . , 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
, , .
, , . ,
, , .
.
-, 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).
, ?
, ,
: . ,
, - ,
. , , ,
,
.
, ,
.
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
,
, , , , , . , ,
.
, ,
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.
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
$ 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)
batch
batch
doing.
doing.
56 2. !
whatis
whatis apropos, ,
:
$ whatis who
who
(1)
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
58 2. !
PostScript-,
, man t. , man
t cat | lpr cat. , - www.tldp.org,
- .
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
Linux
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,
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 __ __
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,
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 22:31 letter_e
l (long ) ls . , letter_e
81
50. bzip2
, .bz2. , ;
, .
bzip2 ,
.
.bz2
K
bzip
2 , bunzip
2,
. k (keep ) .
, zach.jpg :
$ ls -l
-rw-r--r--
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
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
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
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 ,
, . ,
.
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
,
, ,
. ,
, .
, ,
,
.
, , ,
. 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
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
0.23, 0.26
PCPU WHAT
0.00s vi memo
0.07s w
0.00s run_bdgt
0.24s bash
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
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)!
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 .)
, .
, , ( ,
) (
).
, .
.
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
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).
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.
home
max
names
temp
literature
demo
promo
.4-7
4.6.
,developed
Figure
The file structure
in the examples
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
(
/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
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,
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
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
, . . /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
.
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
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
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
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
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
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
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
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
, 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
zach
memo
/home/max/letter
planning
/home/zach /draft
, 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-
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
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---
drwxr-xr-x
, ,
? ,
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,
, ,
.
:
: 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
. 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
$
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
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
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
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
.
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.
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.
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
tee:
tee (.
V
) , . (tee ):
. 5.10 who
tee. tee
who.out, .
tee
grep, , sam.
tee , ,
a (append ).
5.10. tee
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
$
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
,
jobs.
, , , PID- . , , , ,
kill.
jobs
157
$ tail -f outfile &
[1] 18236
$ bigjob &
[2] 18237
$ jobs
[1]- Running
[2]+ Running
$ kill %1
$ RETURN
[1]- Terminated
$
, , ,
, . -, .
,
, . , , , - . , , .
, .
, .
, , ,
, .
?
(?) , .
. 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
,
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
$ 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
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
vim , . vim , .
vim
. , . , , , .
,
vim, .
. vim
, vim, ,
, , vim. vim
.
,
, 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 . 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.
,
: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
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).
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
The w (word) key moves the cursor forward to the first letter of the next word
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
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
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
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
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
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
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/
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)
.
. () ,
. , , 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
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'
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
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
ZZ vim-, .
.
vim
vim . ,
:
:sh
:sh
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
,
, , ,
. , ,
: , (.6.9).
6.9. ,
Pear
Pear!
Pear!)
Pear!) The
Pear!) The
, .
() , (
) .
, , .
.
,
, . ,
,
, .
, fmt (. V,
fmt . 708).
213
.
, ,
(.6.10).
6.10.
.
(.6.11).
6.11.
One paragraph
: ,
Even though in
,
.
Just because there are
two indentions does not mean
,
it qualifies as two paragraphs
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+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, .
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
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+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-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
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.
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
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
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+}
() .
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
META+a
M-a
META+A
CONTROL+META+a
C-M-a
META+CONTROL+a
M-C-a ( )
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+_
, ,
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
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 ?,
(, ?a, ?\C-i, ?\033)
- ,
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,
\ 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
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.
:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
:
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
283
dash
Korn Shell
, , , ,
.
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
, , . , ,
.
BASH_ENV
,
.bash_profile .bashrc, . , , .bash_profile, .bashrc ,
( .bashrc ). ,
.bashrc, , .
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
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,
, ,
, , ,
#
#
#
#
HOME CDPATH
kill control-u
,
Bourne Again Shell (, ), [, ] $ .
, .8.1
.
.
8.1. ,
()
(. ( ) , . 298)
$( )
(. , . 358)
(( ))
; let (
, , . . 480)
$(( ))
( ,
, . )
[]
test (. test
[[ ]]
; [ ], (. ( ) . 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.
&>
. >&
tcsh (.
. 377).
1>
hold, 2>&1
2 1.
, hold.
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,
. , ,
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?
./,
:
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
#!
, , (
) .
,
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.
, ,
, .
, 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
Done
Done
d
e
,
:
$ d
[1]
[2]
[3]
, (&) . . , ,
. , ,
(|) .
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
.
, .
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
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
. 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
$
PID-
, , .
, , date, .
( date)
1. fg,
, , . 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
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
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
popd:
popd (pop directory ).
.8.5, popd
, :
$ dirs
~/literature ~/demo ~/names
$ popd
~/demo ~/names
$ pwd
/home/sam/demo
literature
popd
demo
names
- 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
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
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). ,
, ,
.
.
,
person ,
max ( tcsh
setperson=max):
$ person=max
$ echo person
person
$ echo $person
max
, $.
, (\).
$ echo $person
max
307
$ echo "$person"
max
$ echo '$person'
$person
$ echo \$person
$person
,
, , . ,
,
. , .
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*
. , :
$
$
$
$
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
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
.
$
$
$
$
declare
declare
declare
declare
person1=max
-r person2=zach
-rx person3=helen
-x person4
, +. .
, 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
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)
, . ,
. , .
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
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
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 , .
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:
. 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
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)
<<
( * . 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
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
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
.
(&) ,
, ,
. , , 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
$ HISTSIZE=100
bash 100
:
$ HISTFILESIZE=100
HISTFILESIZE
, 100
.
, history.
. tcsh
.
,
. history,
.
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
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.
!! . 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--
,
,
. , , . , .
!
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
, !$
. , , 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*
, ??
- .
, ,
.
,
, .
(:).
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 )
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. -
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
, ,
.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
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"
[$else
]
$endif
, 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 ,
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. .
,
. ,
, , . ,
, .
.
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'
, , ,
. ,
, . , ,
, ,
./.
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
F ls (/) (*) . ,
ll (ls l), (ls). , ,
. ls,
,
ls F l. ( , ls , ,
.)
= ,
alias .
, -
(\):
$ \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
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, . (),
:
.
(.
. 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
End of options
.
( ) ,
()
Bourne
Again
Shell
. set ,
shopt .
, bash.
, , ?
,
, , . bash info-
options ,
values of variables controlling optional shell behavior.
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
histappend
shopt s
bash HISTFILE histappend
( ,
. 324). bash
histexpand
( ; .
(!) . 329).
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
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
.
. 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, ,
.
,
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 ( ,
).
($), . , (. -
,
.
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
;
:
$ 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 $(( )) ,
($) . ,
,
.
. 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 -
TC Shell
:
tcsh $ echo tmp*
echo: No match
, .
, ,
, (*).
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)
, . ,
.
, . , ,
.
, ,
.
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 TC ,
.
, ,
,
. . .
.
,
, .
, , .
.
bash .
bash
set shopt.
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
, .
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
, ,
~/.login. ,
, .
( )
setenv (. . 384). .login
.
.login
.login TERM ( if , -
, , , ,
TERM). stty
(. V) date (. V)
.
TC Shell
/etc/csh.logout ~/.logout.
.logout date .
sleep , echo , .
, .
/etc/csh.logout
.logout
()
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
. ,
!! tcsh, , bash, . !328 328; !?txt?
, txt.
(!)
. 329. .9.1 , tcsh
bash.
9.1.
. , a u l
.
,
,
(. .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
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 $ 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
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
21 last, , .
23 fn2, ,
.
bash (.
. 299) tcsh .
,
. %
, , .
,
, .
bash PID
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:
,
, . , - ,
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
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
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 , ( ), ,
. ,
, .
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.
$ @ 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.
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
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
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 . )
.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%
:
tcsh $ time
0.200u 0.340s 17:32:33.27 0.0%
. ,
, , . , 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
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
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
ifthenelse
ifthenelse . , if,
, true.
.
if () then
endif
. true, . false,
, else.
if () then
else
endif
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
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
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,
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/*
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
416
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
-
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.
( )
( 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 , ;
[.
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).
,
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
# 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
#!/bin/bash
#! . , ,
, .
lnks ,
,
. lnks , , :
#
# : lnks []
if lnks :
true, lnks 1. ,
,
.
, .
if , ( $1)
( d test true,
):
if [ -d "$1" ]; then
echo " ." 1>&2
427
, 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, (
, ).
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
$
+
+
+
+
+
+
+
, , ,
PS4. (+).
, .
PS4 , ,
. PS4 >>>>,
, :
PS4
x , , - set:
set -x
, 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 .
_
_
$ 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 ,
chas, , Marilou Smith:
$ ./whos chas "Marilou Smith"
chas Charles Casey
msmith Marilou Smith
$@
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
test
433
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
. ((()); .
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
aspell ( Linux) V.
if :
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
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"
;;
*)
-
=
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"
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
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)
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
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
, ,
,
,
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'
.
449
.
file1
echo file2 1>&2
cat >file2 <<' file2'
.
.
file2
. 289,
, .
, Linux
( ).
.
.
, .
, , . Linux :
( 0), ( 1) ( 2). . ,
>
1>
2>.
, , , 0, 1 2,
, - . ,
- .
exec n> _
exec m< _
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
# $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 , 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
, * , :
$ 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
}
$ 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-
, ; . ,
.
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
$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
$* $@:
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:
${} ( $, ) .
, bash ${} .
:
: , :
${:__}
: :
, __
. ,
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)
, , 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
: *
: *
$ 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"
, ,
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
$
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). , ,
. ( ) ,
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
exec
/dev/tty ,
, exec .
, (
), /dev/tty ( ):
read name < /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
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).
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. * ;
, .
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
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)
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
(()),
.
.
$ 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. ,
( )
:
[[ ]]
() . ($).
, test, .
, , , test (. V).
(AND) a,
[[ ]] &&. , test
o (OR), [[]]
||.
, , , age2, . [[]]
:
if [[ 30 < $age && $age < 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
bash
, ,
C. .10.8
. . , .
10.8.
var++
var
++var
var
! ( )
~ ( )
**
, ,
*
/
%
,
-
+
<<
>>
<=
>=
<
>
==
!=
485
& (AND)
^ (XOR)
| (OR)
()
&& (AND)
|| (OR)
? :
, .
, . ,
,
$ ((cmd1 | cmd2) || (cmd3 | cmd4)) && (cmd5 | cmd6)
:
.
, ,
.
, , . - ,
( ++COUNT VALUE), (++ 1, 1),
. - ( COUNT++ VALUE);
,
.
$ N=10
$ echo $N
10
$ echo $((--N+3))
12
$ echo $N
9
(%)
. , $((15%7)) 1.
. .
:
$ (mkdir bkup && cp -r src bkup) || echo " mkdir" >> /tmp/log
, && ||
. true
false. true (0) false
(1) :
$ false; echo $?
1
$? (.
$?: . 458). 1 (false):
$
$
1
$
$
1
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
(&&) 1, , 0 .
(|) , 0101, 0110,
0111 (7 ).
(||) 1,
, 0 .
$ echo $(( v1 && v2 ))
1
$ echo $(( v1 | v2 ))
7
$ echo $(( v1 || v2 ))
1
echo $(( v1 ^ v2 ))
echo $(( ! v1 ))
echo $(( v1 < v2 ))
echo $(( v1 > v2 ))
Bourne
Again
Shell
, . , bash, , ,
, ,
C Perl. :
, .
Bourne
Again
Shell
489
.
,
. - ,
, .
- , . , ,
. . ,
, .
, ,
: , ,
. .
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.)
#
#
#
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
, 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
{
# .
}
# 1
subject=$(choose_subj)
[[ $? -eq 0 ]] || exit 2
cd $subject || exit 2
echo
scramble
#
#
#
#
#
# 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 (
questions (), .
quescount , .
: index ,
quescount 1 ( ).
index
0 index .
((target=RANDOM % index))
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
}
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 ,
.
typeset bash
, readonly ( ). Bourne
Again Shell ,
, .
,
, 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) ,
? .
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
, 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-, , .
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)
, ,
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
,
({}), .
.
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
.
Perl 5.10
Perl 5.10. ,
Perl ,
:
$ perl -v
This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
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
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
\0xx ()
ASCII-, xx
\a
\e
\n
\r
\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.
, . ,
.
, ,
,
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
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
$ ./scalars1.pl
Sam 5 2
5 + 2
$name $n1 $n2
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;
#
#
#
# , ,
$ ./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 :
,
, , , .
() ,
, .
.
(. , . 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
, . -
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");
$ 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
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";
}
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;
@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.
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";
$line $_, :
while (my $_ = <>) {
chomp $_;
if ($_ =~ /regex/)
}
$_
, $_ :
while (<>) {
chomp;
if (/regex/)
}
#
#
#
#
$_
$_ chomp
$_
regex
, 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. .
$! .
, . words , file2.pl :
$!
, :
: 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) {
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";
#
#
#
#
#
#
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 ,
. .
,
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.
#
# , 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:
!~
,
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
:
$ 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
, . {.*?}
. 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
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/;
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
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. ,
.
, 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
, , (
).
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
gawk- GNU,
, UNIX awk ( gawk).
assign =
v =
. gawk- BEGIN (. BEGIN
END . 555). .
, AWK.
Linux
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)
split(str,arr,del)
sprintf(fmt,args)
args fmt ;
C
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) :
, ,
, 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, (~):
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$). -,
.
$5}' cars
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,
.
565
, .
. , , .
$ gawk '2000 <=
plym
fury
chevy
malibu
chevy
malibu
honda
accord
(,) .
, .
, . ,
, , gawk
. , , volvo ,
, bmw.
, (
)
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
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
-n
9850
750
1550
3000
3500
6000
.
; .
567
NR (), . ,
24. .
NR (
)
, (,) 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
, ,
, 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).
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
. 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}
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
, 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 >
$ ./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)
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)
( 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
g3 gawk
$0, gawk- (
BEGIN). (NR), $0: $0 (
) .
$ cat g3
$
1
2
3
4
{
print NR, "$0:", $0
getline aa
print NR, "aa:", aa
}
$
1
2
3
4
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 ""
}
, . 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
g6 to_upper . gawk-
, ,
.
$ cat g6
{
print $0 |& "to_upper"
"to_upper" |& getline hold
print hold
}
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
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
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
(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 ,
.
!
( ).
, ,
. , , (;).
{}
:
sed-. b t.
b []
.
.
,
,
( ).
.
t []
, ,
, sed.
, ; .
, . ,
.
.
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.
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 , . 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
(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.
, ,
(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
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 .
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.
, , ,
sed- ( ,
*$) .
,
, vim.
$ cat cleanup
sed 's/ *$//' $*
$
h
n
p
g
p
sed- , .
cat s1
# ( ) .
# .
# .
# .
# ( ).
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 , , .
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.
devices
( ,
root-).
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
speedup is 0.97
$ ls -l memos.copy
total 4
drwxr-xr-x 2 max max 4096 Jul 20 17:42 memos
(/) -
,
.
(/) - rsync , /*, , rsync
. - .
memos, times. memos , memos.copy2 ,
.
times
archive rsync , ( ,
, ), , , ..
;
hardlinks. archive
. 603. , :
archive
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
.
.
,
. , 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
rsync
,
. () ,
;
rsync. , rsync
, ,
.
link-dest=dir rsync
.
() ,
. dir , -.
dir , . . 603.
rsync,
link-dest=dir:
linkdest=dir
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
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
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
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
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
. 772
Mtools
DOS- . 775
619
tee
. 849
test
. 852
tr
. 861
tty
. 864
tune2fs
xargs
. 877
, .V.1.
. , 5K 5210. , 1. ,
, .
V.1.
KB
1000 (10 )
PB
1015
1024(2 )
250
MB
EB
1018
1048 576 (2 )
260
GB
ZB
1021
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
.
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
emacs
aspell 625
(setq-default ispell-program-name "aspell")
vim
aspell vim,
~/.vimrc (. .vimrc
.204):
~/.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.
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
,
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 .
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
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 .
.
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
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
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
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. ()
, _;
, , _
, _; ,
, _,
,
, , _;
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.
--w4th-
. ,
, n,
README, .
Windows.
GNU
configure,
. configure
, ,
. configure
make
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, :
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-,
, ,
-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,
letter .
, (
letter), .
jhomejsamjmemo jhomejsamj
letter.
1etter /home/sam
- f (- - force).
(),
. ,
, , .
- f (- - force)
,
.
$ ls -ld
drwxrwxr-x
$ 1s -1
-rw-r--r--rw-rw-r--
epio
$
657
one
'':
PermlSSlOn denled
$ -f one
$ 1s -1
-rw-r--r--rw-rw-r--
1
1
'' :
(--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 :
cpio
>/dev/sdel
-depth find , ,
,
. . ,
--.
--pass-through
. , ,
- copy-pass).
cpio - (
cpio). ,
,
, , .
~maxjcode:
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
v.
. ,
.
, .
, ,
--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 ,
.
,
. (. .)
(--preserve-modlflcatlon-tlme),
, .
.
.
Sam,
US-, jdevjsde1.
cpio
>/dev/sdel
find , cpio
-depth
, cpio
(. --).
(. )
-d (--make-directorles)
5120
(--preserve-modlflcatlon-tlme).
jhome,
OS
jUsers.
Sam:
catlon-tlme)
(--preserve-modl fl-
,
.
cpio .
662
v.
$ pwd
/home/sam/memocopy
$ cpio -idm --no-absolute-filenames home/sam/memo/\* < /dev/sdel
-f,
, , , :
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 -
). ,
, .
nt- .
). nt-.
-1
(list -
-r
(remove -
). nt-.
-u _
(user -
). nt-, -
, t-.
crontab
663
Vixie cron.
Vixie cron, ,
S VR3.
jvar jspooljcronjcrontabs. ,
cron nt- .
nt- ,
, ,
MAI L crontab-
, . ,
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
Sam,
~jbin.
MAI L , ,
nt-
Sam,
$ 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-.
-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- .
- -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 ]
(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
Time (GMT).
, date
, .
14:07:30 19 ),
2:07:30
August 19
# date 08191407.30
Frl Aug 19 14:07:30
2009
date :
%'
dd
.
dd []
dd (
device-to-device -
.
dd ,
.
. dd
,
.
.
V.
670
Lin ux dd
V.2. dd
, .
.
s=
).
(block size -
s=
l bs
n .
obs.
v=
,
n ,
[ , .. .]
. .
V.11.
count=
, dd,
u_.
4s=
4 f=_
(input file -
bs l bs.
).
n .
). ,
_. - u_
.
s=
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.
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
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
, Linux,
/free2 :
$ df -BM /free2
Filesystem
/dev/hda9
1M-blocks
988
, Linux,
NFS :
$ df -ht nfs
Filesystem
zach:/c
zach:/d
Size
2.0G
2.0G
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 .
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
$ 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
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)
.
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
( )
Backups.root.
,
root-. X ditto ( )
/.
# ditto -X / /Volumes/Backups.root
dmesg
.
dmesg []
dmesg , .
dmesg. (Linux)
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). . ,
( 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
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
\\
-,
\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 :
, ( ) . , 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:
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
pubs
pubs
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
708 V.
(. mesg: . 91).
,
.
finger kudos:
$ finger
[kudos]
Login
max
roy
@kudos
Name
Max Wild
Roy Wong
Tty
tty1
pts/2
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
directory structure
( )
,
i-, 1
, , lost+found
, ,
lost+found, ,
,
i-
ftp 715
()
fsck
.
, 6
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.
(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.