home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / shell / t2 < prev    next >
Encoding:
Text File  |  1979-01-10  |  18.0 KB  |  830 lines

  1. .bp
  2. .SH
  3. 2.0\ Shell\ procedures
  4. .LP
  5. The shell may be used to read and execute commands
  6. contained in a file.
  7. For example,
  8. .DS
  9.     sh file [ args \*(ZZ ]
  10. .DE
  11. calls the shell to read commands from \fIfile.\fP
  12. Such a file is called a \fIcommand procedure\fP
  13. or \fIshell procedure.\fP
  14. Arguments may be supplied with the call
  15. and are referred to in \fIfile\fP
  16. using the positional parameters
  17. \fB$1, $2, \*(ZZ\|.\fR
  18. For example, if the file \fIwg\fP contains
  19. .DS
  20.     who \*(VT grep $1
  21. .DE
  22. then
  23. .DS
  24.     sh wg fred
  25. .DE
  26. is equivalent to
  27. .DS
  28.     who \*(VT grep fred
  29. .DE
  30. .LP
  31. UNIX files have three independent attributes,
  32. \fIread,\fP \fIwrite\fP and \fIexecute.\fP
  33. The UNIX command \fIchmod\fP (1) may be used
  34. to make a file executable.
  35. For example,
  36. .DS
  37.     chmod +x wg
  38. .DE
  39. will ensure that the file \fIwg\fP has execute status.
  40. Following this, the command
  41. .DS
  42.     wg fred
  43. .DE
  44. is equivalent to
  45. .DS
  46.     sh wg fred
  47. .DE
  48. This allows shell procedures and programs
  49. to be used interchangeably.
  50. In either case a new process is created to
  51. run the command.
  52. .LP
  53. As well as providing names for the positional
  54. parameters,
  55. the number of positional parameters in the call
  56. is available as \fB$#\|.\fP
  57. The name of the file being executed
  58. is available as \fB$0\|.\fP
  59. .LP
  60. A special shell parameter \fB$\*(ST\fP
  61. is used to substitute for all positional parameters
  62. except \fB$0\|.\fP
  63. A typical use of this is to provide
  64. some default arguments,
  65. as in,
  66. .DS
  67.     nroff \(miT450 \(mims $\*(ST
  68. .DE
  69. which simply prepends some arguments
  70. to those already given.
  71. .SH
  72. 2.1\ Control\ flow\ -\ for
  73. .LP
  74. A frequent use of shell procedures is to loop
  75. through the arguments (\fB$1, $2, \*(ZZ\fR)
  76. executing commands once for each argument.
  77. An example of such a procedure is
  78. \fItel\fP that searches the file
  79. \fB/usr/lib/telnos\fR
  80. that contains lines of the form
  81. .DS
  82.     \*(ZZ
  83.     fred mh0123
  84.     bert mh0789
  85.     \*(ZZ
  86. .DE
  87. The text of \fItel\fP is
  88. .DS
  89.     for i
  90.     do grep $i /usr/lib/telnos; done
  91. .DE
  92. The command
  93. .DS
  94.     tel fred
  95. .DE
  96. prints those lines in \fB/usr/lib/telnos\fR
  97. that contain the string \fIfred\|.\fP
  98. .DS
  99.     tel fred bert
  100. .DE
  101. prints those lines containing \fIfred\fP
  102. followed by those for \fIbert.\fP
  103. .LP
  104. The \fBfor\fP loop notation is recognized by the shell
  105. and has the general form
  106. .DS
  107.     \fBfor\fR \fIname\fR \fBin\fR \fIw1 w2 \*(ZZ\fR
  108.     \fBdo\fR \fIcommand-list\fR
  109.     \fBdone\fR
  110. .DE
  111. A \fIcommand-list\fP is a sequence of one or more
  112. simple commands separated or terminated by a newline or semicolon.
  113. Furthermore, reserved words
  114. like \fBdo\fP and \fBdone\fP are only
  115. recognized following a newline or
  116. semicolon.
  117. \fIname\fP is a shell variable that is set
  118. to the words \fIw1 w2 \*(ZZ\fR in turn each time the \fIcommand-list\fP
  119. following \fBdo\fP
  120. is executed.
  121. If \fBin\fR \fIw1 w2 \*(ZZ\fR
  122. is omitted then the loop
  123. is executed once for each positional parameter;
  124. that is, \fBin\fR \fI$\*(ST\fR is assumed.
  125. .LP
  126. Another example of the use of the \fBfor\fP
  127. loop is the \fIcreate\fP command
  128. whose text is
  129. .DS
  130.     for i do >$i; done
  131. .DE
  132. The command
  133. .DS
  134.     create alpha beta
  135. .DE
  136. ensures that two empty files
  137. \fIalpha\fP and \fIbeta\fP exist
  138. and are empty.
  139. The notation \fI>file\fP may be used on its
  140. own to create or clear the contents of a file.
  141. Notice also that a semicolon (or newline) is required before \fBdone.\fP
  142. .SH
  143. 2.2\ Control\ flow\ -\ case
  144. .LP
  145. A multiple way branch is provided for by the
  146. \fBcase\fP notation.
  147. For example,
  148. .DS
  149.     case $# in
  150.     \*(Ca1)    cat \*(AP$1 ;;
  151.     \*(Ca2)    cat \*(AP$2 <$1 ;;
  152.     \*(Ca\*(ST)    echo \\'usage: append [ from ] to\\' ;;
  153.     esac
  154. .DE
  155. is an \fIappend\fP command.
  156. When called
  157. with one argument as
  158. .DS
  159.     append file
  160. .DE
  161. \fB$#\fP is the string \fI1\fP and
  162. the standard input is copied onto the
  163. end of \fIfile\fP
  164. using the \fIcat\fP command.
  165. .DS
  166.     append file1 file2
  167. .DE
  168. appends the contents of \fIfile1\fP
  169. onto \fIfile2.\fP
  170. If the number of arguments supplied to
  171. \fIappend\fP is other than 1 or 2
  172. then a message is printed indicating
  173. proper usage.
  174. .LP
  175. The general form of the \fBcase\fP command
  176. is
  177. .DS
  178.     \fBcase \fIword \fBin
  179.     \*(Ca\fIpattern\|\fB)\ \fIcommand-list\fB\|;;
  180.     \*(Ca\*(ZZ
  181.     \fBesac\fR
  182. .DE
  183. The shell attempts to match
  184. \fIword\fR with each \fIpattern,\fR
  185. in the order in which the patterns
  186. appear.
  187. If a match is found the
  188. associated \fIcommand-list\fP is
  189. executed and execution
  190. of the \fBcase\fP is complete.
  191. Since \*(ST is the pattern that matches any
  192. string it can be used for the default case.
  193. .LP
  194. A word of caution:
  195. no check is made to ensure that only
  196. one pattern matches
  197. the case argument.
  198. The first match found defines the set of commands
  199. to be executed.
  200. In the example below the commands following
  201. the second \*(ST will never be executed.
  202. .DS
  203.     case $# in
  204.     \*(Ca\*(ST) \*(ZZ ;;
  205.     \*(Ca\*(ST) \*(ZZ ;;
  206.     esac
  207. .DE
  208. .LP
  209. Another example of the use of the \fBcase\fP
  210. construction is to distinguish
  211. between different forms
  212. of an argument.
  213. The following example is a fragment of a \fIcc\fP command.
  214. .DS
  215.     for i
  216.     do case $i in
  217.     \*(DC\(mi[ocs])    \*(ZZ ;;
  218.     \*(DC\(mi\*(ST)    echo \\'unknown flag $i\\' ;;
  219.     \*(DC\*(ST.c)    /lib/c0 $i \*(ZZ ;;
  220.     \*(DC\*(ST)    echo \\'unexpected argument $i\\' ;;
  221.     \*(DOesac
  222.     done
  223. .DE
  224. .LP
  225. To allow the same commands to be associated
  226. with more than one pattern
  227. the \fBcase\fP command provides
  228. for alternative patterns
  229. separated by a \*(VT\|.
  230. For example,
  231. .DS
  232.     case $i in
  233.     \*(Ca\(mix\*(VT\(miy)    \*(ZZ
  234.     esac
  235. .DE
  236. is equivalent to
  237. .DS
  238.     case $i in
  239.     \*(Ca\(mi[xy])    \*(ZZ
  240.     esac
  241. .DE
  242. .LP
  243. The usual quoting conventions apply
  244. so that
  245. .DS
  246.     case $i in
  247.     \*(Ca\\\\?)    \*(ZZ
  248. .DE
  249. will match the character \fB?\|.\fP
  250. .SH
  251. 2.3\ Here\ documents
  252. .LP
  253. The shell procedure \fItel\fP
  254. in section 2.1 uses the file \fB/usr/lib/telnos\fR
  255. to supply the data
  256. for \fIgrep.\fP
  257. An alternative is to include this
  258. data
  259. within the shell procedure as a \fIhere\fP document, as in,
  260. .DS
  261.     for i
  262.     do grep $i \*(HE!
  263.     \*(DO\*(ZZ
  264.     \*(DOfred mh0123
  265.     \*(DObert mh0789
  266.     \*(DO\*(ZZ
  267.     !
  268.     done
  269. .DE
  270. In this example
  271. the shell takes the lines between \fB\*(HE!\fR and \fB!\fR
  272. as the standard input for \fIgrep.\fP
  273. The string \fB!\fR is arbitrary, the document
  274. being terminated by a line that consists
  275. of the string following \*(HE\|.
  276. .LP
  277. Parameters are substituted in the document
  278. before it is made available to \fIgrep\fP
  279. as illustrated by the following procedure
  280. called \fIedg\|.\fP
  281. .DS
  282.     ed $3 \*(HE%
  283.     g/$1/s//$2/g
  284.     w
  285.     %
  286. .DE
  287. The call
  288. .DS
  289.     edg string1 string2 file
  290. .DE
  291. is then equivalent to the command
  292. .DS
  293.     ed file \*(HE%
  294.     g/string1/s//string2/g
  295.     w
  296.     %
  297. .DE
  298. and changes all occurrences of \fIstring1\fP
  299. in \fIfile\fP to \fIstring2\|.\fP
  300. Substitution can be prevented using \\
  301. to quote the special character \fB$\fP
  302. as in
  303. .DS
  304.     ed $3 \*(HE+
  305.     1,\\\\$s/$1/$2/g
  306.     w
  307.     +
  308. .DE
  309. (This version of \fIedg\fP is equivalent to
  310. the first except that \fIed\fP will print
  311. a \fB?\fR if there are no occurrences of
  312. the string \fB$1\|.\fP)
  313. Substitution within a \fIhere\fP document
  314. may be prevented entirely by quoting
  315. the terminating string,
  316. for example,
  317. .DS
  318.     grep $i \*(HE\\\\#
  319.     \*(ZZ
  320.     #
  321. .DE
  322. The document is presented
  323. without modification to \fIgrep.\fP
  324. If parameter substitution is not required
  325. in a \fIhere\fP document this latter form
  326. is more efficient.
  327. .SH
  328. 2.4\ Shell\ variables
  329. .LP
  330. The shell
  331. provides string-valued variables.
  332. Variable names begin with a letter
  333. and consist of letters, digits and
  334. underscores.
  335. Variables may be given values by writing, for example,
  336. .DS
  337.     user=fred\ box=m000\ acct=mh0000
  338. .DE
  339. which assigns values to the variables
  340. \fBuser, box\fP and \fBacct.\fP
  341. A variable may be set to the null string
  342. by saying, for example,
  343. .DS
  344.     null=
  345. .DE
  346. The value of a variable is substituted
  347. by preceding its name with \fB$\|;\fP
  348. for example,
  349. .DS
  350.     echo $user
  351. .DE
  352. will echo \fIfred.\fP
  353. .LP
  354. Variables may be used interactively
  355. to provide abbreviations for frequently
  356. used strings.
  357. For example,
  358. .DS
  359.     b=/usr/fred/bin
  360.     mv pgm $b
  361. .DE
  362. will move the file \fIpgm\fP
  363. from the current directory to the directory \fB/usr/fred/bin\|.\fR
  364. A more general notation is available for parameter
  365. (or variable)
  366. substitution, as in,
  367. .DS
  368.     echo ${user}
  369. .DE
  370. which is equivalent to
  371. .DS
  372.     echo $user
  373. .DE
  374. and is used when the parameter name is
  375. followed by a letter or digit.
  376. For example,
  377. .DS
  378.     tmp=/tmp/ps
  379.     ps a >${tmp}a
  380. .DE
  381. will direct the output of \fIps\fR
  382. to the file \fB/tmp/psa,\fR
  383. whereas,
  384. .DS
  385.     ps a >$tmpa
  386. .DE
  387. would cause the value of the variable \fBtmpa\fP
  388. to be substituted.
  389. .LP
  390. Except for \fB$?\fP the following
  391. are set initially by the shell.
  392. \fB$?\fP is set after executing each command.
  393. .RS
  394. .IP \fB$?\fP 8
  395. The exit status (return code)
  396. of the last command executed
  397. as a decimal string.
  398. Most commands return a zero exit status
  399. if they complete successfully,
  400. otherwise a non-zero exit status is returned.
  401. Testing the value of return codes is dealt with
  402. later under \fBif\fP and \fBwhile\fP commands.
  403. .IP \fB$#\fP 8
  404. The number of positional parameters
  405. (in decimal).
  406. Used, for example, in the \fIappend\fP command
  407. to check the number of parameters.
  408. .IP \fB$$\fP 8
  409. The process number of this shell (in decimal).
  410. Since process numbers are unique among
  411. all existing processes, this string is
  412. frequently used to generate
  413. unique
  414. temporary file names.
  415. For example,
  416. .DS
  417.     ps a >/tmp/ps$$
  418.     \*(ZZ
  419.     rm /tmp/ps$$
  420. .DE
  421. .IP \fB$\|!\fP 8
  422. The process number of the last process
  423. run in the background (in decimal).
  424. .IP \fB$\(mi\fP 8
  425. The current shell flags, such as
  426. \fB\(mix\fR and \fB\(miv\|.\fR
  427. .RE
  428. .LP
  429. Some variables have a special meaning to the
  430. shell and should be avoided for general
  431. use.
  432. .RS
  433. .IP \fB$\s-1MAIL\s0\fP 8
  434. When used interactively
  435. the shell looks at the file
  436. specified by this variable
  437. before it issues a prompt.
  438. If the specified file has been modified
  439. since it
  440. was last looked at the shell
  441. prints the message
  442. \fIyou have mail\fP before prompting
  443. for the next command.
  444. This variable is typically set
  445. in the file \fB.profile,\fP
  446. in the user's login directory.
  447. For example,
  448. .DS
  449.     \s-1MAIL\s0=/usr/mail/fred
  450. .DE
  451. .IP \fB$\s-1HOME\s0\fP 8
  452. The default argument
  453. for the \fIcd\fP command.
  454. The current directory is used to resolve
  455. file name references that do not begin with
  456. a \fB/\|,\fR
  457. and is changed using the \fIcd\fP command.
  458. For example,
  459. .DS
  460.     cd /usr/fred/bin
  461. .DE
  462. makes the current directory \fB/usr/fred/bin\|.\fR
  463. .DS
  464.     cat wn
  465. .DE
  466. will print on the terminal the file \fIwn\fP
  467. in this directory.
  468. The command
  469. \fIcd\fP with no argument
  470. is equivalent to
  471. .DS
  472.     cd $\s-1HOME\s0
  473. .DE
  474. This variable is also typically set in the
  475. the user's login profile.
  476. .IP \fB$\s-1PATH\s0\fP 8
  477. A list of directories that contain commands (the \fIsearch path\fR\|).
  478. Each time a command is executed by the shell
  479. a list of directories is searched
  480. for an executable file.
  481. .ne 5
  482. If \fB$\s-1PATH\s0\fP is not set
  483. then the current directory,
  484. \fB/bin\fP, and \fB/usr/bin\fP are searched by default.
  485. .ne 5
  486. Otherwise \fB$\s-1PATH\s0\fP consists of directory
  487. names separated by \fB:\|.\fP
  488. For example,
  489. .DS
  490.     \s-1PATH\s0=\fB:\fP/usr/fred/bin\fB:\fP/bin\fB:\fP/usr/bin
  491. .DE
  492. specifies that the current directory
  493. (the null string before the first \fB:\fP\|),
  494. \fB/usr/fred/bin, /bin \fRand\fP /usr/bin\fR
  495. are to be searched in that order.
  496. In this way individual users
  497. can have their own `private' commands
  498. that are accessible independently
  499. of the current directory.
  500. If the command name contains a \fB/\fR then this directory search
  501. is not used; a single attempt
  502. is made to execute the command.
  503. .IP \fB$\s-1PS1\s0\fP 8
  504. The primary shell prompt string, by default, `\fB$\ \fR'.
  505. .IP \fB$\s-1PS2\s0\fP 8
  506. The shell prompt when further input is needed,
  507. by default, `\fB>\ \fR'.
  508. .IP \fB$\s-1IFS\s0\fP 8
  509. The set of characters used by \fIblank
  510. interpretation\fR (see section 3.4).
  511. .RE
  512. .SH
  513. 2.5\ The\ test\ command
  514. .LP
  515. The \fItest\fP command, although not part of the shell,
  516. is intended for use by shell programs.
  517. For example,
  518. .DS
  519.     test \(mif file
  520. .DE
  521. returns zero exit status if \fIfile\fP
  522. exists and non-zero exit status otherwise.
  523. In general \fItest\fP evaluates a predicate
  524. and returns the result as its exit status.
  525. Some of the more frequently used \fItest\fP
  526. arguments are given here, see \fItest\fP (1)
  527. for a complete specification.
  528. .DS
  529.     test s        true if the argument \fIs\fP is not the null string
  530.     test \(mif file    true if \fIfile\fP exists
  531.     test \(mir file    true if \fIfile\fP is readable
  532.     test \(miw file    true if \fIfile\fP is writable
  533.     test \(mid file    true if \fIfile\fP is a directory
  534. .DE
  535. .SH
  536. 2.6\ Control\ flow\ -\ while
  537. .LP
  538. The actions of
  539. the \fBfor\fP loop and the \fBcase\fP
  540. branch are determined by data available to the shell.
  541. A \fBwhile\fP or \fBuntil\fP loop
  542. and an \fBif then else\fP branch
  543. are also provided whose
  544. actions are determined by the exit status
  545. returned by commands.
  546. A \fBwhile\fP loop has the general form
  547. .DS
  548.     \fBwhile\fP \fIcommand-list\*1\fP
  549.     \fBdo\fP \fIcommand-list\*2\fP
  550.     \fBdone\fP
  551. .DE
  552. .LP
  553. The value tested by the \fBwhile\fP command
  554. is the exit status of the last simple command
  555. following \fBwhile.\fP
  556. Each time round the loop
  557. \fIcommand-list\*1\fP is executed;
  558. if a zero exit status is returned then
  559. \fIcommand-list\*2\fP
  560. is executed;
  561. otherwise, the loop terminates.
  562. For example,
  563. .DS
  564.     while test $1
  565.     do \*(ZZ
  566.     \*(DOshift
  567.     done
  568. .DE
  569. is equivalent to
  570. .DS
  571.     for i
  572.     do \*(ZZ
  573.     done
  574. .DE
  575. \fIshift\fP is a shell command that
  576. renames the positional parameters
  577. \fB$2, $3, \*(ZZ\fR as \fB$1, $2, \*(ZZ\fR
  578. and loses \fB$1\|.\fP
  579. .LP
  580. Another kind of use for the \fBwhile/until\fP
  581. loop is to wait until some
  582. external event occurs and then run
  583. some commands.
  584. In an \fBuntil\fP loop
  585. the termination condition is reversed.
  586. For example,
  587. .DS
  588.     until test \(mif file
  589.     do sleep 300; done
  590.     \fIcommands\fP
  591. .DE
  592. will loop until \fIfile\fP exists.
  593. Each time round the loop it waits for
  594. 5 minutes before trying again.
  595. (Presumably another process
  596. will eventually create the file.)
  597. .SH
  598. 2.7\ Control\ flow\ -\ if
  599. .LP
  600. Also available is a
  601. general conditional branch
  602. of the form,
  603. .DS
  604.     \fBif\fP \fIcommand-list
  605.     \fBthen    \fIcommand-list
  606.     \fBelse    \fIcommand-list
  607.     \fBfi\fR
  608. .DE
  609. that tests the value returned by the last simple command
  610. following \fBif.\fP
  611. .LP
  612. The \fBif\fP command may be used
  613. in conjunction with the \fItest\fP command
  614. to test for the existence of a file as in
  615. .DS
  616.     if test \(mif file
  617.     then    \fIprocess file\fP
  618.     else    \fIdo something else\fP
  619.     fi
  620. .DE
  621. .LP
  622. An example of the use of \fBif, case\fP
  623. and \fBfor\fP constructions is given in
  624. section 2.10\|.
  625. .LP
  626. A multiple test \fBif\fP command
  627. of the form
  628. .DS
  629.     if \*(ZZ
  630.     then    \*(ZZ
  631.     else    if \*(ZZ
  632.         then    \*(ZZ
  633.         else    if \*(ZZ
  634.             \*(ZZ
  635.             fi
  636.         fi
  637.     fi
  638. .DE
  639. may be written using an extension of the \fBif\fP
  640. notation as,
  641. .DS
  642.     if \*(ZZ
  643.     then    \*(ZZ
  644.     elif    \*(ZZ
  645.     then    \*(ZZ
  646.     elif    \*(ZZ
  647.     \*(ZZ
  648.     fi
  649. .DE
  650. .LP
  651. The following example is the \fItouch\fP command
  652. which changes the `last modified' time for a list
  653. of files.
  654. The command may be used in conjunction
  655. with \fImake\fP (1) to force recompilation of a list
  656. of files.
  657. .DS
  658.     flag=
  659.     for i
  660.     do case $i in
  661.     \*(DC\(mic)    flag=N ;;
  662.     \*(DC\*(ST)    if test \(mif $i
  663.     \*(DC    then    ln $i junk$$; rm junk$$
  664.     \*(DC    elif test $flag
  665.     \*(DC    then    echo file \\\\\'$i\\\\\' does not exist
  666.     \*(DC    else    >$i
  667.     \*(DC    fi
  668.     \*(DO esac
  669.     done
  670. .DE
  671. The \fB\(mic\fP flag is used in this command to
  672. force subsequent files to be created if they do not already exist.
  673. Otherwise, if the file does not exist, an error message is printed.
  674. The shell variable \fIflag\fP
  675. is set to some non-null string if the \fB\(mic\fP
  676. argument is encountered.
  677. The commands
  678. .DS
  679.     ln \*(ZZ; rm \*(ZZ
  680. .DE
  681. make a link to the file and then remove it
  682. thus causing the last modified date to be updated.
  683. .LP
  684. The sequence
  685. .DS
  686.     if command1
  687.     then    command2
  688.     fi
  689. .DE
  690. may be written
  691. .DS
  692.     command1 && command2
  693. .DE
  694. Conversely,
  695. .DS
  696.     command1 \*(VT\*(VT command2
  697. .DE
  698. executes \fIcommand2\fP only if \fIcommand1\fP
  699. fails.
  700. In each case the value returned
  701. is that of the last simple command executed.
  702. .SH
  703. 2.8\ Command\ grouping
  704. .LP
  705. Commands may be grouped in two ways,
  706. .DS
  707.     \fB{\fI command-list\fB ; }\fR
  708. .DE
  709. and
  710. .DS
  711.     \fB(\fI command-list\fB )\fR
  712. .DE
  713. .LP
  714. In the first \fIcommand-list\fP is simply executed.
  715. The second form executes \fIcommand-list\fP
  716. as a separate process.
  717. For example,
  718. .DS
  719.     (cd x; rm junk )
  720. .DE
  721. executes \fIrm junk\fP in the directory
  722. \fBx\fP without changing the current
  723. directory of the invoking shell.
  724. .LP
  725. The commands
  726. .DS
  727.     cd x; rm junk
  728. .DE
  729. have the same effect but leave the invoking
  730. shell in the directory \fBx.\fP
  731. .SH
  732. 2.9\ Debugging\ shell\ procedures
  733. .LP
  734. The shell provides two tracing mechanisms
  735. to help when debugging shell procedures.
  736. The first is invoked within the procedure
  737. as
  738. .DS
  739.     set \(miv
  740. .DE
  741. (\fBv\fP for verbose) and causes lines of the
  742. procedure to be printed as they are read.
  743. It is useful to help isolate syntax errors.
  744. It may be invoked without modifying the procedure
  745. by saying
  746. .DS
  747.     sh \(miv proc \*(ZZ
  748. .DE
  749. where \fIproc\fP is the name of the shell procedure.
  750. This flag may be used in conjunction
  751. with the \fB\(min\fP flag which prevents
  752. execution of subsequent commands.
  753. (Note that saying \fIset \(min\fP at a terminal
  754. will render the terminal useless
  755. until an end-of-file is typed.)
  756. .LP
  757. The command
  758. .DS
  759.     set \(mix
  760. .DE
  761. will produce an execution
  762. trace.
  763. Following parameter substitution
  764. each command is printed as it is executed.
  765. (Try these at the terminal to see
  766. what effect they have.)
  767. Both flags may be turned off by saying
  768. .DS
  769.     set \(mi
  770. .DE
  771. and the current setting of the shell flags is available as \fB$\(mi\|.\fR
  772. .SH
  773. 2.10\ The\ man\ command
  774. .LP
  775. The following is the \fIman\fP command
  776. which is used to print sections of the UNIX manual.
  777. It is called, for example, as
  778. .DS
  779.         man sh
  780.         man \(mit ed
  781.         man 2 fork
  782. .DE
  783. In the first the manual section for \fIsh\fP
  784. is printed.
  785. Since no section is specified, section 1 is used.
  786. The second example will typeset (\fB\(mit\fP option)
  787. the manual section for \fIed.\fP
  788. The last prints the \fIfork\fP manual page
  789. from section 2.
  790. .sp 2
  791. .DS
  792.     cd /usr/man
  793.  
  794.     : \'colon is the comment command\'
  795.     : \'default is nroff ($N), section 1 ($s)\'
  796.     N=n\ s=1
  797.  
  798.     for i
  799.     do case $i in
  800. .sp .5
  801.     \*(DC[1\(mi9]\*(ST)    s=$i ;;
  802. .sp .5
  803.     \*(DC\(mit)    N=t ;;
  804. .sp .5
  805.     \*(DC\(min)    N=n ;;
  806. .sp .5
  807.     \*(DC\(mi\*(ST)    echo unknown flag \\\\\'$i\\\\\' ;;
  808. .sp .5
  809.     \*(DC\*(ST)    if test \(mif man$s/$i.$s
  810.     \*(DC    then    ${N}roff man0/${N}aa man$s/$i.$s
  811.     \*(DC    else    : \'look through all manual sections\'
  812.     \*(DC        found=no
  813.     \*(DC        for j in 1 2 3 4 5 6 7 8 9
  814.     \*(DC        do if test \(mif man$j/$i.$j
  815.     \*(DC        \*(DOthen man $j $i
  816.     \*(DC        \*(DO\*(THfound=yes
  817.     \*(DC        \*(DOfi
  818.     \*(DC        done
  819.     \*(DC        case $found in
  820.     \*(DC        \*(Cano) echo \\'$i: manual page not found\\'
  821.     \*(DC        esac
  822.     \*(DC    fi
  823.     \*(DOesac
  824.     done
  825. .DE
  826. .ce
  827. .ft B
  828. Figure 1. A version of the man command
  829. .ft R
  830.