home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / unix / p4 < prev    next >
Encoding:
Text File  |  1975-06-26  |  13.7 KB  |  403 lines

  1. .s1
  2. 6. The Shell
  3. .es
  4. For most users,
  5. communication with \*sUNIX\*n is carried on with the
  6. aid of a program called the Shell.
  7. The Shell is a command
  8. line interpreter: it reads lines typed by the user and
  9. interprets them as requests to execute
  10. other programs.
  11. In simplest form, a command line consists of the command
  12. name followed by arguments to the command, all separated
  13. by spaces:
  14. .dc
  15. command arg\*t\d1\u\*n arg\*t\d2\u\*n .\|.\|. arg\*t\dn\u\*n
  16. .ec
  17. The Shell splits up the command name and the arguments into
  18. separate strings.
  19. Then a file with name \fIcommand\fR is sought;
  20. \fIcommand\fR may be a path name including the ``/'' character to
  21. specify any file in the system.
  22. If \fIcommand\fR is found, it is brought into
  23. core and executed.
  24. The arguments
  25. collected by the Shell are accessible
  26. to the command.
  27. When the command is finished, the Shell
  28. resumes its own execution, and indicates its readiness
  29. to accept another command by typing a prompt character.
  30. .pg
  31. If file \fIcommand\fR cannot be found,
  32. the Shell prefixes the string  \fI/\|bin/\fR  to \fIcommand\fR and
  33. attempts again to find the file.
  34. Directory \fI/\|bin\fR contains all the commands
  35. intended to be generally used.
  36. .s2
  37. 6.1 Standard I/O
  38. .es
  39. The discussion of I/O in \(sc3 above seems to imply that
  40. every file used by a program must be opened or created by the program in
  41. order to get a file descriptor for the file.
  42. Programs executed by the Shell, however, start off with
  43. two open files which have file descriptors
  44. 0 and 1.
  45. As such a program begins execution, file 1 is open for writing,
  46. and is best understood as the standard output file.
  47. Except under circumstances indicated below, this file
  48. is the user's typewriter.
  49. Thus programs which wish to write informative or diagnostic
  50. information ordinarily use file descriptor 1.
  51. Conversely, file 0 starts off open for reading, and programs which
  52. wish to read messages typed by the user usually
  53. read this file.
  54. .pg
  55. The Shell is able to change the standard assignments of
  56. these file descriptors from the
  57. user's typewriter printer and keyboard.
  58. If one of the
  59. arguments to a command is prefixed by ``>'', file descriptor
  60. 1 will, for the duration of the command, refer to the
  61. file named after the ``>''.
  62. For example,
  63. .dc
  64. ls
  65. .ec
  66. ordinarily lists, on the typewriter, the names of the files in the current
  67. directory.
  68. The command
  69. .dc
  70. ls >there
  71. .ec
  72. creates a file called \fIthere\fR and places the listing there.
  73. Thus the argument ``>there'' means, ``place output on \fIthere\fR.''
  74. On the other hand,
  75. .dc
  76. ed
  77. .ec
  78. ordinarily enters the editor, which takes requests from the
  79. user via his typewriter.
  80. The command
  81. .dc
  82. ed <script
  83. .ec
  84. interprets \fIscript\fR as a file of editor commands;
  85. thus ``<script'' means, ``take input from \fIscript\fR.''
  86. .pg
  87. Although the file name following ``<'' or ``>'' appears
  88. to be an argument to the command, in fact it is interpreted
  89. completely by the Shell and is not passed to the
  90. command at all.
  91. Thus no special coding to handle I/O redirection is needed within each
  92. command; the command need merely use the standard file
  93. descriptors 0 and 1 where appropriate.
  94. .s2
  95. 6.2 Filters
  96. .es
  97. An extension of the standard I/O notion is used
  98. to direct output from one command to
  99. the input of another.
  100. A sequence of commands separated by
  101. vertical bars causes the Shell to
  102. execute all the commands simultaneously and to arrange
  103. that the standard output of each command
  104. be delivered to the standard input of
  105. the next command in the sequence.
  106. Thus in the command line
  107. .dc
  108. ls | pr \(mi2 | opr
  109. .ec
  110. .it ls
  111. lists the names of the files in the current directory;
  112. its output is passed to \fIpr\fR,
  113. which
  114. paginates its input with dated headings.
  115. The argument ``\(mi2'' means
  116. double column.
  117. Likewise the output from \fIpr\fR is input to \fIopr\fR.
  118. This command spools its input onto a file for off-line
  119. printing.
  120. .pg
  121. This procedure could have been carried out
  122. more clumsily by
  123. .dc
  124. ls >temp1
  125. .ti 1i
  126. pr \(mi2 <temp1 >temp2
  127. .ti 1i
  128. opr <temp2
  129. .ec
  130. followed by removal of the temporary files.
  131. In the absence of the ability
  132. to redirect output and input,
  133. a still clumsier method would have been to
  134. require the
  135. .it ls
  136. command
  137. to accept user requests to paginate its output,
  138. to print in multi-column format, and to arrange
  139. that its output be delivered off-line.
  140. Actually it would be surprising, and in fact
  141. unwise for efficiency reasons,
  142. to expect authors of
  143. commands such as
  144. .it ls
  145. to provide such a wide variety of output options.
  146. .pg
  147. A program
  148. such as \fIpr\fR
  149. which copies its standard input to its standard output
  150. (with processing)
  151. is called a \fIfilter\fR.
  152. Some filters which we have found useful
  153. perform
  154. character transliteration,
  155. sorting of the input,
  156. and encryption and decryption.
  157. .s2
  158. 6.3 Command Separators; Multitasking
  159. .es
  160. Another feature provided by the Shell is relatively straightforward.
  161. Commands need not be on different lines; instead they may be separated
  162. by semicolons.
  163. .dc
  164. ls; ed
  165. .ec
  166. will first list the contents of the current directory, then enter
  167. the editor.
  168. .pg
  169. A related feature is more interesting.
  170. If a command is followed
  171. by ``&'', the Shell will not wait for the command to finish before
  172. prompting again; instead, it is ready immediately
  173. to accept a new command.
  174. For example,
  175. .dc
  176. as source >output &
  177. .ec
  178. causes \fIsource\fR to be assembled, with diagnostic
  179. output going to \fIoutput;\fR no matter how long the
  180. assembly takes, the Shell returns immediately.
  181. When the Shell does not wait for
  182. the completion of a command,
  183. the identification of the
  184. process running that command is printed.
  185. This identification may be used to
  186. wait for the completion of the command or to
  187. terminate it.
  188. The ``&'' may be used
  189. several times in a line:
  190. .dc
  191. as source >output & ls >files &
  192. .ec
  193. does both the assembly and the listing in the background.
  194. In the examples above using ``&'', an output file
  195. other than the typewriter was provided; if this had not been
  196. done, the outputs of the various commands would have been
  197. intermingled.
  198. .pg
  199. The Shell also allows parentheses in the above operations.
  200. For example
  201. .dc
  202. (\|date; ls\|) >x &
  203. .ec
  204. prints the current date and time followed by
  205. a list of the current directory onto the file \fIx.\fR
  206. The Shell also returns immediately for another request.
  207. .s2
  208. 6.4 The Shell as a Command; Command Files
  209. .es
  210. The Shell is itself a command, and may be called recursively.
  211. Suppose file \fItryout\fR contains the lines
  212. .dc
  213. as source
  214. .ti 1i
  215. mv a.out testprog
  216. .ti 1i
  217. testprog
  218. .ec
  219. The \fImv\fR command causes the file \fIa.out\fR to be renamed \fItestprog.\fR
  220. \fIA.out\fR is the (binary) output of the assembler, ready to be executed.
  221. Thus if the three lines above were typed on the console,
  222. \fIsource\fR would be assembled, the resulting program renamed \fItestprog\fR,
  223. and \fItestprog\fR executed.
  224. When the lines are in \fItryout\fR, the command
  225. .dc
  226. sh <tryout
  227. .ec
  228. would cause the Shell \fIsh\fR to execute the commands
  229. sequentially.
  230. .pg
  231. The Shell has further capabilities, including the
  232. ability to substitute parameters
  233. and
  234. to construct argument lists from a specified
  235. subset of the file names in a directory.
  236. It is also possible to
  237. execute commands conditionally on character string comparisons
  238. or on existence of given files
  239. and to perform transfers of control
  240. within filed command sequences.
  241. .s2
  242. 6.5 Implementation of the Shell
  243. .es
  244. The outline of the operation of the Shell can now be understood.
  245. Most of the time, the Shell
  246. is waiting for the user to type a command.
  247. When the
  248. new-line character ending the line
  249. is typed, the Shell's \fIread\fR call returns.
  250. The Shell analyzes the command line, putting the
  251. arguments in a form appropriate for \fIexecute\fR.
  252. Then \fIfork\fR is called.
  253. The child process, whose code
  254. of course is still that of the Shell, attempts
  255. to perform an \fIexecute\fR with the appropriate arguments.
  256. If successful, this will bring in and start execution of the program whose name
  257. was given.
  258. Meanwhile, the other process resulting from the \fIfork\fR, which is the
  259. parent process, \fIwait\|\fRs for the child process to die.
  260. When this happens, the Shell knows the command is finished, so
  261. it types its prompt and reads the typewriter to obtain another
  262. command.
  263. .pg
  264. Given this framework, the implementation of background processes
  265. is trivial; whenever a command line contains ``&'',
  266. the Shell merely refrains from waiting for the process
  267. which it created
  268. to execute the command.
  269. .pg
  270. Happily, all of this mechanism meshes very nicely with
  271. the notion of standard input and output files.
  272. When a process is created by the \fIfork\fR primitive, it
  273. inherits not only the core image of its parent
  274. but also all the files currently open in its parent,
  275. including those with file descriptors 0 and 1.
  276. The Shell, of course, uses these files to read command
  277. lines and to write its prompts and diagnostics, and in the ordinary case
  278. its children_the command programs_inherit them automatically.
  279. When an argument with ``<'' or ``>'' is given however, the
  280. offspring process, just before it performs \fIexecute,\fR
  281. makes the standard I/O
  282. file descriptor 0 or 1 respectively refer to the named file.
  283. This is easy
  284. because, by agreement,
  285. the smallest unused file descriptor is assigned
  286. when a new file is \fIopen\fR\|ed
  287. (or \fIcreate\fR\|d);
  288. it is only necessary to close file 0 (or 1)
  289. and open the named file.
  290. Because the process in which the command program runs simply terminates
  291. when it is through, the association between a file
  292. specified after ``<'' or ``>'' and file descriptor 0 or 1 is ended
  293. automatically when the process dies.
  294. Therefore
  295. the Shell need not know the actual names of the files
  296. which are its own standard input and output, since it need
  297. never reopen them.
  298. .pg
  299. Filters are straightforward extensions
  300. of standard I/O redirection with pipes used
  301. instead of files.
  302. .pg
  303. In ordinary circumstances, the main loop of the Shell never
  304. terminates.
  305. (The main loop includes that
  306. branch of the return from \fIfork\fR belonging to the
  307. parent process; that is, the branch which does a \fIwait\fR, then
  308. reads another command line.)
  309. The one thing which causes the Shell to terminate is
  310. discovering an end-of-file condition on its input file.
  311. Thus, when the Shell is executed as a command with
  312. a given input file, as in
  313. .dc
  314. sh <comfile
  315. .ec
  316. the commands in \fIcomfile\fR will be executed until
  317. the end of \fIcomfile\fR is reached; then the instance of the Shell
  318. invoked by \fIsh\fR will terminate.
  319. Since this Shell process
  320. is the child of another instance of the Shell, the \fIwait\fR
  321. executed in the latter will return, and another
  322. command may be processed.
  323. .s2
  324. 6.6 Initialization
  325. .es
  326. The instances of the Shell to which users type
  327. commands are themselves children of another process.
  328. The last step in the initialization of \*sUNIX\*n is the creation of
  329. a single process and the invocation (via \fIexecute\fR)
  330. of a program called \fIinit\fR.
  331. The role of \fIinit\fR is to create one process
  332. for each typewriter channel which may be dialed up by a user.
  333. The various subinstances of \fIinit\fR open the appropriate typewriters
  334. for input and output.
  335. Since when \fIinit\fR was invoked there were
  336. no files open, in each process the typewriter keyboard will
  337. receive file descriptor 0 and the printer file descriptor 1.
  338. Each process types out a message requesting that the user log in
  339. and waits, reading the typewriter, for a reply.
  340. At the outset, no one is logged in,
  341. so each process simply hangs.
  342. Finally someone types his name or other identification.
  343. The appropriate instance of \fIinit\fR wakes up, receives the log-in
  344. line, and reads a password file.
  345. If the user name is found, and if
  346. he is able to supply the correct password, \fIinit\fR
  347. changes to the user's default current directory, sets
  348. the process's user \*sID\*n to that of the person logging in, and performs
  349. an \fIexecute\fR of the Shell.
  350. At this point the Shell is ready to receive commands
  351. and the logging-in protocol is complete.
  352. .pg
  353. Meanwhile, the mainstream path of \fIinit\fR (the parent of all
  354. the subinstances of itself which will later become Shells)
  355. does a \fIwait\fR.
  356. If one of the child processes terminates, either
  357. because a Shell found an end of file or because a user
  358. typed an incorrect name or password, this path of \fIinit\fR
  359. simply recreates the defunct process, which in turn reopens the appropriate
  360. input and output files and types another login message.
  361. Thus a user may log out simply by typing the end-of-file
  362. sequence in place of a command to the Shell.
  363. .s2
  364. 6.7 Other programs as Shell
  365. .es
  366. The Shell as described above is designed to allow users
  367. full access to the facilities of the system, since it will
  368. invoke the execution of any program
  369. with appropriate protection mode.
  370. Sometimes, however, a different interface to the system
  371. is desirable, and this feature is easily arranged.
  372. .pg
  373. Recall that after a user has successfully logged in by supplying
  374. his name and password, \fIinit\fR ordinarily invokes the Shell
  375. to interpret command lines.
  376. The user's entry
  377. in the password file may contain the name
  378. of a program to be invoked after login instead of the Shell.
  379. This program is free to interpret the user's messages
  380. in any way it wishes.
  381. .pg
  382. For example, the password file entries
  383. for users of a secretarial editing system
  384. specify that the
  385. editor \fIed\fR is to be used instead of the Shell.
  386. Thus when editing system users log in, they are inside the editor and
  387. can begin work immediately; also, they can be prevented from
  388. invoking \*sUNIX\*n programs not intended for their use.
  389. In practice, it has proved desirable to allow a temporary
  390. escape from the editor
  391. to execute the formatting program and other utilities.
  392. .pg
  393. Several of the games (e.g., chess, blackjack, 3D tic-tac-toe)
  394. available on \*sUNIX\*n illustrate
  395. a much more severely restricted environment.
  396. For each of these an entry exists
  397. in the password file specifying that the appropriate game-playing
  398. program is to be invoked instead of the Shell.
  399. People who log in as a player
  400. of one of the games find themselves limited to the
  401. game and unable to investigate the presumably more interesting
  402. offerings of \*sUNIX\*n as a whole.
  403.