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

  1. .SH
  2. IV.  PROGRAMMING
  3. .PP
  4. .UC UNIX
  5. is a marvelously pleasant and productive system for
  6. writing programs;
  7. productivity seems to be an order of magnitude higher
  8. than on other interactive systems.
  9. .PP
  10. There will be no attempt made to teach any of
  11. the programming languages available on
  12. .UC UNIX ,
  13. but a few words of advice are in order.
  14. First,
  15. .UC UNIX
  16. is written in C,
  17. as is most of the applications code.
  18. If you are undertaking anything substantial,
  19. C is the only reasonable choice.
  20. More on that in a moment.
  21. But remember that there are quite a few programs already written,
  22. some of which have substantial power.
  23. .PP
  24. The editor can be made to do things that would normally
  25. require special programs on other systems.
  26. For example, to list the first and last lines of each of a
  27. set of files, say a book,
  28. you could laboriously type
  29. .B1
  30. ed
  31. e chap1.1
  32. 1p
  33. $p
  34. e chap1.2
  35. 1p
  36. $p
  37.  etc.
  38. .B2
  39. But instead you can do the job once and for all.
  40. Type
  41. .B1
  42. ls chap* >temp
  43. .B2
  44. to get the list of filenames into a file.
  45. Then edit this file to make the necessary
  46. series of editing commands
  47. (using the global commands of
  48. .C ed ),
  49. and write it into ``script''.
  50. Now the command
  51. .B1
  52. ed <script
  53. .B2
  54. will produce
  55. the same output as the laborious hand typing.
  56. .PP
  57. The pipe mechanism lets you fabricate quite complicated operations
  58. out of spare parts already built.
  59. For example, the first draft of the 
  60. .C spell
  61. program was (roughly)
  62. .B1
  63. .ne 7
  64. cat ...     (collect the files)
  65. | tr ...    (put each word on a new line,
  66.       delete punctuation, etc.)
  67. | sort    (into dictionary order)
  68. | uniq    (strip out duplicates)
  69. | comm    (list words found in text but
  70.       not in dictionary)
  71. .B2
  72. .SH
  73. Programming the Shell
  74. .PP
  75. An option often overlooked by newcomers
  76. is that the shell is itself a programming language,
  77. and since
  78. .UC UNIX
  79. already has a host of building-block programs,
  80. you can sometimes avoid writing a special purpose program
  81. merely by piecing together some of the building blocks
  82. with shell command files.
  83. .PP
  84. As an unlikely example,
  85. suppose you want to count
  86. the number of users on the machine every hour.
  87. You could type
  88. .B1
  89. .ne 2
  90. date
  91. who | wc -l
  92. .B2
  93. every hour, and write down the numbers, but that is rather primitive.
  94. The next step is probably to say
  95. .B1
  96. (date; who | wc -l) >>users
  97. .B2
  98. which uses ``>>'' to 
  99. .ul
  100. append
  101. to the end of the file ``users''.
  102. (We haven't mentioned ``>>'' before _
  103. it's another service of the shell.)
  104. Now all you have to do is to put a loop around this,
  105. and ensure that it's done every hour.
  106. Thus, place the following commands into a file, say ``count'':
  107. .B1
  108. .ne 4
  109. : loop
  110. (date; who | wc -l) >>users
  111. sleep  3600
  112. goto loop
  113. .B2
  114. The command 
  115. .C :
  116. is followed by a space and a label,
  117. which you can then 
  118. .C goto .
  119. Notice that it's quite legal to branch backwards.
  120. Now if you issue the command
  121. .B1
  122. sh  count  &
  123. .B2
  124. the users will be counted every hour,
  125. and you can go on with other things.
  126. (You will have to use
  127. .C kill
  128. to stop counting.)
  129. .PP
  130. If you would like ``every hour'' to be a parameter,
  131. you can arrange for that too:
  132. .B1
  133. .ne 4
  134. : loop
  135. (date; who | wc - l) >>users
  136. sleep  $1
  137. goto loop
  138. .B2
  139. ``$1'' means the first argument when this procedure is invoked.
  140. If you say
  141. .B1
  142. sh count 60
  143. .B2
  144. it will count every minute.
  145. A shell program can have up to nine arguments,
  146. ``$1'' through ``$9''.
  147. .PP
  148. The other aspect of programming is conditional testing.
  149. The
  150. .C if
  151. command
  152. can test conditions and execute commands accordingly.
  153. As a simple example, suppose you want to add to your login sequence
  154. something to print your mail if you have some.
  155. Thus, knowing that mail is stored in a file called
  156. `mailbox', you could say
  157. .B1
  158. if -r mailbox  mail
  159. .B2
  160. This says
  161. ``if the file `mailbox' is readable, execute the
  162. .C mail
  163. command.''
  164. .PP
  165. As another example, you could arrange that the
  166. ``count''
  167. procedure count every hour by default,
  168. but allow an optional argument to specify a different time.
  169. Simply replace the ``sleep $1'' line by
  170. .B1
  171. .ne 2
  172. if $1x = x sleep 3600
  173. if $1x != x sleep $1
  174. .B2
  175. The construction
  176. .B1
  177. if $1x = x
  178. .B2
  179. tests whether ``$1'', the first argument, was present or absent.
  180. .PP
  181. More complicated conditions can be tested:
  182. you can find out the status of an executed command,
  183. and you can combine conditions with `and', `or',
  184. `not' and parentheses _
  185. see
  186. .SE if (I).
  187. You should also read
  188. .SE shift (I)
  189. which describes how to manipulate arguments to shell command files.
  190. .SH
  191. Programming in C
  192. .PP
  193. As we said, C is the language of choice:
  194. everything in
  195. .UC UNIX
  196. is tuned to it.
  197. It is also a remarkably easy language to use
  198. once you get started.
  199. Sections II and III of the manual
  200. describe the system interfaces, that is,
  201. how you do I/O
  202. and similar functions.
  203. .PP
  204. You can write quite significant C programs
  205. with the level of I/O and system interface described in
  206. .ul
  207. Programming in C: A Tutorial,
  208. if you use existing programs and pipes to help.
  209. For example, rather than learning how to open and close files
  210. you can (at least temporarily) write a program that reads
  211. from its standard input,
  212. and use
  213. .C cat
  214. to concatentate several files into it.
  215. This may not be adequate for the long run,
  216. but for the early stages it's just right.
  217. .PP
  218. There are a number of supporting programs that go with C.
  219. The C debugger,
  220. .C cdb ,
  221. is marginally useful for digging through the dead bodies
  222. of C programs.
  223. .C db ,
  224. the assembly language debugger, is actually more useful most
  225. of the time,
  226. but you have to know more about the machine
  227. and system to use it well.
  228. The most effective debugging tool is still
  229. careful thought, coupled with judiciously placed
  230. print statements.
  231. .PP
  232. You can instrument C programs and thus find out
  233. where they spend their time and what parts are worth optimising.
  234. Compile the routines with the ``-p'' option;
  235. after the test run use
  236. .C prof
  237. to print an execution profile.
  238. The command
  239. .C time
  240. will give you the gross run-time statistics
  241. of a program, but it's not super accurate or reproducible.
  242. .PP
  243. C programs that don't depend too much on special features of 
  244. .UC UNIX
  245. can be moved to the Honeywell 6070
  246. and
  247. .UC IBM
  248. 370 systems
  249. with modest effort.
  250. Read 
  251. .ul 3
  252. The
  253. .UC GCOS
  254. C Library
  255. by M. E. Lesk and B. A. Barres for details.
  256. .SH
  257. Miscellany
  258. .PP
  259. If you 
  260. .ul
  261. have
  262. to use Fortran,
  263. you might consider
  264. .C ratfor ,
  265. which gives you the decent control structures
  266. and free-form input that characterize C,
  267. yet lets you write code that
  268. is still portable to other environments.
  269. Bear in mind that
  270. .UC UNIX
  271. Fortran
  272. tends to produce large and relatively slow-running
  273. programs.
  274. Furthermore, supporting software like
  275. .C db ,
  276. .C prof ,
  277. etc., are all virtually useless with Fortran programs.
  278. .PP
  279. If you want to use assembly language
  280. (all heavens forfend!),
  281. try the implementation language
  282. .UC LIL,
  283. which gives you many of the advantages of a high-level language,
  284. like decent control flow structures,
  285. but still lets you get close to the machine
  286. if you really want to.
  287. .PP
  288. If your application requires you to translate
  289. a language into a set of actions or another language,
  290. you are in effect building a compiler,
  291. though probably a small one.
  292. In that case,
  293. you should be using
  294. the
  295. .C yacc
  296. compiler-compiler, 
  297. which helps you develop a compiler quickly.
  298.