home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / beginners / u2 < prev    next >
Encoding:
Text File  |  1979-01-10  |  23.7 KB  |  1,181 lines

  1. .SH
  2. II.  DAY-TO-DAY USE
  3. .SH
  4. Creating Files \(em The Editor
  5. .PP
  6. If you have to type a paper or a letter or a program,
  7. how do you get the information stored in the machine?
  8. Most of these tasks are done with
  9. the
  10. .UC UNIX
  11. ``text editor''
  12. .UL ed .
  13. Since
  14. .UL ed
  15. is thoroughly documented in 
  16. .UL ed (1) 
  17. and explained in
  18. .ul
  19. A Tutorial Introduction to the UNIX Text Editor,
  20. we won't spend any time here describing how to use it.
  21. All we want it for right now is to make some
  22. .ul
  23. files.
  24. (A file is just a collection of information stored in the machine,
  25. a simplistic but adequate definition.)
  26. .PP
  27. To create a file 
  28. called
  29. .UL junk
  30. with some text in it, do the following:
  31. .P1
  32. .ta .65i
  33. ed junk    \fR(invokes the text editor)\f3
  34. a    \fR(command to ``ed'', to add text)\f3
  35. .ft I
  36. now type in
  37. whatever text you want ...
  38. .ft 3
  39. \&.    \fR(signals the end of adding text)\f3
  40. .P2
  41. The ``\f3.\fR'' that signals the end of adding text must be
  42. at the beginning of a line by itself.
  43. Don't forget it,
  44. for until it is typed,
  45. no other
  46. .UL ed
  47. commands will be recognized \(em
  48. everything you type will be treated as text to be added.
  49. .PP
  50. At this point you can do various editing operations
  51. on the text you typed in, such as correcting spelling mistakes,
  52. rearranging paragraphs and the like.
  53. Finally, you must write the information you have typed
  54. into a file with the editor command
  55. .UL w :
  56. .P1
  57. w
  58. .P2
  59. .UL ed
  60. will respond with the number of characters it wrote
  61. into the file 
  62. .UL junk .
  63. .PP
  64. Until the
  65. .UL w
  66. command,
  67. nothing is stored permanently,
  68. so if you hang up and go home
  69. the information is lost.\(dg
  70. .FS
  71. \(dg This is not strictly true \(em
  72. if you hang up while editing, the data you were
  73. working on is saved in a file called
  74. .UL ed.hup ,
  75. which you can continue with at your next session.
  76. .FE
  77. But after
  78. .UL w
  79. the information is there permanently;
  80. you can re-access it any time by typing
  81. .P1
  82. ed junk
  83. .P2
  84. Type a
  85. .UL q
  86. command
  87. to quit the editor.
  88. (If you try to quit without writing,
  89. .UL ed
  90. will print a
  91. .UL ?
  92. to remind you.
  93. A second
  94. .UL q
  95. gets you out regardless.)
  96. .PP
  97. Now create a second file called 
  98. .UL temp
  99. in the same manner.
  100. You should now have two files,
  101. .UL junk
  102. and
  103. .UL temp .
  104. .SH
  105. What files are out there?
  106. .PP
  107. The
  108. .UL ls
  109. (for ``list'') command lists the names
  110. (not contents)
  111. of any of the files that
  112. .UC UNIX
  113. knows about.
  114. If you type
  115. .P1
  116. ls
  117. .P2
  118. the response will be
  119. .P1
  120. junk
  121. temp
  122. .P2
  123. which are indeed the two files just created.
  124. The names are sorted into alphabetical order automatically,
  125. but other variations are possible.
  126. For example,
  127. the command
  128. .P1
  129. ls -t
  130. .P2
  131. causes the files to be listed in the order in which they were last changed,
  132. most recent first.
  133. The
  134. .UL \-l
  135. option gives a ``long'' listing:
  136. .P1
  137. ls -l
  138. .P2
  139. will produce something like
  140. .P1
  141. -rw-rw-rw-  1 bwk   41 Jul 22 2:56 junk
  142. -rw-rw-rw-  1 bwk   78 Jul 22 2:57 temp
  143. .P2
  144. The date and time are of the last change to the file.
  145. The 41 and 78 are the number of characters
  146. (which should agree with the numbers you got from
  147. .UL ed ).
  148. .UL bwk
  149. is the owner of the file, that is, the person
  150. who created it.
  151. The
  152. .UL \-rw\-rw\-rw\- 
  153. tells who has permission to read and write the file,
  154. in this case everyone.
  155. .PP
  156. Options can be combined:
  157. .UL ls\ \-lt
  158. gives the same thing as
  159. .UL ls\ \-l ,
  160. but sorted into time order.
  161. You can also name the files you're interested in,
  162. and 
  163. .UL ls
  164. will list the information about them only.
  165. More details can be found in 
  166. .UL ls (1).
  167. .PP
  168. The use of optional arguments that begin with a minus sign,
  169. like
  170. .UL \-t
  171. and
  172. .UL \-lt ,
  173. is a common convention for
  174. .UC UNIX
  175. programs.
  176. In general, if a program accepts such optional arguments,
  177. they precede any filename arguments.
  178. It is also vital that you separate the various arguments with spaces:
  179. .UL ls\-l
  180. is not the same as
  181. .UL ls\ \ \-l .
  182. .SH
  183. Printing Files
  184. .PP
  185. Now that you've got a file of text,
  186. how do you print it so people can look at it?
  187. There are a host of programs that do that,
  188. probably more than are needed.
  189. .PP
  190. One simple thing is to use the editor,
  191. since printing is often done just before making changes anyway.
  192. You can say
  193. .P1
  194. ed junk
  195. 1,$p
  196. .P2
  197. .UL ed
  198. will reply with the count of the characters in 
  199. .UL junk
  200. and then print all the lines in the file.
  201. After you learn how to use the editor,
  202. you can be selective about the parts you print.
  203. .PP
  204. There are times when it's not feasible to use the editor for printing.
  205. For example, there is a limit on how big a file
  206. .UL ed
  207. can handle
  208. (several thousand lines).
  209. Secondly, 
  210. it
  211. will only print one file at a time,
  212. and sometimes you want to print several, one after another.
  213. So here are a couple of alternatives.
  214. .PP
  215. First is
  216. .UL cat ,
  217. the simplest of all the printing programs.
  218. .UL cat
  219. simply prints on the terminal the contents of all the files
  220. named in a list.
  221. Thus
  222. .P1
  223. cat junk
  224. .P2
  225. prints one file, and
  226. .P1
  227. cat junk temp
  228. .P2
  229. prints two.
  230. The files are simply concatenated (hence the name
  231. .UL cat '') ``
  232. onto the terminal.
  233. .PP
  234. .UL pr
  235. produces formatted printouts of files.
  236. As with 
  237. .UL cat ,
  238. .UL pr
  239. prints all the files named in a list.
  240. The difference is that it produces 
  241. headings with date, time, page number and file name
  242. at the top of each page,
  243. and
  244. extra lines to skip over the fold in the paper.
  245. Thus,
  246. .P1
  247. pr junk temp
  248. .P2
  249. will print
  250. .UL junk
  251. neatly,
  252. then skip to the top of a new page and print
  253. .UL temp
  254. neatly.
  255. .PP
  256. .UL pr
  257. can also produce multi-column output:
  258. .P1
  259. pr -3 junk 
  260. .P2
  261. prints
  262. .UL junk
  263. in 3-column format.
  264. You can use any reasonable number in place of ``3''
  265. and 
  266. .UL pr
  267. will do its best.
  268. .UL pr
  269. has other capabilities as well;
  270. see
  271. .UL pr (1).
  272. .PP
  273. It should be noted that
  274. .UL pr
  275. is
  276. .ul
  277. not
  278. a formatting program in the sense of shuffling lines around
  279. and justifying margins.
  280. The true formatters are
  281. .UL nroff
  282. and
  283. .UL troff ,
  284. which we will get to in the section on document preparation.
  285. .PP
  286. There are also programs that print files
  287. on a high-speed printer.
  288. Look in your manual under
  289. .UL opr
  290. and
  291. .UL lpr .
  292. Which to use depends on
  293. what equipment is attached to your machine.
  294. .SH
  295. Shuffling Files About
  296. .PP
  297. Now that you have some files in the file system
  298. and some experience in printing them,
  299. you can try bigger things.
  300. For example,
  301. you can move a file from one place to another
  302. (which amounts to giving it a new name),
  303. like this:
  304. .P1
  305. mv junk precious
  306. .P2
  307. This means that what used to be ``junk'' is now ``precious''.
  308. If you do an
  309. .UL ls
  310. command now,
  311. you will get
  312. .P1
  313. precious
  314. temp
  315. .P2
  316. Beware that if you move a file to another one
  317. that already exists,
  318. the already existing contents are lost forever.
  319. .PP
  320. If you want
  321. to make a
  322. .ul
  323. copy
  324. of a file (that is, to have two versions of something),
  325. you can use the 
  326. .UL cp
  327. command:
  328. .P1
  329. cp precious temp1
  330. .P2
  331. makes a duplicate copy of 
  332. .UL precious
  333. in
  334. .UL temp1 .
  335. .PP
  336. Finally, when you get tired of creating and moving
  337. files,
  338. there is a command to remove files from the file system,
  339. called
  340. .UL rm .
  341. .P1
  342. rm temp temp1
  343. .P2
  344. will remove both of the files named.
  345. .PP
  346. You will get a warning message if one of the named files wasn't there,
  347. but otherwise
  348. .UL rm ,
  349. like most
  350. .UC UNIX
  351. commands,
  352. does its work silently.
  353. There is no prompting or chatter,
  354. and error messages are occasionally curt.
  355. This terseness is sometimes disconcerting
  356. to new\%comers,
  357. but experienced users find it desirable.
  358. .SH
  359. What's in a Filename
  360. .PP
  361. So far we have used filenames without ever saying what's
  362. a legal name,
  363. so it's time for a couple of rules.
  364. First, filenames are limited to 14 characters,
  365. which is enough to be descriptive.
  366. Second, although you can use almost any character
  367. in a filename,
  368. common sense says you should stick to ones that are visible,
  369. and that you should probably avoid characters that might be used
  370. with other meanings.
  371. We have already seen, for example,
  372. that in the
  373. .UL ls
  374. command,
  375. .UL ls\ \-t
  376. means to list in time order.
  377. So if you had a file whose name
  378. was
  379. .UL \-t ,
  380. you would have a tough time listing it by name.
  381. Besides the minus sign, there are other characters which
  382. have special meaning.
  383. To avoid pitfalls,
  384. you would do well to 
  385. use only letters, numbers and the period
  386. until you're familiar with the situation.
  387. .PP
  388. On to some more positive suggestions.
  389. Suppose you're typing a large document
  390. like a book.
  391. Logically this divides into many small pieces,
  392. like chapters and perhaps sections.
  393. Physically it must be divided too,
  394. for 
  395. .UL ed
  396. will not handle really big files.
  397. Thus you should type the document as a number of files.
  398. You might have a separate file for each chapter,
  399. called
  400. .P1
  401. chap1
  402. chap2
  403. .ft R
  404. etc...
  405. .P2
  406. Or, if each chapter were broken into several files, you might have
  407. .P1
  408. chap1.1
  409. chap1.2
  410. chap1.3
  411. \&...
  412. chap2.1
  413. chap2.2
  414. \&...
  415. .P2
  416. You can now tell at a glance where a particular file fits into the whole.
  417. .PP
  418. There are advantages to a systematic naming convention which are not obvious
  419. to the novice
  420. .UC UNIX 
  421. user.
  422. What if you wanted to print the whole book?
  423. You could say
  424. .P1
  425. pr chap1.1 chap1.2 chap1.3 ......
  426. .P2
  427. but you would get tired pretty fast, and would probably even make mistakes.
  428. Fortunately, there is a shortcut.
  429. You can say
  430. .P1
  431. pr chap*
  432. .P2
  433. The
  434. .UL *
  435. means ``anything at all,''
  436. so this translates into ``print all files
  437. whose names begin with 
  438. .UL chap '',
  439. listed in alphabetical order.
  440. .PP
  441. This shorthand notation
  442. is not a property of the
  443. .UL pr
  444. command, by the way.
  445. It is system-wide, a service of the program
  446. that interprets commands
  447. (the ``shell,''
  448. .UL sh (1)).
  449. Using that fact, you can see how to list the names of the files in the book:
  450. .P1
  451. ls chap*
  452. .P2
  453. produces
  454. .P1
  455. chap1.1
  456. chap1.2
  457. chap1.3
  458. \&...
  459. .P2
  460. The
  461. .UL *
  462. is not limited to the last position in a filename \(em
  463. it can be anywhere
  464. and can occur several times.
  465. Thus
  466. .P1
  467. rm *junk* *temp*
  468. .P2
  469. removes all files that contain
  470. .UL junk
  471. or
  472. .UL temp
  473. as any part of their name.
  474. As a special case,
  475. .UL *
  476. by itself matches every filename,
  477. so
  478. .P1
  479. pr *
  480. .P2
  481. prints all your files
  482. (alphabetical order),
  483. and
  484. .P1
  485. rm *
  486. .P2
  487. removes
  488. .ul
  489. all files.
  490. (You had better be
  491. .IT  very 
  492. sure that's what you wanted to say!)
  493. .PP
  494. The
  495. .UL *
  496. is not 
  497. the only pattern-matching feature available.
  498. Suppose you want to print only chapters 1 through 4 and 9.
  499. Then you can say
  500. .P1
  501. pr chap[12349]*
  502. .P2
  503. The
  504. .UL [...]
  505. means to match any of the characters inside the brackets.
  506. A range of consecutive letters or digits can be abbreviated,
  507. so you can also do this 
  508. with
  509. .P1
  510. pr chap[1-49]*
  511. .P2
  512. Letters can also be used within brackets:
  513. .UL [a\-z]
  514. matches any character in the range
  515. .UL a
  516. through
  517. .UL z .
  518. .PP
  519. The
  520. .UL ?
  521. pattern matches any single character,
  522. so
  523. .P1
  524. ls ?
  525. .P2
  526. lists all files which have single-character names,
  527. and
  528. .P1
  529. ls -l chap?.1
  530. .P2
  531. lists information about the first file of each chapter
  532. .UL chap1.1 \&, (
  533. .UL chap2.1 ,
  534. etc.).
  535. .PP
  536. Of these niceties,
  537. .UL *
  538. is certainly the most useful,
  539. and you should get used to it.
  540. The others are frills, but worth knowing.
  541. .PP
  542. If you should ever have to turn off the special meaning
  543. of
  544. .UL * ,
  545. .UL ? ,
  546. etc.,
  547. enclose the entire argument in single quotes,
  548. as in
  549. .P1
  550. ls \(fm?\(fm
  551. .P2
  552. We'll see some more examples of this shortly.
  553. .SH
  554. What's in a Filename, Continued
  555. .PP
  556. When you first made that file called
  557. .UL junk ,
  558. how did 
  559. the system
  560. know that there wasn't another
  561. .UL junk
  562. somewhere else,
  563. especially since the person in the next office is also
  564. reading this tutorial?
  565. The answer is that generally each user 
  566. has a private
  567. .IT directory ,
  568. which contains only the files that belong to him.
  569. When you log in, you are ``in'' your directory.
  570. Unless you take special action,
  571. when you create a new file,
  572. it is made in the directory that you are currently in;
  573. this is most often your own directory,
  574. and thus the file is unrelated to any other file of the same name
  575. that might exist in someone else's directory.
  576. .PP
  577. The set of all files
  578. is organized into a (usually big) tree,
  579. with your files located several branches into the tree.
  580. It is possible for you to ``walk'' around this tree,
  581. and to find any file in the system, by starting at the root
  582. of the tree and walking along the proper set of branches.
  583. Conversely, you can start where you are and walk toward the root.
  584. .PP
  585. Let's try the latter first.
  586. The basic tools is the command
  587. .UL pwd
  588. (``print working directory''),
  589. which prints the name of the directory you are currently in.
  590. .PP
  591. Although the details will vary according to the system you are on,
  592. if you give the
  593. command
  594. .UL pwd ,
  595. it will print something like
  596. .P1
  597. /usr/your\(hyname
  598. .P2
  599. This says that you are currently in the directory
  600. .UL your-name ,
  601. which is in turn in the directory
  602. .UL /usr ,
  603. which is in turn in the root directory
  604. called by convention just
  605. .UL / .
  606. (Even if it's not called
  607. .UL /usr
  608. on your system,
  609. you will get something analogous.
  610. Make the corresponding changes and read on.)
  611. .PP
  612. If you now type
  613. .P1
  614. ls /usr/your\(hyname
  615. .P2
  616. you should get exactly the same list of file names
  617. as you get from a plain
  618. .UL ls  :
  619. with no arguments,
  620. .UL ls
  621. lists the contents of the current directory;
  622. given the name of a directory,
  623. it lists the contents of that directory.
  624. .PP
  625. Next, try
  626. .P1
  627. ls /usr
  628. .P2
  629. This should print a long series of names,
  630. among which is your own login name
  631. .UL your-name .
  632. On many systems, 
  633. .UL usr
  634. is a directory that contains the directories
  635. of all the normal users of the system,
  636. like you.
  637. .PP
  638. The next step is to try
  639. .P1
  640. ls /
  641. .P2
  642. You should get a response something like this
  643. (although again the details may be different):
  644. .P1
  645. bin
  646. dev
  647. etc
  648. lib
  649. tmp
  650. usr
  651. .P2
  652. This is a collection of the basic directories of files
  653. that
  654. the system
  655. knows about;
  656. we are at the root of the tree.
  657. .PP
  658. Now try
  659. .P1
  660. cat /usr/your\(hyname/junk
  661. .P2
  662. (if
  663. .UL junk
  664. is still around in your directory).
  665. The name
  666. .P1
  667. /usr/your\(hyname/junk
  668. .P2
  669. is called the
  670. .UL pathname
  671. of the file that
  672. you normally think of as ``junk''.
  673. ``Pathname'' has an obvious meaning:
  674. it represents the full name of the path you have to follow from the root
  675. through the tree of directories to get to a particular file.
  676. It is a universal rule in
  677. the
  678. .UC UNIX
  679. system
  680. that anywhere you can use an ordinary filename,
  681. you can use a pathname.
  682. .PP
  683. Here is a picture which may make this clearer:
  684. .P1 1
  685. .ft R
  686. .if t .vs 9p
  687. .if t .tr /\(sl
  688. .if t .tr ||
  689. .ce 100
  690. (root)
  691. / | \e
  692. /  |  \e
  693. /   |   \e
  694.   bin    etc    usr    dev   tmp 
  695. / | \e   / | \e   / | \e   / | \e   / | \e
  696. /  |  \e
  697. /   |   \e
  698. adam  eve   mary
  699. /        /   \e        \e
  700.              /     \e       junk
  701. junk temp
  702. .ce 0
  703. .br
  704. .tr //
  705. .P2
  706. .LP
  707. Notice that Mary's
  708. .UL junk
  709. is unrelated to Eve's.
  710. .PP
  711. This isn't too exciting if all the files of interest are in your own
  712. directory, but if you work with someone else
  713. or on several projects concurrently,
  714. it becomes handy indeed.
  715. For example, your friends can print your book by saying
  716. .P1
  717. pr /usr/your\(hyname/chap*
  718. .P2
  719. Similarly, you can find out what files your neighbor has
  720. by saying
  721. .P1
  722. ls /usr/neighbor\(hyname
  723. .P2
  724. or make your own copy of one of his files by
  725. .P1
  726. cp /usr/your\(hyneighbor/his\(hyfile yourfile
  727. .P2
  728. .PP
  729. If your neighbor doesn't want you poking around in his files,
  730. or vice versa,
  731. privacy can be arranged.
  732. Each file and directory has read-write-execute permissions for the owner,
  733. a group, and everyone else,
  734. which can be set
  735. to control access.
  736. See
  737. .UL ls (1)
  738. and
  739. .UL chmod (1)
  740. for details.
  741. As a matter of observed fact,
  742. most users most of the time find openness of more
  743. benefit than privacy.
  744. .PP
  745. As a final experiment with pathnames, try
  746. .P1
  747. ls /bin /usr/bin
  748. .P2
  749. Do some of the names look familiar?
  750. When you run a program, by typing its name after the prompt character,
  751. the system simply looks for a file of that name.
  752. It normally looks first in your directory
  753. (where it typically doesn't find it),
  754. then in
  755. .UL /bin
  756. and finally in
  757. .UL /usr/bin .
  758. There is nothing magic about commands like
  759. .UL cat
  760. or
  761. .UL ls ,
  762. except that they have been collected into a couple of places to be easy to find and administer.
  763. .PP
  764. What if you work regularly with someone else on common information
  765. in his directory?
  766. You could just log in as your friend each time you want to,
  767. but you can also say
  768. ``I want to work on his files instead of my own''.
  769. This is done by changing the directory that you are
  770. currently in:
  771. .P1
  772. cd /usr/your\(hyfriend
  773. .P2
  774. (On some systems,
  775. .UL cd
  776. is spelled
  777. .UL chdir .)
  778. Now when you use a filename in something like
  779. .UL cat
  780. or
  781. .UL pr ,
  782. it refers to the file in your friend's directory.
  783. Changing directories doesn't affect any permissions associated
  784. with a file \(em
  785. if you couldn't access a file from your own directory,
  786. changing to another directory won't alter that fact.
  787. Of course,
  788. if you forget what directory you're in, type
  789. .P1
  790. pwd
  791. .P2
  792. to find out.
  793. .PP
  794. It is usually convenient to arrange your own files
  795. so that all the files related to one thing are in a directory separate
  796. from other projects.
  797. For example, when you write your book, you might want to keep all the text
  798. in a directory called
  799. .UL book .
  800. So make one with
  801. .P1
  802. mkdir book
  803. .P2
  804. then go to it with
  805. .P1
  806. cd book
  807. .P2
  808. then start typing chapters.
  809. The book is now found in (presumably)
  810. .P1
  811. /usr/your\(hyname/book
  812. .P2
  813. To remove the directory
  814. .UL book ,
  815. type
  816. .P1
  817. rm book/*
  818. rmdir book
  819. .P2
  820. The first command removes all files from the directory;
  821. the second
  822. removes the empty directory.
  823. .PP
  824. You can go up one level in the tree of files 
  825. by saying
  826. .P1
  827. cd ..
  828. .P2
  829. .UL .. '' ``
  830. is the name of the parent of whatever directory you are currently in.
  831. For completeness,
  832. .UL . '' ``
  833. is an alternate name
  834. for the directory you are in.
  835. .SH
  836. Using Files instead of the Terminal
  837. .PP
  838. Most of the commands we have seen so far produce output
  839. on the terminal;
  840. some, like the editor, also take their input from the terminal.
  841. It is universal in
  842. .UC UNIX
  843. systems
  844. that the terminal can be replaced by a file
  845. for either or both of input and output.
  846. As one example,
  847. .P1
  848. ls
  849. .P2
  850. makes a list of files on your terminal.
  851. But if you say
  852. .P1
  853. ls >filelist
  854. .P2
  855. a list of your files will be placed in the file
  856. .UL filelist
  857. (which
  858. will be created if it doesn't already exist,
  859. or overwritten if it does).
  860. The symbol
  861. .UL >
  862. means ``put the output on the following file,
  863. rather than on the terminal.''
  864. Nothing is produced on the terminal.
  865. As another example, you could combine
  866. several files into one by capturing the output of
  867. .UL cat
  868. in a file:
  869. .P1
  870. cat f1 f2 f3 >temp
  871. .P2
  872. .PP
  873. The symbol
  874. .UL >>
  875. operates very much like
  876. .UL >
  877. does,
  878. except that it means
  879. ``add to the end of.''
  880. That is,
  881. .P1
  882. cat f1 f2 f3 >>temp
  883. .P2
  884. means to concatenate
  885. .UL f1 ,
  886. .UL f2 
  887. and
  888. .UL f3
  889. to the end of whatever is already in
  890. .UL temp ,
  891. instead of overwriting the existing contents.
  892. As with
  893. .UL > ,
  894. if 
  895. .UL temp
  896. doesn't exist, it will be created for you.
  897. .PP
  898. In a similar way, the symbol
  899. .UL <
  900. means to take the input
  901. for a program from the following file,
  902. instead of from the terminal.
  903. Thus, you could make up a script of commonly used editing commands
  904. and put them into a file called
  905. .UL script .
  906. Then you can run the script on a file by saying
  907. .P1
  908. ed file <script
  909. .P2
  910. As another example, you can use
  911. .UL ed
  912. to prepare a letter in file
  913. .UL let ,
  914. then send it to several people with
  915. .P1
  916. mail adam eve mary joe <let
  917. .P2
  918. .SH
  919. Pipes
  920. .PP
  921. One of the novel contributions of
  922. the
  923. .UC UNIX
  924. system
  925. is the idea of a
  926. .ul
  927. pipe.
  928. A pipe is simply a way to connect the output of one program
  929. to the input of another program,
  930. so the two run as a sequence of processes \(em
  931. a pipeline.
  932. .PP
  933. For example,
  934. .P1
  935. pr f g h
  936. .P2
  937. will print the files
  938. .UL f ,
  939. .UL g ,
  940. and
  941. .UL h ,
  942. beginning each on a new page.
  943. Suppose you want
  944. them run together instead.
  945. You could say
  946. .P1
  947. cat f g h >temp
  948. pr <temp
  949. rm temp
  950. .P2
  951. but this is more work than necessary.
  952. Clearly what we want is to take the output of
  953. .UL cat
  954. and
  955. connect it to the input of
  956. .UL pr .
  957. So let us use a pipe:
  958. .P1
  959. cat f g h | pr
  960. .P2
  961. The vertical bar 
  962. .UL |
  963. means to
  964. take the output from
  965. .UL cat ,
  966. which would normally have gone to the terminal,
  967. and put it into
  968. .UL pr
  969. to be neatly formatted.
  970. .PP
  971. There are many other examples of pipes.
  972. For example,
  973. .P1
  974. ls | pr -3
  975. .P2
  976. prints a list of your files in three columns.
  977. The program
  978. .UL wc
  979. counts the number of lines, words and characters in
  980. its input, and as we saw earlier,
  981. .UL who
  982. prints a list of currently-logged on people,
  983. one per line.
  984. Thus
  985. .P1
  986. who | wc
  987. .P2
  988. tells how many people are logged on.
  989. And of course
  990. .P1
  991. ls | wc
  992. .P2
  993. counts your files.
  994. .PP
  995. Any program
  996. that reads from the terminal
  997. can read from a pipe instead;
  998. any program that writes on the terminal can drive
  999. a pipe.
  1000. You can have as many elements in a pipeline as you wish.
  1001. .PP
  1002. Many
  1003. .UC UNIX
  1004. programs are written so that they will take their input from one or more files
  1005. if file arguments are given;
  1006. if no arguments are given they will read from the terminal,
  1007. and thus can be used in pipelines.
  1008. .UL pr
  1009. is one example:
  1010. .P1
  1011. pr -3 a b c
  1012. .P2
  1013. prints files
  1014. .UL a ,
  1015. .UL b
  1016. and
  1017. .UL c
  1018. in order in three columns.
  1019. But in
  1020. .P1
  1021. cat a b c | pr -3
  1022. .P2
  1023. .UL pr
  1024. prints the information coming down the pipeline,
  1025. still in
  1026. three columns.
  1027. .SH
  1028. The Shell
  1029. .PP
  1030. We have already mentioned once or twice the mysterious
  1031. ``shell,''
  1032. which is in fact
  1033. .UL sh (1).
  1034. The shell is the program that interprets what you type as
  1035. commands and arguments.
  1036. It also looks after translating
  1037. .UL * ,
  1038. etc.,
  1039. into lists of filenames,
  1040. and
  1041. .UL < ,
  1042. .UL > ,
  1043. and
  1044. .UL |
  1045. into changes of input and output streams.
  1046. .PP
  1047. The shell has other capabilities too.
  1048. For example, you can run two programs with one command line
  1049. by separating the commands with a semicolon;
  1050. the shell recognizes the semicolon and
  1051. breaks the line into two commands.
  1052. Thus
  1053. .P1
  1054. date; who
  1055. .P2
  1056. does both commands before returning with a prompt character.
  1057. .PP
  1058. You can also have more than one program running
  1059. .ul
  1060. simultaneously
  1061. if you wish.
  1062. For example, if you are doing something time-consuming,
  1063. like the editor script
  1064. of an earlier section,
  1065. and you don't want to wait around for the results before starting something else,
  1066. you can say
  1067. .P1
  1068. ed file <script &
  1069. .P2
  1070. The ampersand at the end of a command line
  1071. says ``start this command running,
  1072. then take further commands from the terminal immediately,''
  1073. that is,
  1074. don't wait for it to complete.
  1075. Thus the script will begin,
  1076. but you can do something else at the same time.
  1077. Of course, to keep the output from interfering
  1078. with what you're doing on the terminal,
  1079. it would be better to say
  1080. .P1
  1081. ed file <script >script.out &
  1082. .P2
  1083. which saves the output lines in a file
  1084. called
  1085. .UL script.out .
  1086. .PP
  1087. When you initiate a command with
  1088. .UL & ,
  1089. the system
  1090. replies with a number
  1091. called the process number,
  1092. which identifies the command in case you later want
  1093. to stop it.
  1094. If you do, you can say
  1095. .P1
  1096. kill process\(hynumber
  1097. .P2
  1098. If you forget the process number,
  1099. the command
  1100. .UL ps
  1101. will tell you about everything you have running.
  1102. (If you are desperate,
  1103. .UL kill\ 0
  1104. will kill all your processes.)
  1105. And if you're curious about other people,
  1106. .UL ps\ a
  1107. will tell you about
  1108. .ul
  1109. all
  1110. programs that are currently running.
  1111. .PP
  1112. You can say
  1113. .P1 1
  1114. (command\(hy1; command\(hy2; command\(hy3) &
  1115. .P2
  1116. to start three commands in the background,
  1117. or you can start a background pipeline with
  1118. .P1
  1119. command\(hy1 | command\(hy2 &
  1120. .P2
  1121. .PP
  1122. Just as you can tell the editor
  1123. or some similar program to take its input
  1124. from a file instead of from the terminal,
  1125. you can tell the shell to read a file
  1126. to get commands.
  1127. (Why not? The shell, after all, is just a program,
  1128. albeit a clever one.)
  1129. For instance, suppose you want to set tabs on
  1130. your terminal, and find out the date
  1131. and who's on the system every time you log in.
  1132. Then you can put the three necessary commands
  1133. .UL tabs , (
  1134. .UL date ,
  1135. .UL who )
  1136. into a file, let's call it
  1137. .UL startup ,
  1138. and then run it with
  1139. .P1
  1140. sh startup
  1141. .P2
  1142. This says to run the shell with the file
  1143. .UL startup
  1144. as input.
  1145. The effect is as if you had typed 
  1146. the contents of
  1147. .UL startup
  1148. on the terminal.
  1149. .PP
  1150. If this is to be a regular thing,
  1151. you can eliminate the 
  1152. need to type
  1153. .UL sh :
  1154. simply type, once only, the command
  1155. .P1
  1156. chmod +x startup
  1157. .P2
  1158. and thereafter you need only say
  1159. .P1
  1160. startup
  1161. .P2
  1162. to run the sequence of commands.
  1163. The
  1164. .UL chmod (1)
  1165. command marks the file executable;
  1166. the shell recognizes this and runs it as a sequence of commands.
  1167. .PP
  1168. If you want 
  1169. .UL startup
  1170. to run automatically every time you log in,
  1171. create a file in your login directory called
  1172. .UL .profile ,
  1173. and place in it the line
  1174. .UL startup .
  1175. When the shell first gains control when you log in,
  1176. it looks for the 
  1177. .UL .profile
  1178. file and does whatever commands it finds in it.
  1179. We'll get back to the shell in the section
  1180. on programming.
  1181.