You are on page 1of 100

Linux- Unix-

UNIX
shell


8.08.02.01

: ..

2007

Linux Unix 8.08.02.01 ..:


.. , , 2007.- 11 .
UNIX Linux,
, ,
shell.
: .. , .
.

.. , .

...........................................................................................................................3
1 ..........................................6
1.1 .......................................................................6
1.2 ...............................................................................6
1.3 ...............................................................6
1.4 ..............................................................................................................7
(1945 -1955)........................................................................................7
(1965 - 1980)........................................................................................7
(1980 - ).............................................................8
1.5 UNIX.........8
2 , ...........................14
2.1 ...................................................................................................14
2.1.1 ..........................................14
2.1.2 ................................................................15
2.1.3 ............................................................16
2.1.4 ...................................................................17
2.2 .............................................19
2.2.1 ..............................................................19
2.2.2 ...................................................................20
2.2.3 ..........................................20
2.2.4 ............................................................21
2.2.5 ..................................................22
2.2.6 ...........................................................................23
3 ..............................................................................24
3.1 .......................................................................25
3.2 ..................................................................................28
3.2.1 ..............................................31
3.4 .....................................................................................31
3.4.1 ..........................................................................................................31
3.4.2 ............................33
4 , .................35
4.1 ...............................................................................................35
4.2 ............................................................................36
4.3 .....................................................................36
4.4 ...............................37
5 UNIX...........................................................40
5.1 ...........................................................................................40
5.2 .......................................................................................................40
5.3 ..................................................................................................41
5.3.1 Unix.................................................................................47
5.3.2 . .....................................48
5.4 UNIX System V....................................................49
6 - UNIX...................................................51
6.1 -................................................51
6.2 -......................................51
6.3 ............................................................................................52
6.4 .................................................................................................52
6.5 ....................................53

4
6.6 ............................................53
6.7 .....................................................................................................54
6.7.1 ....................................................................................................54
6.7.2 ......................................................................................................54
6.7.3 ........................................................................56
6.7.4 ..........................................................57
6.7.5 ......................................................................................58
6.7.6 ...........................................................................................59
6.7.7 ...................................................................60
6.7.8 .......................................................................61
6.7.9 ................................................62
7 UNIX Linux System..............................................................64
7.1 s5........................................................................64
7.1.1 .....................................................................................................64
7.1.2 .........................................................................64
7.1.3 ...................................................................................................66
7.1.4 .........................................................................................66
7.1.5 ......................................................................66
7.1.6 .........................................66
7.2 VFS......................................................................68
7.2.1 ............................................................................................68
7.2.2 .................................................................................69
7.2.3 VFS ...............................................................69
7.3 - Unix System V...........................................................72
7.3.1 ............................................................................................................75
8 Linux................................................................................................78
8.1 ...................................................................................................................78
8.2 p p Linux ...........................................................78
8.3 .............................................................79
8.3.1 pp ..................................................................................79
8.3.2 ............................................................................................................79
8.3.3 ...............................................................................................................79
8.3.4 .....................................................................................80
8.4 .....................................................................................................80
8.5 (VFS) .................................................................80
8.6 EXT2 (The Second Extended File System) ..............................81
8.6.1 ....................................................................................81
8.6.2 p EXT2fs ................................................................81
8.6.3 EXT2fs ..........................................................81
8.6.4 pp ......................................................................................82
8.6.5 p .................................................................83
8.6.6 EXT2fs ...........................................................................................83
8.6.7 p EXT2fs ...............................................................................84
8.7 NFS...............................................................................85
9 Shell....................................................................................87
9.1 shell-.................................................................87
9.1.1 . ........................................................................................87
9.1.2 .................................................................................89
9.1.3 . ..................................................................................91
9.1.4 ............................................................................................91
9.2 shell......................................................................................................92

5
9.3 , .........................................93
9.4 ...............................................................................................93
9.5. .......................................................................................94
9.6 if...............................................................................................94
9.7 test...............................................................................................................94
9.8 ........................................................................................................................95
9.8.1 ...........................................................................95
9.8.2 ....................................................................95
9.9 case..............................................................96
9.10 shell- ......................................................................96
9.11 ................................................................................................96
........................................................................................................................100

1
1.1
,
-

(Hardware)

1.2
,
-. ,
16 , 13 ,
, . .
, 23 , , ,
, . , ,
. -
, .
, , .
,
,
. ,
, , - ,
, . ,
,
, ,
, . ,
, , ,
, , ,
.
,
, , .

1.3
, , ,
. , ,
, .
, , , , ,
, .
, , ,
.
, .
, , .
, :

- , , ,
, ;

- ,
, - ,
.

, ,
, . , ,
,
, .

7
1.4
(1945 -1955)
,
. " " - ,
,
. ,
.

. 40- .
, ,
. -
,
- .
. ,
.
,
.
(1955 - 1965)
50- ,
- .
, ,
.
,
.
,
- . ,
.
,
.
, ,
.
,
, .
, , .

(1965 - 1980)
1965-1980 .

, ,
.
- .
- , ,
IBM/360. 60-
/-.
- .
.
, ,
,
. ,
, "".
, , ,
.
.

8
, , OS/360

.
. -
, .
-, ,
( ),
( ).
, .
- (spooling).
,
, , ,
, .
. ,
,
.

(1980 - )

().
. ,
.
PDP-11, .
,
.
,
"" , .
: MS-DOS UNIX.
MS-DOS ,
Intel 8088, 80286, 80386 80486.
UNIX "-" ,
RISC-.
80- ,
.

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

1.5 UNIX
1965 1969 Bell Labs General Electric
Multics.

,
. , MAC (
Multics).
-, .
- ,

9
, (
). , -
, , ,
(, , ,
).

.
-, Multics
. ,
,
,
, . ,
, (
) ,
. ,
,
.
, UNIX.
, -
. ,
,
, . ( ,
Multics .
).
.
Multics,
, ,

. Multics, , ,
, .
Multics .
Multics
Bell Labs, , , .
, , ,
- , .
, UNIX
"Space Travel". 1969 Honeywell 635,
MAC. ,
Bell Labs ,
General Electric 645. GE-645,

, Space Travel. ,
18- PDP-7 4
.
, PDP-7, PDP-7
. ,
Bell Labs
. PDP-7 , :
inodes, ,
,
. UNIX,
.
.
UNICS (Uniplexed Information and Computing System). , ,
, Bell Labs Multics. UNICS
UNIX ( , ).

10
UNIX Bell Labs.
, PDP-7. -,
( ). -,
. 1971
16- Digital Equipment PDP-11/20, UNIX.
,
, . PDP-11
. , roff
( ).
1971 UNIX ("
"). " "
. : UNIX
.
1972 . " "
, ("B").
BCPL.
("pipes").
, ,
. . BCPL
, ,
. . ,
, ,
. ("C"). 1973 .
25 UNIX, " ".
1974 Communications of the ACM
"UNIX Timesharing Operating System",
. UNIX .
Bell Labs, UNIX
( ).
" " UNIX
. . ().
1975 Bell Labs " " UNIX, V6
UNIX. Bell Labs.
.
UNIX V6
. ,

.
, 1976
/ (stdio),
. stdio
,
.

. UNIX V6 PDP-11/70. ( BSD
- Berkeley Software Distribution, - Sun
Microsystems) .
-, UNIX ,
PDP-11, 1977 . ,
. ,
, UNIX .
. ,
(PDP-11/40 UNIX V6). , .
, PDP-11.
32- Interdata 7/32,
, .

11
"" Interdata 7/32 OSMT/32

32- UNIX V6.
(,
PDP-11) .
(, ) 1977 ,
Interdata 7/32.
UNIX, .
, UNIX V6
"" OSMT/32.

. , ( )
UNIX 32- .
Bell Labs
. Bell Labs
Interdata 8/32 (, Interdata 7/32). , Bell Labs
PDP-11. 16-
UNIX (
). 32-
.
32- Interdata 8/32
( ) UNIX Interdata ,
UNIX .
, 32- .
union, short integer, long integer unsigned integer.
, , ,
, .. ,
, "
" ( K&R).
UNIX,
UNIX V6 PDP-11.

, .
" " UNIX ( UNIX Version 7).
PCC (Portable C-Compiler),
sh, Bourne-shell,
.
UNIX Version 7
.
Interdata 8/32.
Interdata 7/32. ,

UNIX - . , , UNIX
.
, 1976
. .
UNIX .
, .

, UNIX V6, ,
ex ( vi) .
Berkeley Software Distribution (BSD 1.0). BSD ,
. , BSD
UNIX V6. UNIX V6,
BSD (BSD 2.x)
.

12

VAX Digital. BSD Bell Labs
UNIX Version 7 32- VAX. UNIX
32/V.
. - BSD 3.x.
BSD TCP/IP
(Transport Control Protocol/Internet Protocol).
. Bell Labs , BSD
UNIX ,
Bell Labs. , UNIX BSD (
!), Bell Labs ,
BSD, , BSD 4.1 (1980 ), ,
UNIX BSD Bell Labs.
1978 Bell Labs UNIX
UNIX (UNIX Support Group - USG). ,
Bell Labs. , UNIX
- .
,
. -
,
,
. , UNIX .
UniSoft Corporation, UNIX
UniPlus+. Microsoft Corporation Santa Cruz Operation (SCO)
UNIX XENIX. 70- UNIX-
, Zilog, Intel, Motorola ..
UNIX.
1982 USG Bell Labs UNIX,
UNIX System III. UNIX Version 7, V/32
UNIX, Bell Labs. 1983 American Telephone and
Telegraph Bell Laboratories (AT&T Bell Labs) UNIX System V. Bell
Labs , AT&T System V.
, System V .
UNIX System V ,
(- ). UNIX System V
UNIX System III. UNIX System V
, .
1984 USG UNIX (UNIX System
Development Laboratories - USDL). 1984 USDL UNIX System V Release 2 (SVR2).
,
(copy-on-write),
( , BSD) .. UNIX
100000 .
1987 USDL UNIX System V Release 3 (SVR3).
,
(Remote File Sharing - RFS), , ..
,
. 1987 750000 UNIX, 4,5
. .
UNIX . 1.1.

13

1.1 - UNIX

14

2 ,

2.1

(, , ),
, ,
.
.

2.1.1

. , ,
, ,
. , ,
, ,
, ,
, .
.
:
(, MS-DOS, MSX)
(OC EC, OS/2, UNIX, Windows 95).

, .
,
, .
, ,
, , , .
.
:
(MS-DOS, Windows 3.x, OS/2);
(UNIX, Windows NT).


. , ,
.
.
.
( )
.
:
(NetWare, Windows 3.x);
(Windows NT, OS/2, UNIX).

.
, -
.
, , ,
, .

, .

15
.
.
, ().
.
- .
.

. Solaris 2.x Sun, Open
Server 3.x Santa Crus Operations, OS/2 IBM, Windows NT Microsoft NetWare 4.1
Novell.

: .
,
.
, .
,
- . ,

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

2.1.2
,
.
, -, , .
, .
, , .
, ,
.
, ,
, .
.

, .
,
.
, , IP, IPX, Ethernet
.
,
,
.
,
,
.
. -
,
, .

,

16
, .
Digital Equipment VAX.
Microsoft
, Windows NT. UNIX.
, ,
, ,
, .
UNIX. ,
. , ,
- , , ,
.

2.1.3

:
(, OC EC),
(UNIX, VMS),
(QNX, RT/11).

, .
,
.
:
, ;
, .
, ,
, ; ,
,
-. ,
, , "" .
,
.
,
, , - -.
,
. , ,
, , ,
-, .
, .

- - .
,
.
, ,
. , ,
, ,
. ,
, ,
, , "" , , ,
.

, .

17
,
, , , ,
, , , ..
,
, , :
, , , ,
. ,

( ).
, - .

,
.
,
, , -
.
.

2.1.4

, .
:
- .
, ,
,
.
,

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


, .
MS-DOS, Windows, UNIX (POSIX),
OS/2 .
,
,
.

. ,

.
: , ,
(RPC)

18
, ,

, .

19

2.2 .

, ,
, .
. ,
, ,
, .

2.2.1
,
, .

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

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

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

20

.
,
.
,
,
.
, ,

, ,
, ,
.

2.2.2
, ,
, ( )
.

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

.
,
,
. , ,
( ) .

,
, . ,
,
.

, .
, . ,
. ,
,
.
,
; ;
; ,
, ,
,
.

2.2.3

.

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

,
.
, -,
, .
. , ,
, , , , ,
-, , , - ,
. ,
,
, ,
.
,
, .
, U-1496 ZyXEL,
, Hayes- . ,
,

.
(,
S/2).
,
, .
,
- -
.

,
, .

- .
UNIX
. ,
,
.
, - , .

.

2.2.4

, ,

22
.
.

. ,
, .
, , , , .
. ,
. ,
, , ,
, .
, , ,
, .
,
, . ,
, ,
. . , , .
, ,
.
. .
'
.
, ,
, . ( ,

.)
. ,

.

2.2.5
,
,
. , ,
, .
,
. ,
, , , .
, , ,
.
,
.

, (Application Programming
Interface, API). -
.

API.
, API ,

, API. ,
API UNIX, Posix,

23
UNIX,
.
API . ,
, .
, ,
, / . ,
, .
, .
c , ,
, . ,
.
/ ,
.
, ,
.

2.2.6

, , .
, .

.
, .

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

24

3
,
, :
, ,
, , - .
. ,
.
. ,
API. ,
, ,
.

, .
, ( , ,
, ).
,
,
(. 2.1).
, - ,
. , ,
,
.

, , .
Microsoft Web-Browser, ,
Windows NT 4.0 Windows 95/98.

3.1 -
:
- ,
, , , ,
;
- ,
, , ;
-
, ;

25
- , ,
, - . .
, ,
, (. 2.2).
-
. ,
, -: ,
. ,
.
.

. 3.2 -
, , ,
,
.
. .
, ,
.

3.1


, .
,
, .
,
.
,
,
.

.
(user mode) ,
(kernel mode), (supervisor mode). ,
,
.

26
,
, (. 2.3).
.

3.3 -

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

, .
. ,
,
, ,
, .
,
(, IBM
Pentium)
.
,
, ,
- , . ,
.
,
.
, ,
, , . , ,
Intel, OS/2
, Windows NT, UNIX
.
, ,
.
, , ,
.
, .

,

27
.
,
, ,
.

,
.
.
,
, .

,
(. 2.4). -
,
.

3.4 -
,
, , , . ,
UNIX,
VAX VMS, IBM OS/390, OS/2, Windows NT.
,
. ,
NetWare Novell
Intel x86/ Pentium ,
NLM (. 2.5).
, -.,
, , .
NetWare
,
.

3.5 - NetWare

28
,
, .
| Intel 8088/86,
IBM. MS-DOS,
Microsoft , msdos.sys io.sys,
( ,
), command.corn,
. MS-DOS ,
. 2.2. MS-DOS,
, MS-DOS (
, MSX, /)
.
Intel ( 80286)
MS-DOS.
,
8086/88. , ,
.
8086/88
,
, .

3.2
, ,
, :
, , , ,
(. 2.6).
, ,
. ,
, .

3.6 -
1
, .
. ,
, (. 2.7).
( )
, , ,
. ,
.
, ,
.
.
,

29
, ,
.
- ,
.

3.7 -

.
.
.
, , ,
.
, . 2.8.
, , ,
, ,
: , ,
, . .
- . ,
.
.
- ,
, .
HAL Windows NT.
. ,
, ,
. .
,
.
, ,
, ,
,
.
. ,
.
( ) , -

30

, .
,
-,
.

. ,
.
, ,
, , ,
,
.
() .
, ,
, ,
, -. ,
,
.
.
,
. API,
, ,
. , UNIX
fd = open (" /doc/a, txt", 0_RDONLY)
a.txt, /doc, read(fd, buffer, count)
, buffer,
.
,
.

3.8 -
.
. ,
, NetWare, , , ,
.
,
.
, .
, , , ,
. -,

31
,
, ,
.

.
, .
.

. ,
,
.
, .
:

, .
, ,
UNIX, ,
, Windows NT,
.
[1,2]
:
,
;
. - ,
, , ,
/.
, .

3.2.1

. , ,
,
,
. -
.

, , ,
.

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

3.4
3.4.1

.
,

32
, ,

(
).
-
. , ,
, .
. .
,
(. 2.9). .
- , , (
!) , ,
, -,
.
. , ,
.

3.9 -
,
. ,
, , .
,
, ,
. ., , .

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

.
.
, ,
(. 2.10). , ,

33
, ,
.
,
. , ,

. ,
, ,
. ,
-,
.

3.10

3.4.2
, ,
, , ,
,
. ,
.
, -
,
. .

.

.
.
, .
,
, . ,
,
.

-
,
(, STREAMS UNIX - Windows NT).


.
.

, ,

34
. ,
. ,
,
, .

.
,
, :
.

35

4 ,

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

, .
, .

4.1
()
:
- ,
;
- , ,
, , ,
-, , -
;
- ,
:
, , .

, .
4.1.

4.1 -

, - ,
.
, .
,
, - ,
"" , ,
. .

36
, ,
.

4.2

. , ,
.
, , ,
-,
.. .
,
: , ,
, .
(, UNIX) ,
, .
,
.
,
.
,
. , , ,
, .
, ,
.
,
. - :
1. , ,
;
2. ;
3. .

4.3
.
: FIFO ( , ), SJF ( - ), HRN
( SJF, ).
:
1. ;
2. ;
3. "" "" .
,
.
,
,
.
: , , ,
.
, ,
, :
,
,
,
, .
, , ,
,
. ,

37
,
. , 2.1,
, .
, , .
, ,
. ,
(, - -),
.
: , " -
" (FIFO) " - " (LIFO).

.
. , FIFO,
. ,
- , , ,
. , ,
.
, ,
.
, ,
. ,

, .
,
, .
"" . - ,

, , : , .
,
. , .

, ,

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

, . , ,
/ .

4.4
- (preemptive)
(non-preemptive).
Non-preemptive multitasking - -
, , ,

38
, ,
, .
Preemptive multitasking - - ,

, .
preemptive non-preemptive
, ,
, . , . ,
, .

, -
. ,
, .
preemptive non-preemptive
.
,
, , .
:
, ,
, .

4.2 - () ; ()


. , ,

- ,
(, ) .
, .

39
,
, ( ).
- , , ,
, , ,
. ,
, .
non-preemptive ,
, ,
. ,
.
, .
,
.
"" ,
. ""
, .
, , ,
.

, ,
,
.
,
"" . ,
: ,
. nonpreemptive .
-
NetWare, , ,
.
Windows 3..
,
(UNIX, Windows NT, OS/2, VAX/VMS),
. ,
, OS/2 Warp Windows 95.
.

40

5 UNIX
UNIX .

,
. ,
.
, , .
, . . ,
, . ,
, , ,
. (run
levels), ,
, , .

5.1
, ,
.
, ( ,
, /
, .
( ) .
,
.
, UNIX .
, ,
, , .
,
. , , who(l) cat(l),
. ,
(, FTP, Telnet), .
UNIX . ,
, .
,
.
, .

UNIX . UNIX
, (signals), (pipes),
(shared memory), (semaphores), (messages) ,
.

5.2
.
.
.
,
, .
: shed ( ), vhand ( ), bdfflush (
) kmadaemon ( ). init,
UNIX. init ,

41
(/etc/init),
.
. ,
( ),
. (
,) UNIX: ,
, . .
.
, ,
.
. ,
. , ,
. . , ls(l)
.
(login shell), UNIX.
, login shell
.
, ,
( ) .
.
,
, .
,
.
shell, .

5.3
UNIX ,
, .
- Process ID (PID).
PID, . ,
(. . ) .
, . .
, , .
, PID
. , .
- Parent Process ID (PPID). ,
.
- (Nice Number). ,
.
, ,
.
( )
, .
- (TTY). , ,
. - .
(RID) (EUID) .
,
.
( ).
, . .
, , .
, SUID,

42

(, ).
- (RGID) (EGID) .
, .

. ,
( SGID).
ps(1) (process status) , ,
:
$ ps -ef head 20
UID

PID

PPID

STIME

root
root
root
root
root
fed
fed
fed

0
1
2
3
164
627
314
3521

0
0
0
0
1
311
304
512

0
0
0
0
0
0
0
0

Dec
Dec
Dec
Dec
Dec
Dec
Dec

17
17
17
17
17
17
17

TTY

TIME

7
7
7
7
7
pts/3
pts/4

0
0
0
7
0
0
0
0

00
01
00
00
01
27
00
01

login(l) ,
login shell, . DID
(EUID) GID KEGID shell ,
/etc/passwd. , ,
.
,
, , , shell.
login shell,
.
, ""
SUID SGID. ,
, , !
. 5.1
.
:
ftinclude <sys/types.h> ftinclude <unistd.h> uid t getuid(void);
uid_t geteuid(void);
gid t getgid(void);
gid_t getegid(void);

.
:
#include <sys/types.h>
#include <unistd.h> int setuid(uid_t uid);
int setegid(gid_t egid);
int seteuid(uid_t euid) ;
int setgid(gid_t gid);

43
setuid(2) setgid(2)
, seteuid(2) setegid(2) .

5.1

shell , , ,
. , .
UNIX .
: , .
UNIX fork(2):
#include <sys/types.h>
#include <unistd.h>
pid_t fork (void);
, fork(2) ,
. . ,
, . ,
,
fork(2). , PID.
, .

44
, , ,
, , . ,
, :

,
,
, ,
,
,
, ,
.
, :
, , , . . fork(2),
,
.
, :
PID,
PPID ,
,
, fork(2) .
. , fork(2)
, .
PID , , , , 0.
fork(2) -1, (,
, ).
fork(2) , ,
, , .
:
main() {
int pid;
pid = fork ();
if (pid == -1)
{ perror("fork"); exit(l);}
if (pid ==0) printf("TOMOK\n");
else printf("\");
}
, ,
. ,
,
. , , ,
: ,
. ,
.
, - ,
, .
, . . ,
(2). ,
.
, ,
/, , .
#include <unistd.h>
int execl(const char *path, const char *arg0, ..., const char *argn, char * /*NULL*/);
int execv(const char *path, char *const argv[]);
int execle (const char *path,char *const arg0[], ... ,const char *argn, char * /*NULL*/, char *const
envp[]);

45
int execve (const char *path, char *const argv[], char *const envp[]);
int execlp (const char *file, const char *arg0, ...,const char *argn, char * /*NULL*/);
int execvp (const char *file, char *const argv[]);
execve(2),
( ),
. execve(2) ,
, . .
5.2 , .
-

5.2 - exec()
fork(2), ,
:
PID PPID,
,
(
SUID SGID).
, ,
,
,
, FD_CLOEXEC,
.

.
, , UNIX.
/
. .

46
fork(2) (2), fork-and-exec
, .
.
, , ( shell)
ls(l). (shell) fork(2), shell.
, shell (2),
, shell. ls(l)
shell, ls(l) . ls(l)
"". ( .
. 5.3.

.
5.3

"" , Ps(I},
, :
:
UID

useri

PID
PPID
CMD

745
1
sh

ls(l), shell fork(2):


UID
PID
PPID
CMD

useri
745
1
sh

useri
802
745
sh

shell (2):
UID
PID
PPID
CMD

useri
745
1
sh

useri
802
745
Is

ls(l) :
UID
PID
PPID
CMD

useri
745
1
sh

fork-and-exec.

47
, fork(2) (2).

.
UNIX fork(2).
fork-and-exec, (2). ""
init(lM), .
" " , , init(lM).
. 5.4 sched vhand
.

5.4 - "" UNIX kill(l)

5.3.1 Unix
,
. 5.5.

5.5 UNIX

48
,
.
(, ), (
, ),
( ,
), ,
(, C
malloc, calloc realloc).
, .
(user-mode).
,
. ,
(kernel mode), ,
, - PCB.
. ,
: - UID,
- GID, - PID, PPID.
5.3.2 .
UNIX
5.6.

5.6 UNIX
, 2 :
.
.
(,

49
).
,
.
.
. Linux .
, .
UNIX "
UNIX".

5.4 UNIX System V


UNIX System V Release 4 ,
.
, .
.
-,
.
, , .
UNIX System V Release 4
. : ,
. UNIX
() , , .
5.2 .
.
.
.
. , , ,
.
,
.
,
, .
:
. .
,
.
5.1 -

(real
time)

(system)

(time-shared)

159 100
99 60

59 0


UNIX System V Release 4
, UNIX System V Release 4 .
,
.

50
, ,
.
, ,
: .
,
.
,
, . ,
, ,
,
, . , , -,
, , .
, .
, , , ,
, .
:
ts_globpri

ts_quantum

,
;

ts_tqexp

,
;

ts_slpret

,
; ,
;

, ;
ts_quantum, , , ,
ts_maxwaite
-,
;
ts_lwait

, ,
ts_maxwait .

proc ,
. ,
:
ts_timeleft , ;
ts_cpupri

ts_uprilim, .
ts_upri
;
ts_nice

nice.
nice,
. , .

SVR4 (multithreading)
, .
UNIX, SVR4,
SVR4.

51

6 - UNIX
-
. ,
; .
(
).

6.1 -
- : - . -
, .
- - . -
, .
, , .
, , , , , ,
, .
.
.
.
.
. ,
, .
, . ,
, , , , ,
. ,
.
.
-. -, ,
-
(, IN OUT i86).
-, . ,
IBM PC 15 , READ, WRITE, SEEK, FORMAT ..
, .
,
, .
, .

6.2 -
-
,
, , , .
.
, .
,
.
-
. , .
, . ,
.
-, , ,
.
, .

52
- ()
() . - , .
, - -
READ ,
. - ,
.
, , . - ,
. - ,
, .
.
:

6.3
,
. ,
-,
. , , , DOWN ,
WAIT , RECEIVE .
,
-, UP, SIGNAL .
,
.

6.4
.
, , .
. , , , ,
, .
,
. n .
, .
,
, , .
-, , ,
.
, , , , ,
. , ,
.
,
. ,
, , ,
. - (,
), .

53
6.5
- .
,
, ,

.
:

- ,

.
,
, , .
,
.

. .

, ,
.

6.6
- ,
, .
, -, .
, ,

count = write (fd, buffer, nbytes),


write .
-. ,
. printf ,
, , ,
ASCII write . , -
.
-
(spooling). - .
, - .
, ,
- ,
. - ,
. ,
. , ,
. -
, .

54
6.7
- , ,
, ,
.
" " :

, , , ,
, ,
,

, , :
, , , , , .

6.7.1
. ,
, .
. FAT
8.3 (8 - , 3 - ),
UNIX System V 14 .
,
, ,
. , ,
. , Windows NT NTFS
, 255 ,
.

, .
,
() , . ,
.
,
. , Windows 95
VFAT, FAT.
VFAT .
, FAT
,
.
.
,
.
, .
, ,
- .
.
UNIX.

6.7.2
: , , -.
.
, ASCII-. ,
.. .
ASCII-, , ,
.
- .

55
- , -,
-,
. ,

. , -, -.
- , , ,
(, , , ,
), - ,
, . , ,
().
,
:
,
,
,
,
" ",
" ",
" ",
" ",
"/",
"" ( ),
,
,
,
,
, ,
,
.
,
MS-DOS, , ,
UNIX ( 6.1).
, ( 6.2).

6.1 - : - MS-DOS (32 );


- UNIX

56
. ,
, - .
MS-DOS , UNIX' - . ,
,
, .

6.2 -
- ; - (); - ()

6.7.3
,
. -
, .
,
. 6.3
. .
( )
, ,
(- ).
, .
UNIX MS-DOS .

57

6.3 -

6.7.4

, . - . -
, . ( 6.4,),
, .
.
- . . -,
, ,
, -, ,
,
( 1 ) .
-
( 6.4, ).
. -
. , -
, .
, .
: , ,
, .
, , ,
( ),
, .

58

6.4 - - ; - ;
- ; -
, , , FAT
MS-DOS, .
- . ( MS-DOS
FAT). ,
.
, : -,
,
, , -,
, , .

, . UNIX ,
, .
13 . 10 ,
. 10 ,
11- , 128
. , 10+128 , 12- ,
, 128 , 128
. , , 10+128+128(128, 13-
, , 10+ 128 +
128(128 + 128(128(128.

6.7.5
-
, .
.
:
,
,

59

,
,
,
,
,
,
,
,
,
,
,
.
,
, - ,
(. 6.1).
.
. , UNIX :
, .
6.1

,
;

,
( ) ,
.

6.7.6
,
(, ), . ,
, , .
, .
, ,
. - ,
. .
,
.

60
. ,
-.

6.7.7

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

6.5.

. ,
(, MS-DOS), , ,
,
. ,
,
. UNIX, ,
(i-node).
, :
, , . ,
.
, .
(, HPFS)
, .
.
, ,
. ,
, , .
,
, ( )
. ,
.
. ,
l, n- l((n-1) .

61
-
(), .

6.6.
: V - N - S -

:
n - ,
s -
n = N + [S/V], [S/V] - S/V
s = R [S/V] - S/V
,
, .
-
, , ,
. 6.6
. ,
, .
, .
,
.

6.7.8
,
. , MULTICS,
.
: MAP () UNMAP ( ).
,
.
, , f 64
512 . ,
512 , 0- .. ,
512 + 1100 1100 .
, SEEK WRITE.
,
. ,
512 , 0
. , 512 + 1100 ,
, , ,
. ,
.
.

62
, .
, k-
k- . 6.7 , . , . , , abc. ,
, ddd.
- -
, mov.
READ WRITE . UNMAP
, . ddd
, .

6.7
) ;
) abc
ddd
-
, . -,
, ddd.
, ,
. , 0,
0 ( ). , 10 .
, 100 . ?
. , , , .
(), ,
. ,
, .

.
, , , ,
.
MAP , , . , ,
, .

6.7.9

.
, .
( 6.8),
( Windows 95, ,
- installable filesystem
manager, IFS).
, .
, - .

.

63
, .
.
,
.
, ,
.
, . , ,
.
, .
- ,
. -
.
(
), , (

, , ), ,
.

-
- , , ,

.
.

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

6.8 -

64

7 UNIX Linux System


UNIX System V Release 4 VFS (Virtual File
System),
. VFS
, .
, UNIX System V Release 4:
s5 - UNIX System V, UNIX
System V AT&T;
ufs - , UNIX System V Release 4,
SunOS, ,
Berkeley Fast File System (FFS);
nfs - NFS Sun Microsystems,
;
rfs - Remote File Sharing UNIX System V Release 3.
NFS, UNIX System V
Release 3 ;
Veritas - ;
specfs -
, /dev;
fifofs - VFS FIFO,
(pipes), STREAMS;
bfs - .
, ;
/pro -
, ;
/dev/fd -
.
,
.

7.1 s5
7.1.1
UNIX s5
. ,
.
,
,
, .
- , ,
. ,
.
- , - -,
.

7.1.2
s5 ,
, .
root.
: ,
.
(, ). ,
. ,
, , mount.

65
, (. 7.1).
:
, , man,
dummy. man
.
( 7.2).
.

7.1 - s5

7.2 - ( )

66
7.1.3
UNIX - , .
. 14
, . .
,
. "/", ,
, /mnt/rk2/test.c, mnt rk2 - , test.c - .
, ,
( ).
" ", , ,
.
. ,
.
"/". , /mnt,
test.c rk2/test.c.
7.1.4
UNIX s5 : ,
. - , -
, , . ,
, .
- , .

.
.

7.1.5
.
13 . 10
10 .
10 , 11 ,
128 . , 10+128 , 12-
, , 128 ,
128 . , 12-
. , , 10+128+128 2 ,
13 .
2 113 674 . s5
512, 1024 2048 .

7.1.6
, ,
, (inode) .
- 64
, ( 13 ), ,
, ,
.
. .

.
, .
16 , 14
, 2 - . s5
.

67
. ,
. ,

, .
. ,
.
s5 . 7.3. ,
, :
(boot), ;
(superblock) - :
, , ,
,
;
,
;
, , -.

,
.

7.3 - s5
,
, .
.
,
.
.
. :
1. , ; , . ,
.
2. ;
, .
3. ,
. , file,
(, ),
, , .
4. ,
.

68
7.2 VFS
UNIX System V Release 3 (File System
Switch, FSS), .
. FSS
, , .
.
, Sun Microsystems,
. - Virtual File System
(VFS). UNIX System V Release 4 .
VFS - ,
, .
, ,
open, read, write , ,

(, s5, ufs, nfs ..). VFS
, : ,
( ,
..) . VFS, ,
.
VFS -
, - vnode,
- inode, VFS ,
vnode. inode ,
inode s5.
, .
VFS :
,
,
,
,
.
,
s5.

7.2.1
UNIX System V Release 4 - ,
symlink. , , .
"" ,
.
, inode. ,
, .

. .
, :
readlink - ,
. , .
lstat - stat,
.
lchown - chown,
.

69
7.2.2
- . ,
, , , ,
" - " (FIFO). UNIX
- pipes,
. , - ,
,
-.
, ..
. -,
, . UNIX System V
Release 4 STREAMS.
UNIX System V Release 4
( )
. .

7.2.3 VFS
UNIX System V Release 4 vfssw [ ],
, . vfssw
:
;
;
, , VFS
;
, UNIX.
vfssw:
struct vfssw vfssw[] = {
{0, 0 , 0 ,0 }, -
{"spec", specint, &spec_vfsops,
0}, - SPEC
{"vxfs", vx_init, &vx_vfsops,
0}, - Veritas
{"cdfs", cdfsinit, &cdfs_vfsops,
0}, - CD ROM
{"ufs",
ufsinit, &ufs_vfsops,
0}, - UFS
{"s5",
vx_init, &vx_vfsops,
0}, - S5
{"fifo",
{"dos",

fifoinit, &fifovfsops,
dosinit, &dos_vfsops,

0},
0},

- FIFO
- MS-DOS


. .
vfsops, , ,
7 , UNIX System V Release 4 7
:
VFS_MOUNT

VFS_UNMOUNT

VFS_ROOT

vnode ,

VFS_STATVFS

VFS_SYNC

VFS_VGET

vnode ,

VFS_MOUNTROOT

70

7.4 - VFS
VFS_MOUNT UNIX
,
VFS_UNMOUNT . VFS_ROOT
, vnode,
. VFS_ROOT vnode,
. VFS_STATVFS
,
, .. VFS_SYNC
. VFS_MOUNTROOT
, , /
. , , UNIX
System V Release 4 rootfstype,
, "ufs".
, UNIX System V Release 4
,
(. 7.4).
7.1
VOP_OPEN

VOP_CLOSE

VOP_READ

VOP_WRITE

VOP_IOCTL

- /

VOP_SETFL

VOP_GETATTR

VOP_SETATTR

VOP_LOOKUP

- vnode

VOP_CREATE

VOP_REMOVE

VOP_LINK

VOP_MAP

71
, (s5, ufs
..), , ,
VFS.
vnodeops, 5.7. ,
,
UNIX. , ,
vnodeops ,
, VOP_OPEN, VOP_CLOSE, VOP_READ .. <sys/vnode.h>
. , vnodeops
.
- ,
, :
, .
VFS VOP_LOOKUP,
vnode.
vnode,
. 7.5. vnode
v_vfsp v_op.
v_pages ,
( ,
). vnode
- inode,
. , vnode
(locks),
.
vnode, offset,
.

7.5 - - vnode

file, , s5,
, , (, ).
file , :
flag - ( , ..);
struct vnode * f_vnode - vnode ( s5
inode);
offset - /;

72

struct cred * f_cred - , ,


( );
file,
.

7.6 -
file . 7.6.
file vnode
() , file
vnode.
vnode - .
, vnode,
. , file
f_vnode vnode, . ,
( , ),
, , vp (
segvn_data) vnode .
UNIX System V Release 4
vnode. (, open),

. , vnode,
, ,
vnode.
UNIX System V Release 4
vnode.
, 800 vnode.

7.3 - Unix System V


- UNIX
. UNIX : -.

- -
,
.
, .
.

73
- ,
:
:
- /,
- /,
- ,
- -.
- , :
- ,
- ,
- .
- .
, -
- .
5.14.
, .
bwrite - .
. , ,
-.
. ,
-.
bawrite - .
, - .
- , .
,
- , .
bdwrite - .
, , ,
.
bread getblk - . ,
. ,
getblk ,
, . bread
-
. , .
getblk , ,
, , .
,
.
. .
.
,
. , -
. ,
,
, .
( )
.
,
UNIX System V . UNIX System V Release 4
, .
, ,
, inode, ..
.
vnode,
vnode/offset. vnode/offset

74
segmap, segvn, .
, segmap, .
.
, , .
,
as.
segkmap, .

7.7 -

7.8 -

75
7.3.1
- (),
.
( 7.8)
:
bdevsw - -
cdevsw - - .
:
(- -),
(, , , , ,
..)
.
bdevsw cdevsw.
, .
. , ..
. ,
UNIX .

bdevsw cdevsw.
- ,
, .
.
cdevsw.
- .

7.9 -

76
7.9 -
.
, , ,
. , ,
, . ,
sleep ( wait).
. ,
. ,
,
wakeup.
.
-
, . , bdevsw
(rktab).
- , ,
, , ,
.

7.10 - RK

77
7.10 . rkstrategy - -
, .
( rkstart)
.
, .
, .
-
.
- rkintr.
-. ,
, -
.

78

8 Linux
8.1
Linux Unix- p , p p PC386. p p pp p Minix p p
p Minix.
p p: p 16 , p
64 , p p pp
p 14 .
pp p , p p p
Linux. , "Extended File System" (Ext fs) "Second Extended File System" (Ext2fs),
p p .
p p Linux. p p
p Unix. p p p
(VFS) Linux, pp p Ext2fs
p p . p p pp
Linux BSD.

8.2 p p Linux
p p Linux pp p Minix. p
p , pp , Linus
Torvalds p p Linux Minix. p
pp p
.
p, pp Minix, ,
pp Linux.
p p p Linux, pp p
(VFS). p VFS Chris Provenzano, p Linus
Torvalds p p p. pp p A.3.
p VFS, p 1992 pp EXTfs
(Extended File System) p Linux 0.96c.
p Minix: 2 ,
- 255 . p
Minix, p p p. p p
, pp p .
pp
pp, p : p
p ppp, p pp .
p p 1993 -p
: Xia EXT2fs (Second Extended File System). , Xia
Minix c .
p , p p p p
p . p p, EXT2fs EXTfs
. p. p
pp p A.4.
, p
p. Xia , EXT2fs, . p
p p p EXT2fs,
. p EXT2fs
p - Linux.
p p ,
p p .

79
Minix FS

64

64
16/30

.
p
p

p
pp
pp
p

Ext FS

Ext2 FS Xia FS

64

255 .255 .248 .

8.3
Linux ,
p Unix: p pp (inode),
p , p , p
p p p p.

8.3.1 pp
p pp, pp. pp
p , p , p , ,
p , pp, . p ,
ppp , p pp. p p
p / , p pp p ,
p p , p p p
/.

8.3.2
pp p. p
.
- . , , p
. p p pp . - p
, p p pp.
, pp p pp, pp
p.

8.3.3
Unix . pp
. pp p , p ,
p p . , p
pp p pp, pp.
p p pp,
p .
p
( p ). ,
( p
).
Unix . , p
, . p p p , p pp
pp, p p pp (..
) ppp .

80
pp, , p p
.

8.3.4
p , p, p
. p , p
pp . p p p p
pp pp, , p p
p ppp .

8.4
Unix-
. .
.
: .
, ,
.
/ ,
. ,
, , .

8.5 (VFS)
Linux ,
, .

/.
Unix-
.
- ,
, , VFS. ,
,
, , . ,
, /.
VFS , .
, :
, .
VFS .
, .
:
, .
.
,
VFS. , ,
VFS .
:
, , ,
,
. ,
, VFS .
VFS : inode .
,
, . inode

81
, (, create
unlink), ,
(, read write).

8.6 EXT2 (The Second Extended File System)


8.6.1
EXT2 pp p , p
p EXT (Extended File System). p pp
, p pp Unix p
.
p, EXT2 p. p,
p , p p p
. , EXT2 pp pp
.

8.6.2 p EXT2fs
EXT2fs p p Unix: , ,
p .
EXT2fs p ,
p. p p p 2 ,
VFS p 4 . p, p
p p.
EXT2fs p . p
. pp p 255 . p p
1012.
EXT2fs ppp p root. 5 %
, pp
p p p p .

8.6.3 EXT2fs
p Unix, EXT2fs p p
, p Unix.
p p p p p p .
p . p , , ,
p.
p p p ,
p. mount pp p .
BSD, p p,
p . System V . setgid ,
p p ,
p p setgid. p ,
p p p.
EXT2fs p ,
BSD. mount pp ( pp,
, ) p p .
p,
p p. , ,
p p, p p ,
p p pp .
EXT2fs p p pp .
pp 1024, 2048 4096 . p p
p / ( p ), , ,
p . p p, p
p pp. p

82
p, , p
pp.
EXT2fs p . p p
, . p
, pp. pp p pp
p p . , pp, ppp
pp, p, p p.
p p 60 .
p pp .
EXT2fs . p p
. p p read/write,
'Not Clean'. p p
p read-only, 'Clean'. p p pp
, p p pp
. p p . p p p
, 'Erroneous'. pp pp
p p pp ,
'Clean'.
pp p p p
p, EXT2fs pp pp .
p p p . p,
p p read/write. (
p p), pp p pp,
'Clean'. p pp p pp
p p. p pp,
pp pp.
EXT2fs p p p. pp tune2fs
: p p . p p p ,
'Erroneous' p :
p , p p read-only
p, pp pp .
p.
p pp.
, ppp root.
, p p, p
p p.
p p . p
, , p p p ,
p. pp p p
p, p pp.
EXT2fs , 4.4
BSD. p : p
. p . p
, , p p p ,
. p.
p , p
.
8.6.4 pp
pp EXT2fs p BSD.
p p . p p p
BSD FFS. , p p ,
p p p p p
pp.

83
p p p p
(p pp ), p
(p , p pp, pp ).
p p ,
p pp p p , p
p p p. pp
p, p pp
, p p p p /.
EXT2fs p p .
p p pp, , .
p p p
pp.

8.6.5 p
EXT2fs p , p p,
p p p .
EXT2fs p. , p p
p / p p . p, p
p, , p , p p.
p p p .
EXT2fs p p p.
p pp
. p p p,
pp. p p p p
pp .
p , EXT2fs p p 8 p p
. p p p
. p , p
.

8.6.6 EXT2fs
p pp EXT2fs pp p pp
, pp libext2fs. p ,
p p EXT2
p p.
EXT2fs (mke2fs, e2fsck, tune2fs, dumpe2fs, debugfs, p.)
. p ,
EXT2fs p
EXT2fs.
p EXT2fs p p,
pp, p p p p .
pp, EXT2fs p p 4.4 BSD
p . p p Linux
(p , ,
EXT2fs).
EXT2fs p p . p -
p, . pp p p
, , .
pp .
p p p . pp, EXT2fs,
pp , .
p pp , p
pp.

84
p pp p.
pp, pp, pp
pp. p p pp.

8.6.7 p EXT2fs
EXT2fs pp p p. p
, pp EXT2fs.
pp mke2fs p, p
EXT2fs.
pp tune2fs p pp .
pp p A.4.3, p
, p , p pp
, ppp root.
, p p pp pp .
E2fsck p p p
p p . p pp e2fsck pp Linus
Torvald fsck Minix. , p pp p
EXT2fs p p
p pp, p p p.
pp e2fsck pp p,
p. pp pp p p ,
p p p e2fsck p, p pp
p p. , , p pp pp
p p p
.
p p e2fsck p pp
pp pp . p, p p
pp p . pp pp
pp , pp
p. p p pp p ,
pp.
e2fsck , p p p pp,
p 1B 1D - p
, pp.
p p p, pp
pp. p p /
p, p p. pp p
. p, p p
pp pp .
p p pp .
pp , p . e2fsck
pp p pp p p,
p p . p pp
p pp p
( p p p).
p pp , pp '.'
'..', p pp '.' . (p pp
'..' p p p.)
p p p, p, p ,
p p.
, p p p p /
. p, p p, p p, p ,
, p p pp 5-10% p
e2fsck.

85
p p pp . E2fsck pp
p p, p, p p p.
pp '..' . , pp
p, /lost+found.
p p e2fsck pp pp
pp pp p ( p p
p p) p , p p p
p p.
/lost+found.
, , p e2fsck pp p .
p p pp, p p
p, , p , p
p ppp.
p p . Debugfs -
pp, p . ,
p p EXT2fs, pp p
. Debugfs p p pp
, p p
e2fsck. , pp p ,
. p p .
debugfs p p read-only . p read/write
-w.

8.7 NFS
Network File System (NFS)
Sun Microsystems. NFS UNIX-.
UNIX, , MS DOS. NFS , ,
MS-DOS- UNIX-.
NFS -
. , .
NFS . NFS-
.
. , , /etc/exports,
.
. Sun - ,
,
. ,
.
, .
NFS ,
, ,
.
,
. .
NFS- .
-
. , ,
. ,
, , , (inode) ,
.
.
. :
,

86
, , ,
, .
NFS- .
-
. , , , ,
. UNIX NFS, open
close. open close .
, , (lookup)
. open, lookup
. read ,
, , .
, .
, , ,
, . , ,
, stateless.
UNIX System V RFS ,
, .
, , , .
, ,
, . NFS .
NFS . UNIX
, . ,
. stateless-, NFS,
, , . , NFS
.
NFS ( 8.1).

8.1 - NFS
- , OPEN, READ, CLOSE.
, (VFS). vnode ,
. , vnode,
MOUNT, READ, OPEN.
, ,
, , ,
.

87
, .
,
, MOUNT .
vnode -
rnode ( inode) . vnode
- rnode NFS -, inode .

9 Shell
9.1 shell-
Shell , ,
(shell-). Shell-
(),
.
9.1.1 .
:
;
< > ;
& () ;
&& ,
;
|| , .
( )
, , ,
, .
, () "find" ,
"/", "conf", "pwd" .
:
-------------------------------| $ find / -name conf -print & | "find"
|
|
| 288
| (PID)
|
|
| $ pwd
| "pwd"
|
|
| /mnt/lab/asu
| "pwd"
|
|
|$
| shell
|
|
| /usr/include/sys/conf
| "find"
|
|
------------------------------- , ,
- . "wait [PID]".
() . ,
, "sh".
"{}" "()" .
, :

88
k1 && k2; k3
k1, k2 k3 - - , "k2" "k1";
"k2" (.. "k2" , )
"k3".
k1 && {k2; k3}
("k2" "k3") "k1".
{k1; k2} &
"k1" "k2".
( ) ,
"CTL/C" .
. ,
() (PID). ,
,
ps -aux
(PID), , ,
, ..
, ,
"849" "866".
kill -9 866 849
.
, (
).
.
(, ),
, () .
,
.
"()", ,
shell.
"/mnt/lab/asu".
cd ..; ls; ls
"ls" 2 "/mnt/lab",
(cd ..; ls) ls
"/mnt/lab", "/mnt/lab/asu", ..
shell, .
shell .

89
9.1.2
() - "stdin" UNIX ,
() - "stdout" .
- "stderr", .
, , .
().
">" ">>" .
ls >f1
"ls" "f1" (
). "f1" , .
pwd >>f1
pwd "f1", .. ">>"
, .
"<" "<<" .
wc -l <f1
f1.
ed f2 <<!
"f2", .
, "<<" (. . "!"). ,
"!".
.
wc -l <f3 >f4 wc -l >f4 <f3
: "f3" "f4".
, ,
"|".
ls | wc -l
"wc",
.
' , , :
cat f1 | grep -h result | sort | cat -b > f2
"f1" ("cat") , "result" ("grep"),
("sort") , ("cat -b") "f2".
UNIX ,
. "/dev". , "lp" - ; "console" ; "ttyi" - i- ; "null" - () ().

90
, ,
ls > /dev/lp
, f1 < /dev/null "f1".
sort f1 | tee /dev/lp | tail -20
"f1" , 20
.
. : 0 - stdin, 1 - stdout 2 - stderr.
,
( "", " " - /dev/null).

cat f1 f2
"f1" "f2", , ,

111111 222222
cat: f2: No such file or directory
111111 222222 - "f1", "f2" , "cat"
, , ,
.
, :
cat f1 f2 2>f-err
( "2>") "f-err".
, "ff",

cat f1 f2 >>ff 2>ff


,
.
cat f1 f2 2>>ff 1>&2
"stderr" ( ) "ff",
"stderr", "ff".
.
"1>&2" - , , ,
"&"; .
<- .
>- .

91
9.1.3 .
:
* ( ) ;
? ;
[...] , / ;
cat f* , "f";
cat *f* , "f";
cat program.? , "program.c"
"program.o", "program.com";
cat [a-d]* , "a", "b", "c", "d".
"cat [abcd]*" "cat [bdac]*".
9.1.4
, ,
.
"cmd", :
date; pwd; ls
shell (!), "sh", "cmd",
, ..
$ sh cmd

$ sh <cmd

, ,
.
- ,
.. , .
. ,
chmod 711 cmd
"rwx__x__x".
cmd
.
,
date; pwd; ls
:
date
pwd
ls
.
, ,
, , shell.
shell-

92
, shell-, UNIX,
.
9.2 shell
shell ,
; .
= , , PS2 = '<' .
$. :
$n, n - ;
, , ,
;
# ? - ! $
, ,
, shell.
. Shell :
= $z z=1000
echo $x
1000
x z, .
. $n. n - ,
, . ,

cat text_1 text_2...text_9
, $1...$9.
$0. , $0 - cat, $1 - text_1, $2 - text_2 ..
set, :
set arg_1 arg_2... arg_9
$1 arg_1, $2 - arg_2 ..
echo,
echo $l $2 $9
arg_1 arg_2 arg_9
( ) *.
:
echo $*
arg_2 arg_3... arg_10 arg_11 arg_12
shell $0
( $1 $2). shell
; .
. - ? # $ ! shell.
echo :
- - ( set);
# - , ;
? - ;
$ - PID;
! - PID .

93
9.3 ,
expr (express - ) expression
. ; ,
, . , ,
. expr, , ,
, , ..
.
, :
b=196
a=' expr 1781 -$b'
*, /, %:
d=' expr $a +125 '*' 209'
c=' expr $d % 13'
(
\) , . ''
d 13 (d % 13).
:
concur=' er 'abcdefgh' : 'abcde' '
echo $concur
5.
(:). - concur.
.
, . chain:
chain ='The program is written in Assembler'.
Set=' expr $chain: '*''
Echo $Set
41. Set.
9.4

.
:
cd [dir] - ;
exec [cmd [arg...]] < > - , cmd arg,
.
umask [ - | -s] [nnn] - , (
, nnn: 3 ,
). nnn , .
- , -s - ;
:
eval [- arg] - ;
exit [n] - , , ,
;
trap [cmd] [cond] - ,
cmd - ;
cond = 0 EXIT - cmd ;
cond = ERR - cmd :
cond - , cmd
;
export (name [=word]...] - . export , name
:
wait [n] - . ,
. n. wait

94
n:
read name... -
, name;
. shell- data, :
echo -n 'Please write down your name:"
read name
, ,
Please wit down your name:_
( - ).
<> .
9.5.
true false.
: true - , 0, false -
, ,
.
while until if. UNIX
.
9.6 if
if . true. shell if
, false, . if:
if<>
then
<list1>
else
<list2>
fi
9.7 test
test () if .
test. Test , ,
. , , -
. :
test [ -rwfds] file

r - ( 0);
w - ;
f - ;
d - ;
s - .
test , , ,
. :

95
q
t
est
A

e
e
e
t

= ?

-n

-g

<>
?
>=
?
<=
?
> ?

-lt

A < B?

-g
-l

test, :
! - , , if test true
if test ! false:
- "" (or) true,
true:
- "" (and) true, true.
9.8
9.8.1
while () , , while
true false.
while true:
while <list1>
do
<list2>
done
<list1> <list2> - , while ,
while. 0, , do done.
while false :
until <list1>
do
<list2>
done
, do done
.
( ).
break . break
2 (break 2), .
continue while.
9.8.2
:
for name in [wordlist]
do
<list>
done
name - ; wordlist - ; <list> - .
name wordlist,
, do done. name
wordlist list. ,
wordlist.

96
9.9 case
case
. :
case <string> in
s1) <list1>;;
s2) <list2>;;
...
sn) <listn>;;
esac
<list1>, <list2>.. .listn - . <string>
sl,s2...sk...sn. ,
sk ;;.
.
echo -n 'Please, write down your age'
read age
case $age in
test $age -le 20) echo 'you are so young' ;;
test $age -le 40) echo 'you are still young' ;;
test $age -le 70) echo 'you are too young' ;;
*)echo 'Please write down once more'
esac
* .
9.10 shell-
,
, shell :
. , ,
.
. , "$",
.
. .
. ("*", "?","[]")
.
9.11
, . Shell .
"set" ,
, (
" "), , .
,
UNIX .
, "set":
HOME=/home/sae
PATH=/usr/local/bin:/usr/bin:/bin:.:/usr/bin/X11:
IFS=
LOGNAME=sae

97
MAIL=/var/spool/mail/sae
PWD=/home/sae/STUDY/SHELL
PS1=${PWD}:" "
PS2=>
SHELL=/bin/bash
TERM=linux
TERMCAP=console|con80x25|dumb|linux:li#25:co#80::
UID=501
perem=stroka
x=5
.
HOME=/home/sae , ( )
. , ,
"/home/sae".
PATH=/bin:/usr/bin:.:/usr/local/bin:/usr/bin/X11

(), "shell" .
. .
.
, "echo", "cd", "pwd", "date".
"/bin", "sh", "cp", "mv", "ls"
.. "/usr/bin" "cat", "", "expr", "nroff", "man" .
(".", "", ..""),
().
<Enter> "shell" (
) , ,
. , .
, ,
, .
, , , , .
(, "" ).
IFS= ( ) ,
(). "", "" " ",
.
LOGNAME=sae ("" ).
MAIL=/var/spool/mail/sae , () .
PWD=/home/sae/STUDY/SHELL
PS1=${PWD}:" " .
. "/home/sae/STUDY/SHELL: ".
PS2=> ( ">") (
) . , "(" <Enter>
. , ,
")" .
SHELL=/bin/bash , .
() shell ("sh"), "" .
TERM=linux . -"bash", (Bourne-Again SHell)
TERMCAP=console|con80x25|dumb|linux:li#25:co#80::
(TERMinal CAPacity) ( ) .
UID=501 ( "501").
perem=stroka
x=5

98
, .

"/etc/rc" "/etc/.profile".
. (, ,
, , ..) ,
".profile" (${HOME}/.profile),
. , ).
.
( ).
".profile",
. ,
( ) "." , ..
. .profile
, , , . ,
"ls" - "-a". ,
"rm *".
"my" , ".profile",
PATH=${PATH}:/home/sae/my

PATH=${PATH}:${HOME}/my
, . ,
export TERM PATH REDKEYS MAIL
".profile" ,
stty -lcase
" ";
cat
, ""
. shell
():
? , ;
$ ;
! ;
# , shell;
* , ;
@ , ;
- , shell.
(. - shell-)
"$".
.

99
specific par1 par2 par3

###
# specific: ()
echo $0 -
echo $? -
echo $$ -
echo $! -
echo
echo $* - ,
echo $@ - ,
echo
set -au
echo $- -

specific -
0 -
499 -
98 -
par1 par2 par3 - ,
par1 par2 par3 - ,
au -
"0" .
"$$",
, . ,
, ,
. , , ,
( ).
. ,
. , " ", ..,
, $$, .
.
"echo" .
$* $@ ,
"par1 par2 par3"

"par1" "par2" "par3"
, "$*" "$@" "for".
"set" ("a" -
; "u" - ),
"$-".

100

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

/ .. , .. . .:, 2001. 544


. UNIX / . . CG,.:BHV, 1999. 451
.http://www.citforum.ru/operating_systems/
. : 2- . .1. - . : ,1987 - 359 .
. : 2- . .2. - . : ,1987 398 .
. - . : , 1980 - 436
., . . - .: , 1984.-392 .

.., .. Delphi4. .: - _, 1999. 816 .
., . . .: , 1978. 648 .
. Windows 95 . : , 1995. 512 .
.. UNIX: . .- : BHV-,
2000.- 514.
.. . - : -, 2002.- 624.
., - . UNIX.- : , 1999.- 374.
- . UNIX: . .- .: , 1997.- 341.
. Unix. http://lib.ru/LINUXGUIDE/