Commit | Line | Data |
---|---|---|
47d38f87 BJ |
1 | .RP |
2 | .TL | |
3 | An introduction to the C shell | |
4 | .AU | |
5 | William Joy | |
6 | .AI | |
7 | Computer Science Division | |
8 | .br | |
9 | Department of Electrical Engineering and Computer Science | |
10 | .br | |
11 | University of California, Berkeley | |
12 | .br | |
13 | Berkeley, California 94720 | |
14 | .AB | |
15 | .I Csh | |
16 | is a new command language interpreter for \s-2UNIX\s0 | |
17 | systems. | |
18 | It incorporates good features of other shells and a | |
19 | .I history | |
20 | mechanism similar to the | |
21 | .I redo | |
22 | of \s-2INTERLISP\s0. | |
23 | While incorporating many features of other shells which make | |
24 | writing shell programs (shell scripts) easier, | |
25 | most of the features unique to | |
26 | .I csh | |
27 | are designed more for the interactive \s-2UNIX\s0 user. | |
28 | .PP | |
29 | \s-2UNIX\s0 | |
30 | users who have read a general introduction to the system | |
31 | will find a valuable basic explanation of the shell here. | |
32 | Simple terminal interaction with | |
33 | .I csh | |
34 | is possible after reading just the first section of this document. | |
35 | The second section describes the shells capabilities which you can | |
36 | explore after you have begun to become acquainted with the shell. | |
37 | Later sections introduce features which are useful, but not necessary | |
38 | for all users of the shell. | |
39 | .PP | |
40 | Back matter includes an appendix listing special characters of the shell | |
41 | and a glossary of terms and commands introduced in this manual. | |
42 | .AE | |
43 | .SH | |
44 | .if n .ND | |
45 | Introduction | |
46 | .PP | |
47 | A | |
48 | .I shell | |
49 | is a command language interpreter. | |
50 | .I Csh | |
51 | is the name of one particular command interpreter on | |
52 | \s-2UNIX\s0. | |
53 | The primary purpose of | |
54 | .I csh | |
55 | is to translate command lines typed at a terminal into | |
56 | system actions, such as invocation of other programs. | |
57 | .I Csh | |
58 | is a user program just like any you might write. | |
59 | Hopefully, | |
60 | .I csh | |
61 | will be a very useful program for you | |
62 | in interacting with the \s-2UNIX\s0 system. | |
63 | .PP | |
64 | In addition to this document, you will want to refer to a copy | |
65 | of the ``\s-2UNIX\s0 Programmers Manual.'' | |
66 | The | |
67 | .I csh | |
68 | documentation in the manual provides a full description of all | |
69 | features of the shell and is a final reference for questions | |
70 | about the shell. | |
71 | .PP | |
72 | Many words in this document are shown in | |
73 | .I italics. | |
74 | These are important words; | |
75 | names of commands, and words which have special meaning in discussing | |
76 | the shell and \s-2UNIX\s0. | |
77 | Many of the words are defined in a glossary at the end of this document. | |
78 | If you don't know what is meant by a word, you should look | |
79 | for it in the glossary. | |
80 | .SH | |
81 | Acknowledgements | |
82 | .PP | |
83 | Numerous people have provided good input about previous versions | |
84 | of | |
85 | .I csh | |
86 | and aided in its debugging and in the debugging of its documentation. | |
87 | I would especially like to thank Michael Ubell | |
88 | who made the crucial observation that history commands could be | |
89 | done well over the word structure of input text, and implemented | |
90 | a prototype history mechanism in an older version of the shell. | |
91 | Eric Allman has also provided a large number of useful comments on the | |
92 | shell, helping to unify those concepts which are present and to identify | |
93 | and eliminate useless and marginally useful features. | |
94 | .br | |
95 | .bp | |
96 | .NH | |
97 | Terminal usage of the shell | |
98 | .NH 2 | |
99 | The basic notion of commands | |
100 | .PP | |
101 | A | |
102 | .I shell | |
103 | in | |
104 | \s-2UNIX\s0 | |
105 | acts mostly as a medium through which other | |
106 | .I commands | |
107 | are invoked. | |
108 | While it has a set of | |
109 | .I builtin | |
110 | commands which it performs directly, | |
111 | most useful commands are, in fact, external to the shell. | |
112 | The shell is thus distinguished from the command interpreters of other | |
113 | systems both by the fact that it is just a user program, and by the fact | |
114 | that it is used almost exclusively as a mechanism for invoking other programs. | |
115 | .PP | |
116 | Commands in the \s-2UNIX\s0 system expect a list of strings or | |
117 | .I words | |
118 | as arguments. | |
119 | Thus the command | |
120 | .DS | |
121 | mail bill | |
122 | .DE | |
123 | consists of two words. | |
124 | The first word | |
125 | .I mail | |
126 | names the command to be executed, in this case the | |
127 | mail program which sends messages to other users. | |
128 | The shell uses the name of the command in attempting to run it for you. | |
129 | It will look in a number of | |
130 | .I directories | |
131 | for a file with the name | |
132 | .I mail | |
133 | which is expected to contain the mail program. | |
134 | .PP | |
135 | The rest of the words of the command are given to the command itself to execute. | |
136 | In this case we specified also the word | |
137 | .I bill | |
138 | which is interpreted by the | |
139 | .I mail | |
140 | program to be the name of a user to whom mail is to be sent. | |
141 | In normal terminal usage we might use the | |
142 | .I mail | |
143 | command as follows. | |
144 | .DS | |
145 | % mail bill | |
146 | I have a question about the csh documentation. | |
147 | My document seems to be missing page 5. | |
148 | Does a page five exist? | |
149 | Bill | |
150 | % | |
151 | .DE | |
152 | .PP | |
153 | Here we typed a message to send to | |
154 | .I bill | |
155 | and ended this message with a control-d which sent an end-of-file to | |
156 | the mail program. | |
157 | The mail program | |
158 | then transmitted our message. | |
159 | The characters `% ' were printed before and after the mail command | |
160 | by the shell to indicate that input was needed. | |
161 | .PP | |
162 | After typing the `% ' prompt the shell was reading command input from | |
163 | our terminal. | |
164 | We typed a complete command `mail bill'. | |
165 | The shell then executed the | |
166 | .I mail | |
167 | program with argument | |
168 | .I bill | |
169 | and went dormant waiting for it to complete. | |
170 | The mail program then read input from our terminal until we signalled | |
171 | an end-of-file after which the shell noticed that mail had completed | |
172 | and signaled us that it was ready to read from the terminal again by | |
173 | printing another `% ' prompt. | |
174 | .PP | |
175 | This is the essential pattern of all interaction with \s-2UNIX\s0 | |
176 | through the shell. | |
177 | A complete command is typed at the terminal, the shell executes | |
178 | the command and when this execution completes prompts for a new command. | |
179 | If you run the editor for an hour, the shell will patiently wait for | |
180 | you to finish editing and obediently prompt you again whenever you finish | |
181 | editing. | |
182 | .NH 2 | |
183 | Flag arguments | |
184 | .PP | |
185 | A useful notion in \s-2UNIX\s0 is that of a | |
186 | .I flag | |
187 | argument. | |
188 | While many arguments to commands specify file names or user names | |
189 | some arguments rather specify an optional capability of the command | |
190 | which you wish to invoke. | |
191 | By convention, such arguments begin with the character `\-'. | |
192 | Thus the command | |
193 | .DS | |
194 | ls | |
195 | .DE | |
196 | will produce a list of the files in the current directory. | |
197 | The option | |
198 | .I \-s | |
199 | is the size option, and | |
200 | .DS | |
201 | ls \-s | |
202 | .DE | |
203 | causes | |
204 | .I ls | |
205 | to also give, for each file the size of the file in blocks of 512 | |
206 | characters. | |
207 | The manual page for each command in the \s-2UNIX\s0 programmers manual | |
208 | gives the available options for each command. | |
209 | The | |
210 | .I ls | |
211 | command has a large number of useful and interesting options. | |
212 | Most other commands have either no options or only one or two options. | |
213 | It is hard to remember options of commands which are not used very | |
214 | frequently, so most \s-2UNIX\s0 utilities perform only one or two functions | |
215 | rather than having a large number of hard to remember options. | |
216 | .NH 2 | |
217 | Output to files | |
218 | .PP | |
219 | Many commands may read input or write output to files rather | |
220 | than simply taking input and output from the terminal. | |
221 | Each such command could take special words as arguments indicating | |
222 | where the output is to go. | |
223 | It is simpler, and usually | |
224 | sufficient, to connect these commands to files to which they wish to | |
225 | write, within the shell itself, and just before they are executed. | |
226 | .PP | |
227 | Thus suppose we wish to save the current date in a file called `now'. | |
228 | The command | |
229 | .DS | |
230 | date | |
231 | .DE | |
232 | will print the current date on our terminal. | |
233 | This is because our terminal is the default | |
234 | .I "standard output" | |
235 | for the date command and the date command prints the date on its | |
236 | standard output. | |
237 | The shell lets us redirect the | |
238 | .I "standard output" | |
239 | of a command through a | |
240 | notation using the | |
241 | .I metacharacter | |
242 | `>' and the name of the file where output is to be placed. | |
243 | Thus the command | |
244 | .DS | |
245 | date > now | |
246 | .DE | |
247 | runs the | |
248 | .I date | |
249 | command in an environment where its standard output is | |
250 | the file `now' rather than our terminal. | |
251 | Thus this command places the current date and time in the file `now'. | |
252 | It is important to know that the | |
253 | .I date | |
254 | command was unaware that its output was going to a file rather than | |
255 | to our terminal. | |
256 | The shell performed this | |
257 | .I redirection | |
258 | before the command began executing. | |
259 | .PP | |
260 | One other thing to note here is that the file `now' | |
261 | need not have existed before the | |
262 | .I date | |
263 | command was executed; the shell would have created the file if it did | |
264 | not exist. | |
265 | And if the file did exist? | |
266 | If it had existed previously these previous contents would have been discarded! | |
267 | A shell option | |
268 | .I noclobber | |
269 | exists to prevent this from happening accidentally; | |
270 | it is discussed in section 2.2. | |
271 | .NH 2 | |
272 | Metacharacters in the shell | |
273 | .PP | |
274 | The shell has a large number of | |
275 | special characters (like `>') | |
276 | which indicate special functions. | |
277 | We say that these notations have | |
278 | .I syntactic | |
279 | and | |
280 | .I semantic | |
281 | meaning to the shell. | |
282 | In general, most characters which are neither letters nor digits | |
283 | have special meaning to the shell. | |
284 | We shall shortly learn a means of | |
285 | .I quotation | |
286 | which allows us to create words which contain | |
287 | .I metacharacters | |
288 | and to thus work without constantly worrying about whether certain | |
289 | characters are metacharacters. | |
290 | .PP | |
291 | Note that the shell is only reading input when it has prompted with | |
292 | `% '. | |
293 | Thus metacharacters will normally have effect only then. | |
294 | We need not worry about placing shell metacharacters in a letter | |
295 | we are sending via | |
296 | .I mail. | |
297 | .NH 2 | |
298 | Input from files; pipelines | |
299 | .PP | |
300 | We learned above how to route the standard output of a command | |
301 | to a file. | |
302 | It is also possible to route the standard input of a command from a file. | |
303 | This is not often necessary since most commands will read from | |
304 | a file name given as argument. | |
305 | We can give the command | |
306 | .DS | |
307 | sort < data | |
308 | .DE | |
309 | to run the | |
310 | .I sort | |
311 | command with standard input, where the command normally | |
312 | reads, from the file | |
313 | `data'. | |
314 | We would more likely say | |
315 | .DS | |
316 | sort data | |
317 | .DE | |
318 | letting the | |
319 | .I sort | |
320 | command open the file | |
321 | `data' | |
322 | for input itself since this is less to type. | |
323 | .PP | |
324 | We should note that if we just typed | |
325 | .DS | |
326 | sort | |
327 | .DE | |
328 | then the sort program would sort lines from its | |
329 | .I "standard input." | |
330 | Since we did not | |
331 | .I redirect | |
332 | the standard input, it would sort lines as we typed them on the terminal | |
333 | until we typed a control-d to generate an end-of-file. | |
334 | .PP | |
335 | A most useful capability is the ability to combine the standard output | |
336 | of one command with the standard input of the next, i.e. to run the | |
337 | commands in a sequence known as a | |
338 | .I pipeline. | |
339 | For instance the command | |
340 | .DS | |
341 | ls \-s | |
342 | .DE | |
343 | normally produces a list of the files in our directory with the size | |
344 | of each in blocks of 512 characters. | |
345 | If we are interested in learning which of our files is largest we | |
346 | may wish to have this sorted by size rather than by name, which is | |
347 | the default way in which | |
348 | .I ls | |
349 | sorts. | |
350 | We could look at the many options of | |
351 | .I ls | |
352 | to see if there was an option to do this but would eventually discover | |
353 | that there is not. | |
354 | Instead we can use a couple of simple options of the | |
355 | .I sort | |
356 | command, combining it with | |
357 | .I ls | |
358 | to get what we want. | |
359 | .PP | |
360 | The | |
361 | .I \-n | |
362 | option of sort specifies a numeric sort rather than an alphabetic sort. | |
363 | Thus | |
364 | .DS | |
365 | ls \-s | sort \-n | |
366 | .DE | |
367 | specifies that the output of the | |
368 | .I ls | |
369 | command run with the option | |
370 | .I \-s | |
371 | is to be | |
372 | .I piped | |
373 | to the command | |
374 | .I sort | |
375 | run with the numeric sort option. | |
376 | This would give us a sorted list of our files by size, but with the | |
377 | smallest first. | |
378 | We could then use the | |
379 | .I \-r | |
380 | reverse sort option and the | |
381 | .I head | |
382 | command in combination with the previous command doing | |
383 | .DS | |
384 | ls \-s | sort \-n \-r | head \-5 | |
385 | .DE | |
386 | Here we have taken a list of our files sorted alphabetically, | |
387 | each with the size in blocks. | |
388 | We have run this to the standard input of the | |
389 | .I sort | |
390 | command asking it to sort numerically in reverse order (largest first). | |
391 | This output has then been run into the command | |
392 | .I head | |
393 | which gives us the first few lines out. | |
394 | In this case we have asked | |
395 | .I head | |
396 | for the first 5 lines. | |
397 | Thus this command gives us the names and sizes of our 5 largest files. | |
398 | .PP | |
399 | The metanotation introduced above is called the | |
400 | .I pipe | |
401 | mechanism. | |
402 | Commands separated by `\||\|' characters are connected together by the | |
403 | shell and the output of each is run into the input of the next. | |
404 | The leftmost command in a pipeline will normally take its standard | |
405 | input from the terminal and the rightmost will place its standard | |
406 | output on the terminal. | |
407 | Other examples of pipelines will be given later when we discuss the | |
408 | history mechanism; | |
409 | one important use of pipes which is illustrated there is in the | |
410 | routing of information to the line printer. | |
411 | .NH 2 | |
412 | Filenames | |
413 | .PP | |
414 | Many commands to be executed will need the names of files as arguments. | |
415 | \s-2UNIX\s0 pathnames consist of a number of components separated by `/'. | |
416 | Each component except the last names a directory in which the next | |
417 | component resides. | |
418 | Thus the pathname | |
419 | .DS | |
420 | /etc/motd | |
421 | .DE | |
422 | specifies a file in the directory | |
423 | `etc' | |
424 | which is a subdirectory of the | |
425 | .I root | |
426 | directory `/'. | |
427 | Within this directory the file named is `motd' which stands | |
428 | for `message of the day'. | |
429 | Filenames which do not begin with `/' are interpreted starting at | |
430 | the current | |
431 | .I working | |
432 | directory. | |
433 | This directory is, by default, your | |
434 | .I home | |
435 | directory and can be changed dynamically by the | |
436 | .I chdir | |
437 | change directory command. | |
438 | .PP | |
439 | Most filenames consist of a number of alphanumeric characters and `.'s. | |
440 | In fact, all printing characters except `/' may appear in filenames. | |
441 | It is inconvenient to have most non-alphabetic characters in filenames | |
442 | because many of these have special meaning to the shell. | |
443 | The character `.' is not a shell-metacharacter and is often used as | |
444 | the prefix with an | |
445 | .I extension | |
446 | of a base name. | |
447 | Thus | |
448 | .DS | |
449 | prog.c prog.o prog.errs prog.output | |
450 | .DE | |
451 | are four related files. | |
452 | They share a | |
453 | .I root | |
454 | portion of a name | |
455 | (a root portion being that part of the name that is left when a trailing | |
456 | `.' and following characters which are not `.' are stripped off). | |
457 | The file | |
458 | `prog.c' | |
459 | might be the source for a C program, | |
460 | the file `prog.o' the corresponding object file, | |
461 | the file | |
462 | `prog.errs' the errors resulting from a compilation of the program | |
463 | and the file | |
464 | `prog.output' the output of a run of the program. | |
465 | .PP | |
466 | If we wished to refer to all four of these files in a command, we could | |
467 | use the metanotation | |
468 | .DS | |
469 | prog.* | |
470 | .DE | |
471 | This word is expanded by the shell, before the command to which it is | |
472 | an argument is executed, into a list of names which begin with `prog.'. | |
473 | The character `*' here matches any sequence (including the empty sequence) | |
474 | of characters in a file name. | |
475 | The names which match are sorted into the argument list to the command | |
476 | alphabetically. | |
477 | Thus the command | |
478 | .DS | |
479 | echo prog.* | |
480 | .DE | |
481 | will echo the names | |
482 | .DS | |
483 | prog.c prog.errs prog.o prog.output | |
484 | .DE | |
485 | Note that the names are in lexicographic order here, and a different | |
486 | order than we listed them above. | |
487 | The | |
488 | .I echo | |
489 | command receives four words as arguments, even though we only typed | |
490 | one word as as argument directly. | |
491 | The four words were generated by filename expansion of the metasyntax | |
492 | in the one input word. | |
493 | .PP | |
494 | Other metanotations for | |
495 | .I "filename expansion" | |
496 | are also available. | |
497 | The character `?' matches any single character in a filename. | |
498 | Thus | |
499 | .DS | |
500 | echo ? \|?? \|??? | |
501 | .DE | |
502 | will echo a line of filenames; first those with one character names, | |
503 | then those with two character names, and finally those with three | |
504 | character names. | |
505 | The names of each length will be independently lexicographically sorted. | |
506 | .PP | |
507 | Another mechanism consists of a sequence of characters between `[' and `]'. | |
508 | This metasequence matches any single character from the enclosed set. | |
509 | Thus | |
510 | .DS | |
511 | prog.[co] | |
512 | .DE | |
513 | will match | |
514 | .DS | |
515 | prog.c prog.o | |
516 | .DE | |
517 | in the example above. | |
518 | We can also place two characters astride a `\-' in this notation to denote | |
519 | a range. | |
520 | Thus | |
521 | .DS | |
522 | chap.[1\-5] | |
523 | .DE | |
524 | might match files | |
525 | .DS | |
526 | chap.1 chap.2 chap.3 chap.4 chap.5 | |
527 | .DE | |
528 | if they existed. | |
529 | This is shorthand for | |
530 | .DS | |
531 | chap.[12345] | |
532 | .DE | |
533 | and otherwise equivalent. | |
534 | .PP | |
535 | An important point to note is that if a list of argument words to | |
536 | a command (an | |
537 | .I "argument list)" | |
538 | contains filename expansion syntax, and if this filename expansion syntax | |
539 | fails to match any existing file names, then the shell considers this | |
540 | to be an error and prints a diagnostic | |
541 | .DS | |
542 | No match. | |
543 | .DE | |
544 | .PP | |
545 | Another very important point is that the character `.' at the beginning | |
546 | of a filename is treated specially. | |
547 | Neither `*' or `?' or the `[' `]' mechanism will match it. | |
548 | This prevents accidental matching of the filenames `.' and `..' | |
549 | in the current directory which have special meaning to the system, | |
550 | as well as other files such as | |
551 | .I \&.cshrc | |
552 | which are not normally | |
553 | visible. | |
554 | We will discuss the special role of the file | |
555 | .I \&.cshrc | |
556 | later. | |
557 | .PP | |
558 | Another filename expansion mechanism gives access to the pathname of | |
559 | the | |
560 | .I home | |
561 | directory of other users. | |
562 | This notation consists of the character `~' followed by another users | |
563 | login name. | |
564 | For instance the word `~bill' would map to the pathname `/mnt/bill' | |
565 | if the home directory for `bill' was in the directory `/mnt/bill'. | |
566 | Since, on large systems, users may have login directories scattered over | |
567 | many different disk volumes with different prefix directory names, | |
568 | this notation provides a reliable way of accessing the files | |
569 | of other users. | |
570 | .PP | |
571 | A special case of this notation consists of a `~' alone, e.g. `~/mbox'. | |
572 | This notation is expanded by the shell into the file `mbox' in your | |
573 | .I home | |
574 | directory, i.e. into `/mnt/bill/mbox' for me on the Cory Hall \s-2UNIX\s0 | |
575 | system. | |
576 | This can be very useful if you have used | |
577 | .I chdir | |
578 | to change to another users directory and have found a file you wish to | |
579 | copy using | |
580 | .I cp. | |
581 | You can do | |
582 | .DS | |
583 | cp thatfile ~ | |
584 | .DE | |
585 | which will be expanded by the shell to | |
586 | .DS | |
587 | cp thatfile /mnt/bill | |
588 | .DE | |
589 | e.g., which the copy command will interpret as a request to make a copy of | |
590 | `thatfile' | |
591 | in the directory `/mnt/bill'. | |
592 | The `~' notation doesn't, by itself, force named files to exist. | |
593 | This is useful, for example, when using the | |
594 | .I cp | |
595 | command, | |
596 | e.g. | |
597 | .DS | |
598 | cp thatfile ~/saveit | |
599 | .DE | |
600 | .PP | |
601 | There also exists a mechanism using the characters `{' and `}' for | |
602 | abbreviating a set of word which have common parts but cannot | |
603 | be abbreviated by the above mechanisms because they are not files, | |
604 | are the names of files which do not yet exist, | |
605 | are not thus conveniently described. | |
606 | This mechanism will be described much later, | |
607 | in section 4.1, | |
608 | as it is used much less frequently. | |
609 | .NH 2 | |
610 | Quotation | |
611 | .PP | |
612 | We have already seen a number of metacharacters used by the shell. | |
613 | These metacharacter pose a problem in that we cannot use them directly | |
614 | as parts of words. | |
615 | Thus the command | |
616 | .DS | |
617 | echo * | |
618 | .DE | |
619 | will not echo the character `*'. | |
620 | It will either echo an sorted list of filenames in the | |
621 | current directory, or print the message `No match' if there are | |
622 | no files in the current directory. | |
623 | .PP | |
624 | The recommended mechanism for placing characters which are neither numbers, | |
625 | digits, `/', `.' or `\-' in an argument word to a command is to enclose | |
626 | it with single quotation characters `\'', i.e. | |
627 | .DS | |
628 | echo \'*\' | |
629 | .DE | |
630 | There is one special character `!' which is used by the | |
631 | .I history | |
632 | mechanism of the shell and which cannot be | |
633 | .I escaped | |
634 | in this way. | |
635 | It and the character `\'' itself can be preceded by a single `\e' | |
636 | to prevent their special meaning. | |
637 | These two mechanisms suffice to place any printing character into a word | |
638 | which is an argument to a shell command. | |
639 | .NH 2 | |
640 | Terminating commands | |
641 | .PP | |
642 | When you are running a command from the shell and the shell is | |
643 | dormant waiting for it to complete there are a couple of ways in | |
644 | which you can force such a command to complete. | |
645 | For instance if you type the command | |
646 | .DS | |
647 | cat /etc/passwd | |
648 | .DE | |
649 | the system will print a copy of a list of all users of the system | |
650 | on your terminal. | |
651 | This is likely to continue for several minutes unless you stop it. | |
652 | You can send an | |
653 | \s-2INTERRUPT\s0 | |
654 | signal to the | |
655 | .I cat | |
656 | command by hitting the | |
657 | \s-2DEL\s0 or \s-2RUBOUT\s0 | |
658 | key on your terminal. | |
659 | Actually, hitting this key sends this | |
660 | \s-2INTERRUPT\s0 | |
661 | signal to all programs running on your terminal, including your shell. | |
662 | The shell normally ignores such signals however, so that the only | |
663 | program affected by the | |
664 | \s-2INTERRUPT\s0 | |
665 | will be | |
666 | .I cat. | |
667 | Since | |
668 | .I cat | |
669 | does not take any precautions to catch this signal | |
670 | the | |
671 | \s-2INTERRUPT\s0 | |
672 | will cause it to terminate. | |
673 | The shell notices that | |
674 | .I cat | |
675 | has died and prompts you again with `% '. | |
676 | If you hit \s-2INTERRUPT\s0 again, the shell will just | |
677 | repeat its prompt since it catches \s-2INTERRUPT\s0 signals | |
678 | and chooses to continue to execute commands rather than going away | |
679 | like | |
680 | .I cat | |
681 | did, which would have the effect of logging you out. | |
682 | .PP | |
683 | Another way in which many programs terminate is when they get an end-of-file | |
684 | from their standard input. | |
685 | Thus the | |
686 | .I mail | |
687 | program in the first example above was terminated when we hit a control-d | |
688 | which generates and end-of-file from the standard input. | |
689 | The shell also terminates when it gets an end-of-file printing `logout'; | |
690 | \s-2UNIX\s0 then logs you off the system. | |
691 | Since this means that typing too many control-d's can accidentally log us off, | |
692 | the shell has a mechanism for preventing this. | |
693 | This | |
694 | .I ignoreeof | |
695 | option will be discussed in section 2.2. | |
696 | .PP | |
697 | If a command has its standard input redirected from a file, then it will | |
698 | normally terminate when it reaches the end of this file. | |
699 | Thus if we execute | |
700 | .DS | |
701 | mail bill < prepared.text | |
702 | .DE | |
703 | the mail command will terminate without our typing a control-d. | |
704 | This is because it read to the end-of-file of our file | |
705 | `prepared.text' in which we placed a message for `bill' with an editor. | |
706 | We could also have done | |
707 | .DS | |
708 | cat prepared.text \||\| mail bill | |
709 | .DE | |
710 | since the | |
711 | .I cat | |
712 | command would then have written the text through the pipe to the | |
713 | standard input of the mail command. | |
714 | When the | |
715 | .I cat | |
716 | command completed it would have terminated, | |
717 | closing down the pipeline | |
718 | and the | |
719 | .I mail | |
720 | command would have received an end-of-file from it and terminated. | |
721 | Using a pipe here is more complicated than redirecting input | |
722 | so we would more likely use the first form. | |
723 | These commands could also have been stopped by sending an \s-2INTERRUPT\s0. | |
724 | .PP | |
725 | If you write or run programs which are not fully debugged then it may | |
726 | be necessary to stop them somewhat ungracefully. | |
727 | This can be done by sending them a \s-2QUIT\s0 | |
728 | signal, generated by a control-\e. | |
729 | This will usually provoke the shell to produce a message like: | |
730 | .DS | |
731 | a.out: Quit \-\- Core dumped | |
732 | .DE | |
733 | indicating that a file | |
734 | `core' has been created containing information about the program `a.out's | |
735 | state when it ran amuck. | |
736 | You can examine this file yourself, or forward information to the | |
737 | maintainer of the program telling him/her where the | |
738 | .I "core file" | |
739 | is. | |
740 | .PP | |
741 | If you run background commands (as explained in section 2.6) then these | |
742 | commands will ignore \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals at the | |
743 | terminal. To stop them you must use the | |
744 | .I kill | |
745 | program. See section 2.6 for an example. | |
746 | .NH 2 | |
747 | What now? | |
748 | .PP | |
749 | We have so far seen a number of mechanisms of the shell and learned a lot | |
750 | about the way in which it operates. | |
751 | The remaining sections will go yet further into the internals of the | |
752 | shell, but you will surely want to try using the shell before you go any further. | |
753 | To try it you can log in to \s-2UNIX\s0 and type the following | |
754 | command to the system: | |
755 | .DS | |
756 | chsh myname /bin/csh | |
757 | .DE | |
758 | Here | |
759 | `myname' | |
760 | should be replaced by the name you typed to | |
761 | the system prompt of | |
762 | `login:' to get onto the system. | |
763 | Thus I would use `chsh bill /bin/csh'. | |
764 | .B | |
765 | You only have to do this once; it takes effect at next login. | |
766 | .R | |
767 | You are now ready to try using | |
768 | .I csh. | |
769 | .PP | |
770 | Before you do the `chsh' command, the shell you are using when | |
771 | you log into the system is `/bin/sh'. | |
772 | In fact, much of the above discussion is applicable to `/bin/sh'. | |
773 | The next section will introduce many features particular to | |
774 | .I csh | |
775 | so you should change your shell to | |
776 | .I csh | |
777 | before you begin reading it. | |
778 | .bp | |
779 | .NH | |
780 | Details on the shell for terminal users | |
781 | .NH 2 | |
782 | Shell startup and termination | |
783 | .PP | |
784 | When you login, the shell is placed by the system in your | |
785 | .I home | |
786 | directory and begins by reading commands from a file | |
787 | .I \&.cshrc | |
788 | in this directory. | |
789 | All shells which you may create during your terminal session will | |
790 | read from this file. | |
791 | We will later see what kinds of commands are usefully placed there. | |
792 | For now we need not have this file and the shell does not complain about | |
793 | its absence. | |
794 | .PP | |
795 | A | |
796 | .I login | |
797 | shell, executed after you login to the system, | |
798 | will, after it reads commands from | |
799 | .I \&.cshrc, | |
800 | read commands from a file | |
801 | .I \&.login | |
802 | also in your home directory. | |
803 | This file contains commands which you wish to do each time you login | |
804 | to the \s-2UNIX\s0 system. | |
805 | My | |
806 | .I \&.login | |
807 | file looks something like: | |
808 | .DS | |
809 | tset \-d adm3a \-p adm3a | |
810 | fixexrc | |
811 | set history=20 | |
812 | set time=3 | |
813 | .DE | |
814 | on the \s-2CORY\s0 Hall \s-2UNIX\s0 system. | |
815 | This file contains four commands to be executed by \s-2UNIX\s0 | |
816 | each time I login. | |
817 | The first is a | |
818 | .I tset | |
819 | command which informs the system that I usually dial in on a Lear-Siegler | |
820 | \s-2ADM\-3A\s0 terminal and that if I am on a patchboard port on the | |
821 | fifth floor of Evans Hall I am probably also on an \s-2ADM\-3A\s0. | |
822 | The second command is a | |
823 | .I fixexrc | |
824 | which manipulates my | |
825 | .I ex | |
826 | startup file | |
827 | in certain ways if I am on a dialup port. | |
828 | We need not be concerned with exactly what this command does. | |
829 | In general you may have certain commands in your | |
830 | .I \&.login | |
831 | which are particular to you. | |
832 | .PP | |
833 | The next two | |
834 | .I set | |
835 | commands are interpreted directly by the shell and affect the values | |
836 | of certain shell variables to modify the future behavior of the shell. | |
837 | Setting the variable | |
838 | .I time | |
839 | tells the shell to print time statistics on commands which take more | |
840 | than a certain threshold of machine time (in this case 3 \s-2CPU\s0 | |
841 | seconds). | |
842 | Setting the variable | |
843 | .I history | |
844 | tells the shell how much history of previous command words it should | |
845 | save in case I wish to repeat or rerun modified versions of previous | |
846 | commands. | |
847 | Since there is a certain overhead in this mechanism the shell does | |
848 | not set this variable by default, but rather lets users who wish to | |
849 | use the mechanism set it themselves. | |
850 | The value of 20 is a reasonably large value to assign to | |
851 | .I history. | |
852 | More casual users of the | |
853 | .I history | |
854 | mechanism | |
855 | would probably set a value of 5 or 10. | |
856 | The use of the | |
857 | .I history | |
858 | mechanism will be described subsequently. | |
859 | .PP | |
860 | After executing commands from | |
861 | .I \&.login | |
862 | the shell reads commands from your terminal, prompting for each with | |
863 | `% '. | |
864 | When it receives an end-of-file from the terminal, the shell | |
865 | will print `logout' and execute commands from the file `.logout' | |
866 | in your home directory. | |
867 | After that the shell will die and \s-2UNIX\s0 will log you off the system. | |
868 | If the system is not going down, you will receive a new login message. | |
869 | In any case, after the `logout' message the shell is doomed | |
870 | and will take no further input from the terminal. | |
871 | .NH 2 | |
872 | Shell variables | |
873 | .PP | |
874 | The shell maintains a set of | |
875 | .I variables. | |
876 | We saw above the variables | |
877 | .I history | |
878 | and | |
879 | .I time | |
880 | which had values `20' and `3'. | |
881 | In fact, each shell variable has as value an array of | |
882 | zero or more | |
883 | .I strings. | |
884 | Shell variables may be assigned values by the set command. It has | |
885 | several forms, the most useful of which was given above and is | |
886 | .DS | |
887 | set name=value | |
888 | .DE | |
889 | .PP | |
890 | Shell variables may be used to store values which are to | |
891 | be reintroduced into commands later through a substitution mechanism. | |
892 | The shell variables most commonly referenced are, however, those which the | |
893 | shell itself refers to. | |
894 | By changing the values of these variables one can directly affect the | |
895 | behavior of the shell. | |
896 | .PP | |
897 | One of the most important variables is the variable | |
898 | .I path. | |
899 | This variable contains a sequence of directory names where the shell | |
900 | searches for commands. | |
901 | The | |
902 | .I set | |
903 | command | |
904 | shows the value of all variables currently defined (we usually say | |
905 | .I set) | |
906 | in the shell. | |
907 | The default value for path will be shown by | |
908 | .I set | |
909 | to be | |
910 | .DS | |
911 | % set | |
912 | argv | |
913 | home /mnt/bill | |
914 | path (. /bin /usr/bin) | |
915 | prompt % | |
916 | shell /bin/csh | |
917 | status 0 | |
918 | % | |
919 | .DE | |
920 | This notation indicates that the variable path points to the current | |
921 | directory `.' and then `/bin' and `/usr/bin'. | |
922 | Commands which you may write might be in `.' (usually one of | |
923 | your directories). | |
924 | The most heavily used system commands live in `/bin'. | |
925 | Less heavily used system commands live in `/usr/bin'. | |
926 | .PP | |
927 | A number of new programs on the system live in the directory | |
928 | `/usr/new'. | |
929 | If we wish, as well we might, all shells which we invoke to have | |
930 | access to these new programs we can place the command | |
931 | .DS | |
932 | set path=(. /usr/new /bin /usr/bin) | |
933 | .DE | |
934 | in our file | |
935 | .I \&.cshrc | |
936 | in our home directory. | |
937 | Try doing this and then logging out and back in and do | |
938 | .DS | |
939 | set | |
940 | .DE | |
941 | again to see that the value assigned to | |
942 | .I path | |
943 | has changed. | |
944 | .PP | |
945 | Other useful built in variables are the variable | |
946 | .I home | |
947 | which shows your home directory, | |
948 | the variable | |
949 | .I ignoreeof | |
950 | which can be set in your | |
951 | .I \&.login | |
952 | file to tell the shell not to exit when it receives an end-of-file from | |
953 | a terminal. | |
954 | To logout from \s-2UNIX\s0 | |
955 | with | |
956 | .I ignoreeof | |
957 | set you must type | |
958 | .DS | |
959 | logout | |
960 | .DE | |
961 | This is one of several variables which the shell does not care about the | |
962 | value of, only whether they are | |
963 | .I set | |
964 | or | |
965 | .I unset. | |
966 | Thus to set this variable you simply do | |
967 | .DS | |
968 | set ignoreeof | |
969 | .DE | |
970 | and to unset it do | |
971 | .DS | |
972 | unset ignoreeof | |
973 | .DE | |
974 | Both | |
975 | .I set | |
976 | and | |
977 | .I unset | |
978 | are built-in commands of the shell. | |
979 | .PP | |
980 | Finally, some other built-in shell variables of use are the | |
981 | variables | |
982 | .I noclobber | |
983 | and | |
984 | .I mail. | |
985 | The metasyntax | |
986 | .DS | |
987 | > filename | |
988 | .DE | |
989 | which redirects the output of a command | |
990 | will overwrite and destroy the previous contents of the named file. | |
991 | In this way you may accidentally overwrite a file which is valuable. | |
992 | If you would prefer that the shell not overwrite files in this | |
993 | way you can | |
994 | .DS | |
995 | set noclobber | |
996 | .DE | |
997 | in your | |
998 | .I \&.login | |
999 | file. | |
1000 | Then trying to do | |
1001 | .DS | |
1002 | date > now | |
1003 | .DE | |
1004 | would cause a diagnostic if `now' existed already. | |
1005 | You could type | |
1006 | .DS | |
1007 | date >! now | |
1008 | .DE | |
1009 | if you really wanted to overwrite the contents of `now'. | |
1010 | The `>!' is a special metasyntax indicating that clobbering the | |
1011 | file is ok. | |
1012 | .PP | |
1013 | If you receive mail frequently while you are logged in and wish to | |
1014 | be informed of the arrival of this mail you can put a command | |
1015 | .DS | |
1016 | set mail=/usr/mail/yourname | |
1017 | .DE | |
1018 | in your | |
1019 | .I \&.login | |
1020 | file. | |
1021 | Here you should change `yourname' to your login name. | |
1022 | The shell will look at this file every 10 minutes to see if new mail has arrived. | |
1023 | If you receive mail only infrequently you are better off not | |
1024 | setting this variable. In this case it will only serve to delay the shells | |
1025 | response to you when it checks for mail. | |
1026 | .PP | |
1027 | The use of shell variables to introduce text into commands, which is | |
1028 | most useful in shell command scripts, will be introduced in section 2.4. | |
1029 | .NH 2 | |
1030 | The shell's history list | |
1031 | .PP | |
1032 | The shell can maintain a history list into which it places the words | |
1033 | of previous commands. | |
1034 | It is possible to use a metanotation to reintroduce commands or words | |
1035 | from commands in forming new commands. | |
1036 | This mechanism can be used to repeat previous commands or to | |
1037 | correct minor typing mistakes in commands. | |
1038 | .PP | |
1039 | Consider the following transcript: | |
1040 | .DS | |
1041 | % where michael | |
1042 | michael is on tty0 dialup 300 baud 642-7927 | |
1043 | % write !$ | |
1044 | write michael | |
1045 | Long time no see michael. | |
1046 | Why don't you call me at 524-4510. | |
1047 | EOF | |
1048 | % | |
1049 | .DE | |
1050 | Here we asked the system where | |
1051 | .I michael | |
1052 | was logged in. | |
1053 | It told us he was on `tty0' | |
1054 | and we told the shell to invoke a `write' command to | |
1055 | `!$'. | |
1056 | This is a history notation which means the last word of the last | |
1057 | command executed, in this case `michael'. | |
1058 | The shell performed this substitution and then echoed the command | |
1059 | as it would execute it. | |
1060 | Let us assume that we don't hear anything from michael. | |
1061 | We might do | |
1062 | .DS | |
1063 | % ps t0 | |
1064 | PID TTY TIME COMMAND | |
1065 | 4808 0 0:05 \- | |
1066 | % !! | |
1067 | ps t0 | |
1068 | PID TTY TIME COMMAND | |
1069 | 5104 0 0:00 \- 7 | |
1070 | % !where | |
1071 | where michael | |
1072 | michael is not logged in | |
1073 | % | |
1074 | .DE | |
1075 | Here we ran a | |
1076 | .I ps | |
1077 | on the teletype | |
1078 | .I michael | |
1079 | was logged in on to see that he had a shell. | |
1080 | Repeating this command via the history substitution `!!' | |
1081 | we saw that he had logged out and that only a | |
1082 | .I getty | |
1083 | process was running on his terminal. | |
1084 | Repeating the | |
1085 | .I where | |
1086 | command | |
1087 | showed that he was indeed gone, most likely having | |
1088 | hung up the phone in order to be able to call. | |
1089 | .PP | |
1090 | This illustrates several useful features of the history mechanism. | |
1091 | The form `!!' repeats the last command execution. | |
1092 | The form `!string' repeats the last command which began with | |
1093 | a word of which `string' is a prefix. | |
1094 | Another useful command form is `\(ualhs\(uarhs' performing a substitute | |
1095 | similar to that in | |
1096 | .I ed | |
1097 | or | |
1098 | .I ex. | |
1099 | Thus after | |
1100 | .DS | |
1101 | % cat ~bill/csh/sh..c | |
1102 | /mnt/bill/csh/sh..c: No such file or directory | |
1103 | % \(ua..(ua. | |
1104 | cat ~bill/csh/sh.c | |
1105 | #include "sh.h" | |
1106 | ||
1107 | /* | |
1108 | * C Shell | |
1109 | * | |
1110 | * Bill Joy, UC Berkeley | |
1111 | * October, 1978 | |
1112 | */ | |
1113 | ||
1114 | char *pathlist[] = { SRCHP | |
1115 | % | |
1116 | .DE | |
1117 | here we used the substitution to correct a typing mistake, | |
1118 | and then rubbed the command out after we saw that we had found the | |
1119 | file that we wanted. | |
1120 | The substitution changed the two `.' characters to a single `.' | |
1121 | character. | |
1122 | .PP | |
1123 | After this command we might do | |
1124 | .DS | |
1125 | % !! \||\| lpr | |
1126 | cat ~bill/csh/sh.c \||\| lpr | |
1127 | .DE | |
1128 | to put a copy of this file on the line printer, or (immediately | |
1129 | after the | |
1130 | .I cat | |
1131 | which worked above) | |
1132 | .DS | |
1133 | % pr !$ \||\| lpr | |
1134 | pr ~bill/csh/sh.c \||\| lpr | |
1135 | % | |
1136 | .DE | |
1137 | to print a copy on the printer using | |
1138 | .I pr. | |
1139 | .PP | |
1140 | More advanced forms of the history mechanism are also possible. | |
1141 | A notion of modification on substitutions allows one to say | |
1142 | (after the first successful | |
1143 | .I cat | |
1144 | above). | |
1145 | .DS | |
1146 | % cd !$:h | |
1147 | cd ~bill/csh | |
1148 | % | |
1149 | .DE | |
1150 | The trailing `:h' on the history substitution here causes only | |
1151 | the head portion of the pathname reintroduced by the history | |
1152 | mechanism to be substituted. | |
1153 | This mechanism and related mechanisms are used less often than | |
1154 | the forms above. | |
1155 | .PP | |
1156 | A complete description of history mechanism | |
1157 | features is given in the C shell manual pages in the \s-2UNIX\s0 Programmers Manual. | |
1158 | .NH 2 | |
1159 | Aliases | |
1160 | .PP | |
1161 | The shell has an | |
1162 | .I alias | |
1163 | mechanism which can be used to make transformations on input commands. | |
1164 | This mechanism can be used to simplify the commands you type, | |
1165 | to supply default arguments to commands, | |
1166 | or to perform transformations on commands and their arguments. | |
1167 | The alias facility is similar to the macro facility of many assemblers. | |
1168 | .PP | |
1169 | Some of the features obtained by aliasing can be obtained also | |
1170 | using shell command files, but these take place in another instance | |
1171 | of the shell and cannot directly affect the current shells environment | |
1172 | and commands such as | |
1173 | .I chdir | |
1174 | which must be done in the current shell. | |
1175 | .PP | |
1176 | As an example, suppose that there is a new version of the mail program | |
1177 | on the system called `Mail' | |
1178 | you wish to use, rather than the standard mail program which is called `mail'. | |
1179 | If you place the shell command | |
1180 | .DS | |
1181 | alias mail Mail | |
1182 | .DE | |
1183 | in your | |
1184 | .I \&.login | |
1185 | file, the shell will transform an input line of the form | |
1186 | .DS | |
1187 | mail bill | |
1188 | .DE | |
1189 | into a call on `Mail'. | |
1190 | More generally, suppose we wish the command `ls' to always show | |
1191 | sizes of files, that is to always do `\-s'. | |
1192 | We can do | |
1193 | .DS | |
1194 | alias ls ls \-s | |
1195 | .DE | |
1196 | or even | |
1197 | .DS | |
1198 | alias dir ls \-s | |
1199 | .DE | |
1200 | creating a new command syntax `dir' | |
1201 | which does an `ls \-s'. | |
1202 | If we say | |
1203 | .DS | |
1204 | dir ~bill | |
1205 | .DE | |
1206 | then the shell will translate this to | |
1207 | .DS | |
1208 | ls \-s /mnt/bill | |
1209 | .DE | |
1210 | .PP | |
1211 | Thus the | |
1212 | .I alias | |
1213 | mechanism can be used to provide short names for commands, | |
1214 | to provide default arguments, | |
1215 | and to define new short commands in terms of other commands. | |
1216 | It is also possible to define aliases which contain multiple | |
1217 | commands or pipelines, showing where the arguments to the original | |
1218 | command are to be substituted using the facilities of the | |
1219 | history mechanism. | |
1220 | Thus the definition | |
1221 | .DS | |
1222 | alias cd \'cd \e!* ; ls \' | |
1223 | .DE | |
1224 | would do an | |
1225 | .I ls | |
1226 | command after each change directory | |
1227 | .I cd | |
1228 | command. | |
1229 | We enclosed the entire alias definition in `\'' characters to prevent | |
1230 | most substitutions from occuring and the character `;' from being | |
1231 | recognized as a parser metacharacter. | |
1232 | The `!' here is escaped with a `\e' to prevent it from being interpreted | |
1233 | when the alias command is typed in. | |
1234 | The `\e!*' here substitutes the entire argument list to the pre-aliasing | |
1235 | .I cd | |
1236 | command, without giving an error if there were no arguments. | |
1237 | The `;' separating commands is used here | |
1238 | to indicate that one command is to be done and then the next. | |
1239 | Similarly the definition | |
1240 | .DS | |
1241 | alias whois \'grep \e!\(ua /etc/passwd\' | |
1242 | .DE | |
1243 | defines a command which looks up its first argument in the password file. | |
1244 | .NH 2 | |
1245 | Detached commands; >> and >& redirection | |
1246 | .PP | |
1247 | There are a few more metanotations useful to the terminal user | |
1248 | which have not been introduced yet. | |
1249 | The metacharacter `&' may be placed after a command, or after a sequence | |
1250 | of commands separated by `;' or `|'. | |
1251 | This causes the shell to not wait for the commands to terminate before | |
1252 | prompting again. | |
1253 | We say that they are | |
1254 | .I detached | |
1255 | or | |
1256 | .I background | |
1257 | processes. Thus | |
1258 | .DS | |
1259 | % pr ~bill/csh/sh.c \||\| lpr & | |
1260 | 5120 | |
1261 | 5121 | |
1262 | % | |
1263 | .DE | |
1264 | Here the shell printed two numbers and came back very quickly rather | |
1265 | than waiting for the | |
1266 | .I pr | |
1267 | and | |
1268 | .I lpr | |
1269 | commands to finish. | |
1270 | These numbers are the process numbers assigned by the system to the | |
1271 | .I pr | |
1272 | and | |
1273 | .I lpr | |
1274 | commands.\(dg | |
1275 | .FS | |
1276 | \(dgRunning commands in the background like this tends to slow down | |
1277 | the system and is not a good idea if the system is overloaded. | |
1278 | When overloaded, the system will just bog down more if you run a large | |
1279 | number of processes at once. | |
1280 | .FE | |
1281 | .PP | |
1282 | Since havoc would result if a command run in the background were to | |
1283 | read from your terminal at the same time as the shell does, the | |
1284 | default standard input for a command run in the background is | |
1285 | not your terminal, but an empty file called `/dev/null'. | |
1286 | Commands run in the background are also made immune to | |
1287 | \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals which you may subsequently | |
1288 | generate at your terminal.* | |
1289 | .FS | |
1290 | *If a background command stops suddenly when you hit \s-2INTERRUPT\s0 | |
1291 | or \s-2QUIT\s0 it is likely a bug in the background program. | |
1292 | .FE | |
1293 | .PP | |
1294 | If you intend to log off the system before the command completes you | |
1295 | must run the command immune to | |
1296 | \s-2HANGUP\s0 | |
1297 | signals. | |
1298 | This is done by placing the word | |
1299 | `nohup' before each program in the command, i.e.: | |
1300 | .DS | |
1301 | nohup man csh \||\| nohup lpr & | |
1302 | .DE | |
1303 | .PP | |
1304 | In addition to the standard output, commands also have a | |
1305 | diagnostic output | |
1306 | which is normally directed to the terminal even when the standard output | |
1307 | is directed to a file or a pipe. | |
1308 | It is occasionally desirable to direct the diagnostic output along with | |
1309 | the standard output. | |
1310 | For instance if you want to redirect the output of a long running command | |
1311 | into a file and wish to have a record of any error diagnostic it produces | |
1312 | you can do | |
1313 | .DS | |
1314 | command >& file | |
1315 | .DE | |
1316 | The `>&' here tells the shell to route both the diagnostic output and the | |
1317 | standard output into `file'. | |
1318 | of the standard output. | |
1319 | Similarly you can give the command | |
1320 | .DS | |
1321 | command |\|& lpr | |
1322 | .DE | |
1323 | to route both standard and diagnostic output through the pipe | |
1324 | to the line printer daemon | |
1325 | .I lpr.# | |
1326 | .FS | |
1327 | #A command form | |
1328 | .DS | |
1329 | command >&! file | |
1330 | .DE | |
1331 | exists, and is used when | |
1332 | .I noclobber | |
1333 | is set and | |
1334 | .I file | |
1335 | already exists. | |
1336 | .FE | |
1337 | .PP | |
1338 | Finally, it is possible to use the form | |
1339 | .DS | |
1340 | command >> file | |
1341 | .DE | |
1342 | to place output at the end of an existing file.\(dg | |
1343 | .FS | |
1344 | \(dgIf | |
1345 | .I noclobber | |
1346 | is set, then an error will result if | |
1347 | .I file | |
1348 | does not exist, otherwise the shell will create | |
1349 | .I file | |
1350 | if it doesn't exist. | |
1351 | A form | |
1352 | .DS | |
1353 | command >>! file | |
1354 | .DE | |
1355 | makes it not be an error for file to not exist when | |
1356 | .I noclobber | |
1357 | is set. | |
1358 | .FE | |
1359 | .NH 2 | |
1360 | Useful built-in commands | |
1361 | .PP | |
1362 | We now give a few of the useful built-in commands of the shell describing | |
1363 | how they are used. | |
1364 | .PP | |
1365 | The | |
1366 | .I alias | |
1367 | command described above is used to assign new aliases and to show the | |
1368 | existing aliases. | |
1369 | With no arguments it prints the current aliases. | |
1370 | It may also be given an argument such as | |
1371 | .DS | |
1372 | alias ls | |
1373 | .DE | |
1374 | to show the current alias for, e.g., `ls'. | |
1375 | .PP | |
1376 | The | |
1377 | .I cd | |
1378 | and | |
1379 | .I chdir | |
1380 | commands are equivalent, and change the working directory of the shell. | |
1381 | It is useful to make a directory for each project you wish to work on | |
1382 | and to place all files related to that project in that directory. | |
1383 | Thus after you login you can do | |
1384 | .DS | |
1385 | % pwd | |
1386 | /mnt/bill | |
1387 | % mkdir newpaper | |
1388 | % chdir newpaper | |
1389 | % pwd | |
1390 | /mnt/bill/newpaper | |
1391 | % | |
1392 | .DE | |
1393 | after which you will be in the directory | |
1394 | .I newpaper. | |
1395 | You can place a group of related files there. | |
1396 | You can return to your `home' login directory by doing just | |
1397 | .DS | |
1398 | chdir | |
1399 | .DE | |
1400 | with no arguments. | |
1401 | We used the | |
1402 | .I pwd | |
1403 | print working directory command to show the name of the current | |
1404 | directory here. | |
1405 | The current directory will usually be a subdirectory of your | |
1406 | home directory, and have it (here `/mnt/bill') at the start of it. | |
1407 | .PP | |
1408 | The | |
1409 | .I echo | |
1410 | command prints its arguments. | |
1411 | It is often used in shell scripts or as an interactive command | |
1412 | to see what filename expansions will yield. | |
1413 | .PP | |
1414 | The | |
1415 | .I history | |
1416 | command will show the contents of the history list. | |
1417 | The numbers given with the history events can be used to reference | |
1418 | previous events which are difficult to reference using the | |
1419 | contextual mechanisms introduced above. | |
1420 | There is also a shell variable called | |
1421 | .I prompt. | |
1422 | By placing a `!' character in its value the shell will there substitute | |
1423 | the index of the current command in the history list. | |
1424 | You can use this number to refer to this command in a history substitution. | |
1425 | Thus you could | |
1426 | .DS | |
1427 | set prompt=\'\e! % \' | |
1428 | .DE | |
1429 | Note that the `!' character had to be escaped here even within `\'' characters. | |
1430 | .PP | |
1431 | The | |
1432 | .I logout | |
1433 | command can be used to terminate a login shell which has | |
1434 | .I ignoreeof | |
1435 | set. | |
1436 | .PP | |
1437 | The | |
1438 | .I repeat | |
1439 | command can be used to repeat a command several times. | |
1440 | Thus to make 5 copies of the file | |
1441 | .I one | |
1442 | in the file | |
1443 | .I five | |
1444 | you could do | |
1445 | .DS | |
1446 | repeat 5 cat one >> five | |
1447 | .DE | |
1448 | .PP | |
1449 | The | |
1450 | .I setenv | |
1451 | command can be used, on version 7 \s-2UNIX\s0 systems, | |
1452 | to set variables in the environment. | |
1453 | Thus | |
1454 | .DS | |
1455 | setenv TERM adm3a | |
1456 | .DE | |
1457 | will set the value of the environment variable \s-2TERM\s0 | |
1458 | to | |
1459 | `adm3a'. | |
1460 | A user program | |
1461 | .I printenv | |
1462 | exists which will print out the environment. | |
1463 | It might then show: | |
1464 | .DS | |
1465 | % printenv | |
1466 | HOME /usr/bill | |
1467 | SHELL /bin/csh | |
1468 | TERM adm3a | |
1469 | % | |
1470 | .DE | |
1471 | .PP | |
1472 | The | |
1473 | .I source | |
1474 | command can be used to force the current shell to read commands from | |
1475 | a file. | |
1476 | Thus | |
1477 | .DS | |
1478 | source .cshrc | |
1479 | .DE | |
1480 | can be used after editing in a change to the | |
1481 | .I \&.cshrc | |
1482 | file which you wish to take effect before the next time you login. | |
1483 | .PP | |
1484 | The | |
1485 | .I time | |
1486 | command can be used to cause a command to be timed no matter how much | |
1487 | \s-2CPU\s0 time it takes. | |
1488 | Thus | |
1489 | .DS | |
1490 | % time cp five five.save | |
1491 | 0.0u 0.3s 0:01 26% | |
1492 | % time wc five.save | |
1493 | 1200 6300 37650 five.save | |
1494 | 1.2u 0.5s 0:03 55% | |
1495 | % | |
1496 | .DE | |
1497 | indicates that the | |
1498 | .I cp | |
1499 | command used less that 1/10th of a second of user time and only | |
1500 | 3/10th of a second of system time in copying the file `five' to | |
1501 | `five.save'. | |
1502 | The command word count | |
1503 | `wc' | |
1504 | on the other hand used 1.2 seconds of user time and 0.5 seconds of system | |
1505 | time in 3 seconds of elapsed time in counting the number of words, | |
1506 | character and lines in `five.save'. | |
1507 | The percentage `55%' indicates that over this period of 3 seconds, | |
1508 | our command `wc' used an average of 55 percent of the available \s-2CPU\s0 | |
1509 | cycles of the machine. | |
1510 | This is a very high percentage and indicates that the system is lightly | |
1511 | loaded. | |
1512 | .PP | |
1513 | The | |
1514 | .I unalias | |
1515 | and | |
1516 | .I unset | |
1517 | commands can be used | |
1518 | to remove aliases and variable definitions from the shell. | |
1519 | .PP | |
1520 | The | |
1521 | .I wait | |
1522 | command can be used after starting processes with `&' | |
1523 | to quickly see if they have finished. | |
1524 | If the shell responds immediately with another prompt, they have. | |
1525 | Otherwise you can wait for the shell to prompt at which point they | |
1526 | will have finished, or interrupt the shell by sending a \s-2RUB\s0 | |
1527 | or \s-2DELETE\s0 character. | |
1528 | If the shell is interrupted, it will print the names and numbers of the | |
1529 | processes it knows to be unfinished. | |
1530 | Thus: | |
1531 | .DS | |
1532 | % nroff paper \||\| lpr & | |
1533 | 2450 | |
1534 | 2451 | |
1535 | % wait | |
1536 | 2451 lpr | |
1537 | 2450 nroff | |
1538 | wait: Interrupted. | |
1539 | % | |
1540 | .DE | |
1541 | .PP | |
1542 | You can check again later by doing another | |
1543 | .I wait, | |
1544 | or see which commands are still running by doing a | |
1545 | .I ps. | |
1546 | As `time' | |
1547 | will show you, | |
1548 | .I ps | |
1549 | is fairly expensive. | |
1550 | It is thus counterproductive to run many | |
1551 | .I ps | |
1552 | commands to see how a background process is doing.\(dg | |
1553 | .FS | |
1554 | \(dgIf you do you are usurping with these | |
1555 | .I ps | |
1556 | commands the processor time the job needs to finish, thereby delaying its | |
1557 | completion! | |
1558 | .FE | |
1559 | .PP | |
1560 | If you run a background process and decide you want to stop it for whatever | |
1561 | reason you must use the | |
1562 | .I kill | |
1563 | program. | |
1564 | You must use the number of the processes you wish to kill. | |
1565 | Thus to stop the | |
1566 | .I nroff | |
1567 | in the above pipeline you would do | |
1568 | .DS | |
1569 | % kill 2450 | |
1570 | % wait | |
1571 | 2450: nroff: Terminated. | |
1572 | % | |
1573 | .DE | |
1574 | Here the shell printed a diagnostic that we terminated `nroff' only | |
1575 | after we did a | |
1576 | .I wait. | |
1577 | If we want the shell to discover the termination of all processes | |
1578 | it has created we must, in general, use | |
1579 | .I wait. | |
1580 | .NH 2 | |
1581 | What else? | |
1582 | .PP | |
1583 | This concludes the basic discussion of the shell for terminal users. | |
1584 | There are more features of the shell to be discussed here, and all | |
1585 | features of the shell are discussed in its manual pages. | |
1586 | One useful feature which is discussed later is the | |
1587 | .I foreach | |
1588 | built-in command which can be used to run the same command | |
1589 | sequence with a number of different arguments. | |
1590 | .PP | |
1591 | If you intend to use \s-2UNIX\s0 a lot you you should look through | |
1592 | the rest of this document and the shell manual pages to become familiar | |
1593 | with the other facilities which are available to you. | |
1594 | .bp | |
1595 | .NH | |
1596 | Shell control structures and command scripts | |
1597 | .NH 2 | |
1598 | Introduction | |
1599 | .PP | |
1600 | It is possible to place commands in files and to cause shells to be | |
1601 | invoked to read and execute commands from these files, | |
1602 | which are called | |
1603 | .I "shell scripts." | |
1604 | We here detail those features of the shell useful to the writers of such | |
1605 | scripts. | |
1606 | .NH 2 | |
1607 | Make | |
1608 | .PP | |
1609 | It is important to first note what shell scripts are | |
1610 | .I not | |
1611 | useful for. | |
1612 | There is a program called | |
1613 | .I make | |
1614 | which is very useful for maintaining a group of related files | |
1615 | or performing sets of operations on related files. | |
1616 | For instance a large program consisting of one or more files | |
1617 | can have its dependencies described in a | |
1618 | .I makefile | |
1619 | which contains definitions of the commands used to create these | |
1620 | different files when changes occur. | |
1621 | Definitions of the means for printing listings, cleaning up the directory | |
1622 | in which the files reside, and installing the resultant programs | |
1623 | are easily, and most appropriately placed in this | |
1624 | .I makefile. | |
1625 | This format is superior and preferable to maintaining a group of shell | |
1626 | procedures to maintain these files. | |
1627 | .PP | |
1628 | Similarly when working on a document a | |
1629 | .I makefile | |
1630 | may be created which defines how different versions of the document | |
1631 | are to be created and which options of | |
1632 | .I nroff | |
1633 | or | |
1634 | .I troff | |
1635 | are appropriate. | |
1636 | .NH 2 | |
1637 | Invocation and the argv variable | |
1638 | .PP | |
1639 | A | |
1640 | .I csh | |
1641 | command script may be interpreted by saying | |
1642 | .DS | |
1643 | % csh script ... | |
1644 | .DE | |
1645 | where | |
1646 | .I script | |
1647 | is the name of the file containing a group of | |
1648 | .I csh | |
1649 | commands and | |
1650 | `\&...' is replaced by a sequence of arguments. | |
1651 | The shell places these arguments in the variable | |
1652 | .I argv | |
1653 | and then begins to read commands from the script. | |
1654 | These parameters are then available through the same mechanisms | |
1655 | which are used to reference any other shell variables. | |
1656 | .PP | |
1657 | If you make the file | |
1658 | `script' | |
1659 | executable by doing | |
1660 | .DS | |
1661 | chmod 755 script | |
1662 | .DE | |
1663 | and place a shell comment at the beginning of the shell script | |
1664 | (i.e. begin the file with a `#' character) | |
1665 | then a `/bin/csh' will automatically be invoked to execute `script' when | |
1666 | you type | |
1667 | .DS | |
1668 | script | |
1669 | .DE | |
1670 | If the file does not begin with a `#' then the standard shell | |
1671 | `/bin/sh' will be used to execute it. | |
1672 | This allows you to convert your older shell scripts to use | |
1673 | .I csh | |
1674 | at your convenience. | |
1675 | .NH 2 | |
1676 | Variable substitution | |
1677 | .PP | |
1678 | After each input line is broken into words and history substitutions | |
1679 | are done on it, the input line is parsed into distinct commands. | |
1680 | Before each command is executed a mechanism know as | |
1681 | .I "variable substitution" | |
1682 | is done on these words. | |
1683 | Keyed by the character `$' this substitution replaces the names | |
1684 | of variables by their values. | |
1685 | Thus | |
1686 | .DS | |
1687 | echo $argv | |
1688 | .DE | |
1689 | when placed in a command script would cause the current value of the | |
1690 | variable | |
1691 | .I argv | |
1692 | to be echoed to the output of the shell script. | |
1693 | It is an error for | |
1694 | .I argv | |
1695 | to be unset at this point. | |
1696 | .PP | |
1697 | A number of notations are provided for accessing components and attributes | |
1698 | of variables. | |
1699 | The notation | |
1700 | .DS | |
1701 | $?name | |
1702 | .DE | |
1703 | expands to `1' if name is | |
1704 | .I set | |
1705 | or to `0' | |
1706 | if name is not | |
1707 | .I set. | |
1708 | It is the fundamental mechanism used for checking whether particular | |
1709 | variables have been assigned values. | |
1710 | All other forms of reference to undefined variables cause errors. | |
1711 | .PP | |
1712 | The notation | |
1713 | .DS | |
1714 | $#name | |
1715 | .DE | |
1716 | expands to the number of elements in the variable | |
1717 | .I name. | |
1718 | Thus | |
1719 | .DS | |
1720 | % set argv=(a b c) | |
1721 | % echo $?argv | |
1722 | 1 | |
1723 | % echo $#argv | |
1724 | 3 | |
1725 | % unset argv | |
1726 | % echo $?argv | |
1727 | 0 | |
1728 | % echo $argv | |
1729 | Undefined variable: argv. | |
1730 | % | |
1731 | .DE | |
1732 | .PP | |
1733 | It is also possible to access the components of a variable | |
1734 | which has several values. | |
1735 | Thus | |
1736 | .DS | |
1737 | $argv[1] | |
1738 | .DE | |
1739 | gives the first component of | |
1740 | .I argv | |
1741 | or in the example above `a'. | |
1742 | Similarly | |
1743 | .DS | |
1744 | $argv[$#argv] | |
1745 | .DE | |
1746 | would give `c', | |
1747 | and | |
1748 | .DS | |
1749 | $argv[1\-2] | |
1750 | .DE | |
1751 | Other notations useful in shell scripts are | |
1752 | .DS | |
1753 | $\fIn\fR | |
1754 | .DE | |
1755 | where | |
1756 | .I n | |
1757 | is an integer as a shorthand for | |
1758 | .DS | |
1759 | $argv[\fIn\fR\|] | |
1760 | .DE | |
1761 | the | |
1762 | .I n\|th | |
1763 | parameter and | |
1764 | .DS | |
1765 | $* | |
1766 | .DE | |
1767 | which is a shorthand for | |
1768 | .DS | |
1769 | $argv | |
1770 | .DE | |
1771 | The form | |
1772 | .DS | |
1773 | $$ | |
1774 | .DE | |
1775 | expands to the process number of the current shell. | |
1776 | Since this process number is unique in the system it can | |
1777 | be used in generation of unique temporary file names. | |
1778 | .PP | |
1779 | One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]' | |
1780 | should be noted here. | |
1781 | The form | |
1782 | `$argv[\fIn\fR\|]' | |
1783 | will yield an error if | |
1784 | .I n | |
1785 | is not in the range | |
1786 | `1\-$#argv' | |
1787 | while `$n' | |
1788 | will never yield an out of range subscript error. | |
1789 | This is for compatibility with the way older shells handled parameters. | |
1790 | .PP | |
1791 | Another important point is that it is never an error to give a subrange | |
1792 | of the form `n\-'; if there are less than | |
1793 | .I n | |
1794 | components of the given variable then no words are substituted. | |
1795 | A range of the form `m\-n' likewise returns an empty vector without giving | |
1796 | an error when \fIm\fR exceeds the number of elements of the given variable, | |
1797 | provided the subscript \fIn\fR is in range. | |
1798 | .NH 2 | |
1799 | Expressions | |
1800 | .PP | |
1801 | In order for interesting shell scripts to be constructed it | |
1802 | must be possible to evaluate expressions in the shell based on the | |
1803 | values of variables. | |
1804 | In fact, all the arithmetic operations of the language C are available | |
1805 | in the shell | |
1806 | with the same precedence that they have in C. | |
1807 | In particular, the operations `==' and `!=' compare strings | |
1808 | and the operators `&&' and `|\|\||' implement the boolean and/or operations. | |
1809 | .PP | |
1810 | The shell also allows file enquiries of the form | |
1811 | .DS | |
1812 | \-? filename | |
1813 | .DE | |
1814 | where `?' is replace by a number of single characters. | |
1815 | For instance the expression primitive | |
1816 | .DS | |
1817 | \-e filename | |
1818 | .DE | |
1819 | tell whether the file | |
1820 | `filename' | |
1821 | exists. | |
1822 | Other primitives test for read, write and execute access to the file, | |
1823 | whether it is a directory, or has non-zero length. | |
1824 | .PP | |
1825 | It is possible to test whether a command terminates normally, | |
1826 | by a primitive of the | |
1827 | form `{ command }' which returns true, i.e. `1' if the command | |
1828 | succeeds exiting normally with exit status 0, or `0' if the command | |
1829 | terminates abnormally or with exit status non-zero. | |
1830 | If more detailed information about the execution status of a command | |
1831 | is required, it can be executed and the variable `$status' examined | |
1832 | in the next command. | |
1833 | Since `$status' is set by every command, it is very transient. | |
1834 | It can be saved if it is inconvenient to use it only in the single | |
1835 | immediately following command. | |
1836 | .PP | |
1837 | For a full list of expression components available see the manual | |
1838 | section for the shell. | |
1839 | .NH 2 | |
1840 | Sample shell script | |
1841 | .PP | |
1842 | A sample shell script which makes use of the expression mechanism | |
1843 | of the shell and some of its control structure follows: | |
1844 | .DS | |
1845 | % cat copyc | |
1846 | # | |
1847 | # Copyc copies those C programs in the specified list | |
1848 | # to the directory ~/backup if they differ from the files | |
1849 | # already in ~/backup | |
1850 | # | |
1851 | set noglob | |
1852 | foreach i ($argv) | |
1853 | ||
1854 | if ($i:r.c != $i) continue # not a .c file so do nothing | |
1855 | ||
1856 | if (! \-r ~/backup/$i:t) then | |
1857 | echo $i:t not in backup... not cp\e\'ed | |
1858 | continue | |
1859 | endif | |
1860 | ||
1861 | cmp \-s $i ~/backup/$i:t # to set $status | |
1862 | ||
1863 | if ($status != 0) then | |
1864 | echo new backup of $i | |
1865 | cp $i ~/backup/$i:t | |
1866 | endif | |
1867 | end | |
1868 | .DE | |
1869 | .PP | |
1870 | This script makes use of the | |
1871 | .I foreach | |
1872 | command, which causes the shell to execute the commands between the | |
1873 | .I foreach | |
1874 | and the matching | |
1875 | .I end | |
1876 | for each of the values given between `(' and `)' with the named | |
1877 | variable, in this case `i' set to successive values in the list. | |
1878 | Within this loop we may use the command | |
1879 | .I break | |
1880 | to stop executing the loop | |
1881 | and | |
1882 | .I continue | |
1883 | to prematurely terminate one iteration | |
1884 | and begin the next. | |
1885 | After the | |
1886 | .I foreach | |
1887 | loop the iteration variable | |
1888 | (\fIi\fR in this case) | |
1889 | has the value at the last iteration. | |
1890 | .PP | |
1891 | We set the variable | |
1892 | .I noglob | |
1893 | here to prevent filename expansion of the members of | |
1894 | .I argv. | |
1895 | This is a good idea, in general, if the arguments to a shell script | |
1896 | are filenames which have already been expanded or if the arguments | |
1897 | may contain filename expansion metacharacters. | |
1898 | It is also possible to quote each use of a `$' variable expansion, | |
1899 | but this is harder and less reliable. | |
1900 | .PP | |
1901 | The other control construct used here is a statement of the form | |
1902 | .DS | |
1903 | \fBif\fR ( expression ) \fBthen\fR | |
1904 | command | |
1905 | ... | |
1906 | \fBendif\fR | |
1907 | .DE | |
1908 | The placement of the keywords here is | |
1909 | .B not | |
1910 | flexible due to the current implementation of the shell.\(dg | |
1911 | .FS | |
1912 | \(dgThe following two formats are not currently acceptable to the shell: | |
1913 | .DS | |
1914 | \fBif\fR ( expression ) # \fBWon't work!\fR | |
1915 | \fBthen\fR | |
1916 | command | |
1917 | ... | |
1918 | \fBendif\fR | |
1919 | .DE | |
1920 | and | |
1921 | .DS | |
1922 | \fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR | |
1923 | .DE | |
1924 | .FE | |
1925 | .PP | |
1926 | The shell does have another form of the if statement of the form | |
1927 | .DS | |
1928 | \fBif\fR ( expression ) \fBcommand\fR | |
1929 | .DE | |
1930 | which can be written | |
1931 | .DS | |
1932 | \fBif\fR ( expression ) \e | |
1933 | command | |
1934 | .DE | |
1935 | Here we have escaped the newline for the sake of appearance, | |
1936 | and the `\e' must \fBimmediately\fR. | |
1937 | The command must not involve `\||\|', `&' or `;' | |
1938 | and must not be another control command. | |
1939 | The second form requires the final `\e' to | |
1940 | .B immediately | |
1941 | precede the end-of-line. | |
1942 | .PP | |
1943 | The more general | |
1944 | .I if | |
1945 | statements above also admit a sequence of | |
1946 | .I else\-if | |
1947 | pairs followed by a single | |
1948 | .I else | |
1949 | and an | |
1950 | .I endif, | |
1951 | e.g.: | |
1952 | .DS | |
1953 | \fBif\fR ( expression ) \fBthen\fR | |
1954 | commands | |
1955 | \fBelse\fR \fBif\fR (expression ) \fBthen\fR | |
1956 | commands | |
1957 | \&... | |
1958 | ||
1959 | \fBelse\fR | |
1960 | commands | |
1961 | \fBendif\fR | |
1962 | .DE | |
1963 | .PP | |
1964 | Another important mechanism used in shell scripts is `:' modifiers. | |
1965 | We can use the modifier `:r' here to extract a root of a filename. | |
1966 | Thus if the variable | |
1967 | .I i | |
1968 | has the value | |
1969 | `foo.bar' | |
1970 | then | |
1971 | .DS | |
1972 | % echo $i $i:r | |
1973 | foo.bar foo | |
1974 | % | |
1975 | .DE | |
1976 | shows how the `:r' modifier strips off the trailing `.bar'. | |
1977 | Other modifiers will take off the last component of a pathname leaving | |
1978 | the head `:h' or all but the last component of a pathname leaving the | |
1979 | tail `:t'. | |
1980 | These modifiers are fully described in the | |
1981 | .I csh | |
1982 | manual pages in the programmers manual. | |
1983 | It is also possible to use the | |
1984 | .I "command substitution" | |
1985 | mechanism described in the next major section to perform modifications | |
1986 | on strings to then reenter the shells environment. | |
1987 | Since each usage of this mechanism involves the creation of a new process, | |
1988 | it is much more expensive to use than the `:' modification mechanism.# | |
1989 | .FS | |
1990 | #It is also important to note that | |
1991 | the current implementation of the shell limits the number of `:' modifiers | |
1992 | on a `$' substitution to 1. | |
1993 | Thus | |
1994 | .DS | |
1995 | % echo $i $i:h:t | |
1996 | /a/b/c /a/b:t | |
1997 | % | |
1998 | .DE | |
1999 | does not do what one would expect. | |
2000 | .FE | |
2001 | Finally, we note that the character `#' lexically introduces a shell | |
2002 | comment in shell scripts (but not from the terminal). | |
2003 | All subsequent characters on the input line after a `#' are discarded | |
2004 | by the shell. | |
2005 | This character can be quoted using `\'' or `\e' to place it in | |
2006 | an argument word. | |
2007 | .NH 2 | |
2008 | Other control structures | |
2009 | .PP | |
2010 | The shell also has control structures | |
2011 | .I while | |
2012 | and | |
2013 | .I switch | |
2014 | similar to those of C. | |
2015 | These take the forms | |
2016 | .DS | |
2017 | \fBwhile\fR ( expression ) | |
2018 | commands | |
2019 | \fBend\fR | |
2020 | .DE | |
2021 | and | |
2022 | .DS | |
2023 | \fBswitch\fR ( word ) | |
2024 | ||
2025 | \fBcase\fR str1: | |
2026 | commands | |
2027 | \fBbreaksw\fR | |
2028 | ||
2029 | \& ... | |
2030 | ||
2031 | \fBcase\fR strn: | |
2032 | commands | |
2033 | \fBbreaksw\fR | |
2034 | ||
2035 | \fBdefault:\fR | |
2036 | commands | |
2037 | \fBbreaksw\fR | |
2038 | ||
2039 | \fBendsw\fR | |
2040 | .DE | |
2041 | For details see the manual section for | |
2042 | .I csh. | |
2043 | C programmers should note that we use | |
2044 | .I breaksw | |
2045 | to exit from a | |
2046 | .I switch | |
2047 | while | |
2048 | .I break | |
2049 | exits a | |
2050 | .I while | |
2051 | or | |
2052 | .I foreach | |
2053 | loop. | |
2054 | A common mistake to make in | |
2055 | .I csh | |
2056 | scripts is to use | |
2057 | .I break | |
2058 | rather than | |
2059 | .I breaksw | |
2060 | in switches. | |
2061 | .PP | |
2062 | Finally, | |
2063 | .I csh | |
2064 | allows a | |
2065 | .I goto | |
2066 | statement, with labels looking like they do in C, i.e.: | |
2067 | .DS | |
2068 | loop: | |
2069 | commands | |
2070 | \fBgoto\fR loop | |
2071 | .DE | |
2072 | .NH 2 | |
2073 | Supplying input to commands | |
2074 | .PP | |
2075 | Commands run from shell scripts receive by default the standard | |
2076 | input of the shell which is running the script. | |
2077 | This it is different from previous shells running | |
2078 | under \s-2UNIX\s0. It allowing shell scripts to fully participate | |
2079 | in pipelines, but mandates extra notation for commands which are to take | |
2080 | inline data. | |
2081 | .PP | |
2082 | Thus we need a metanotation for supplying inline data to commands in | |
2083 | shell scripts. | |
2084 | As an example, consider this script which runs the editor to | |
2085 | delete leading blanks from the lines in each argument file | |
2086 | .DS | |
2087 | % cat deblank | |
2088 | # deblank \-\- remove leading blanks | |
2089 | foreach i ($argv) | |
2090 | ed \- $i << \'EOF\' | |
2091 | 1,$s/\(ua[ ]*// | |
2092 | w | |
2093 | q | |
2094 | \&\'EOF\' | |
2095 | end | |
2096 | % | |
2097 | .DE | |
2098 | The notation `<< \'EOF\'' | |
2099 | means that the standard input for the | |
2100 | .I ed | |
2101 | command is to come from the text in the shell script file | |
2102 | up to the next line consisting of exactly `\'EOF\''. | |
2103 | The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted, | |
2104 | causes the shell to not perform variable substitution on the | |
2105 | intervening lines. | |
2106 | In general, if any part of the word following the `<<' which the | |
2107 | shell uses to terminate the text to be given to the command is quoted | |
2108 | then these substitutions will not be performed. | |
2109 | In this case since we used the form `1,$' in our editor script | |
2110 | we needed to insure that this `$' was not variable substituted. | |
2111 | We could also have insured this by preceding the `$' here with a `\e', | |
2112 | i.e.: | |
2113 | .DS | |
2114 | 1,\e$s/\(ua[ ]*// | |
2115 | .DE | |
2116 | but quoting the `EOF' terminator is a more reliable way of achieving the | |
2117 | same thing. | |
2118 | .NH 2 | |
2119 | Catching interrupts | |
2120 | .PP | |
2121 | If our shell script creates temporary files, we may wish to catch | |
2122 | interruptions of the shell script so that we can clean up | |
2123 | these files. | |
2124 | We can then do | |
2125 | .DS | |
2126 | onintr label | |
2127 | .DE | |
2128 | where | |
2129 | .I label | |
2130 | is a label in our program. | |
2131 | If an interrupt is received the shell will do a | |
2132 | `goto label' | |
2133 | and we can remove the temporary files and then do a | |
2134 | .I exit | |
2135 | command (which is built in to the shell) | |
2136 | to exit from the shell script. | |
2137 | If we wish to exit with a non-zero status we can do | |
2138 | .DS | |
2139 | exit(1) | |
2140 | .DE | |
2141 | e.g. to exit with status `1'. | |
2142 | .NH 2 | |
2143 | What else? | |
2144 | .PP | |
2145 | There are other features of the shell useful to writers of shell | |
2146 | procedures. | |
2147 | The | |
2148 | .I verbose | |
2149 | and | |
2150 | .I echo | |
2151 | options and the related | |
2152 | .I \-v | |
2153 | and | |
2154 | .I \-x | |
2155 | command line options can be used to help trace the actions of the shell. | |
2156 | The | |
2157 | .I \-n | |
2158 | option causes the shell only to read commands and not to execute | |
2159 | them and may sometimes be of use. | |
2160 | .PP | |
2161 | One other thing to note is that | |
2162 | .I csh | |
2163 | will not execute shell scripts which do not begin with the | |
2164 | character `#', that is shell scripts that do not begin with a comment. | |
2165 | Similarly, the `/bin/sh' on your system may well defer to `csh' | |
2166 | to interpret shell scripts which begin with `#'. | |
2167 | This allows shell scripts for both shells to live in harmony. | |
2168 | .PP | |
2169 | There is also another quotation mechanism using `"' which allows | |
2170 | only some of the expansion mechanisms we have so far discussed to occur | |
2171 | on the quoted string and serves to make this string into a single word | |
2172 | as `\'' does. | |
2173 | .bp | |
2174 | .NH 1 | |
2175 | Miscellaneous, less generally useful, shell mechanisms | |
2176 | .NH 2 | |
2177 | Loops at the terminal; variables as vectors | |
2178 | .PP | |
2179 | It is occasionally useful to use the | |
2180 | .I foreach | |
2181 | control structure at the terminal to aid in performing a number | |
2182 | of similar commands. | |
2183 | For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0 | |
2184 | system at Cory Hall, | |
2185 | `/bin/sh', | |
2186 | `/bin/nsh', | |
2187 | and | |
2188 | `/bin/csh'. | |
2189 | To count the number of persons using each shell one could issue the commands | |
2190 | .DS | |
2191 | % grep \-c csh$ /etc/passwd | |
2192 | 27 | |
2193 | % grep \-c nsh$ /etc/passwd | |
2194 | 128 | |
2195 | % grep \-c \-v sh$ /etc/passwd | |
2196 | 430 | |
2197 | % | |
2198 | .DE | |
2199 | Since these commands are very similar we can use | |
2200 | .I foreach | |
2201 | to do this more easily. | |
2202 | .DS | |
2203 | % foreach i (\'sh$\' \'csh$\' \'\-v sh$\') | |
2204 | ? grep \-c $i /etc/passwd | |
2205 | ? end | |
2206 | 27 | |
2207 | 128 | |
2208 | 430 | |
2209 | % | |
2210 | .DE | |
2211 | Note here that the shell prompts for | |
2212 | input with `? ' when reading the body of the loop. | |
2213 | .PP | |
2214 | Very useful with loops are variables which contain lists of filenames | |
2215 | or other words. | |
2216 | You can, for example, do | |
2217 | .DS | |
2218 | % set a=(`ls`) | |
2219 | % echo $a | |
2220 | csh.n csh.rm | |
2221 | % ls | |
2222 | csh.n | |
2223 | csh.rm | |
2224 | % echo $#a | |
2225 | 2 | |
2226 | % | |
2227 | .DE | |
2228 | The | |
2229 | .I set | |
2230 | command here gave the variable | |
2231 | .I a | |
2232 | a list of all the filenames in the current directory as value. | |
2233 | We can then iterate over these names to perform any chosen function. | |
2234 | .PP | |
2235 | The output of a command within `\`' characters is converted by | |
2236 | the shell to a list of words. | |
2237 | You can also place the `\`' quoted string within `"' characters | |
2238 | to take each (non-empty) line as a component of the variable; | |
2239 | preventing the lines from being split into words at blanks and tabs. | |
2240 | A modifier `:x' exists which can be used later to expand each component | |
2241 | of the variable into another variable splitting it into separate words | |
2242 | at embedded blanks and tabs. | |
2243 | .NH 2 | |
2244 | Braces { ... } in argument expansion | |
2245 | .PP | |
2246 | Another form of filename expansion, alluded | |
2247 | to before involves the characters `{' and `}'. | |
2248 | These characters specify that the contained strings, separated by `,' | |
2249 | are to be consecutively substituted into the containing characters | |
2250 | and the results expanded left to right. | |
2251 | Thus | |
2252 | .DS | |
2253 | A{str1,str2,...strn}B | |
2254 | .DE | |
2255 | expands to | |
2256 | .DS | |
2257 | Astr1B Astr2B ... AstrnB | |
2258 | .DE | |
2259 | This expansion occurs before the other filename expansions, and may | |
2260 | be applied recursively (i.e. nested). | |
2261 | The results of each expanded string are sorted separately, left | |
2262 | to right order being preserved. | |
2263 | The resulting filenames are not required to exist if no other expansion | |
2264 | mechanisms are used. | |
2265 | This means that this mechanism can be used to generate arguments which are | |
2266 | not filenames, but which have common parts. | |
2267 | .PP | |
2268 | A typical use of this would be | |
2269 | .DS | |
2270 | mkdir ~/{hdrs,retrofit,csh} | |
2271 | .DE | |
2272 | to make subdirectories `hdrs', `retrofit' and `csh' | |
2273 | in your home directory. | |
2274 | This mechanism is most useful when the common prefix is longer | |
2275 | than in this example, i.e. | |
2276 | .DS | |
2277 | chown bin /usr/{bin/{ex,edit},lib/{ex1.1strings,how_ex}} | |
2278 | .DE | |
2279 | .NH 2 | |
2280 | Command substitution | |
2281 | .PP | |
2282 | A command enclosed in `\`' characters is replaced, just before | |
2283 | filenames are expanded, by the output from that command. | |
2284 | Thus it is possible to do | |
2285 | .DS | |
2286 | set pwd=\`pwd\` | |
2287 | .DE | |
2288 | to save the current directory in the variable | |
2289 | .I pwd | |
2290 | or to do | |
2291 | .DS | |
2292 | ex \`grep -l TRACE *.c\` | |
2293 | .DE | |
2294 | to run the editor | |
2295 | .I ex | |
2296 | suppling as arguments those files whose names end in `.c' | |
2297 | which have the string `TRACE' in them.* | |
2298 | .FS | |
2299 | *Command expansion also occurs in input redirected with `<<' | |
2300 | and within `"' quotations. | |
2301 | Refer to the shell manual section for full details. | |
2302 | .FE | |
2303 | .NH 2 | |
2304 | Other details not covered here | |
2305 | .PP | |
2306 | In particular circumstances it may be necessary to know the exact | |
2307 | nature and order of different substitutions performed by the shell. | |
2308 | The exact meaning of certain combinations of quotations is also | |
2309 | occasionally important. | |
2310 | These are detailed fully in its manual section. | |
2311 | .PP | |
2312 | The shell has a number of command line option flags mostly of use | |
2313 | in writing \s-2UNIX\s0 programs, | |
2314 | and debugging shell scripts. | |
2315 | See the shells manual section for a list of these options. | |
2316 | .bp | |
2317 | .SH | |
2318 | Appendix \- Special characters | |
2319 | .LP | |
2320 | The following table lists the special characters of | |
2321 | .I csh | |
2322 | and the \s-2UNIX\s0 system, giving for each the section(s) in which it | |
2323 | is discussed. | |
2324 | A number of these characters also have special meaning in expressions. | |
2325 | See the | |
2326 | .I csh | |
2327 | manual section | |
2328 | for a complete list. | |
2329 | .LP | |
2330 | Syntactic metacharacters | |
2331 | .DS | |
2332 | ; 2.4 separates commands to be executed sequentially | |
2333 | | 1.5 separates commands in a pipeline | |
2334 | ( ) 2.2,3.6 brackets expressions and variable values | |
2335 | & 2.5 follows commands to be executed without waiting for completion | |
2336 | .DE | |
2337 | .LP | |
2338 | Filename metacharacters | |
2339 | .DS | |
2340 | / 1.6 separates components of a file's pathname | |
2341 | \. 1.6 separates root parts of a file name from extensions | |
2342 | ? 1.6 expansion character matching any single character | |
2343 | * 1.6 expansion character matching any sequence of characters | |
2344 | [ ] 1.6 expansion sequence matching any single character from a set | |
2345 | ~ 1.6 used at the beginning of a filename to indicate home directories | |
2346 | { } 4.2 used to specify groups of arguments with common parts | |
2347 | .DE | |
2348 | .LP | |
2349 | Quotation metacharacters | |
2350 | .DS | |
2351 | \e 1.7 prevents meta-meaning of following single character | |
2352 | \' 1.7 prevents meta-meaning of a group of characters | |
2353 | " 4.3 like \', but allows variable and command expansion | |
2354 | .DE | |
2355 | .LP | |
2356 | Input/output metacharacters | |
2357 | .DS | |
2358 | < 1.3 indicates redirected input | |
2359 | > 1.5 indicates redirected output | |
2360 | .DE | |
2361 | .LP | |
2362 | Expansion/substitution metacharacters | |
2363 | .DS | |
2364 | $ 3.4 indicates variable substitution | |
2365 | ! 2.3 indicates history substitution | |
2366 | : 3.6 precedes substitution modifiers | |
2367 | \(ua 2.3 used in special forms of history substitution | |
2368 | \` 4.3 indicates command substitution | |
2369 | .DE | |
2370 | .LP | |
2371 | Other metacharacters | |
2372 | .DS | |
2373 | # 3.6 begins a shell comment | |
2374 | \- 1.2 prefixes option (flag) arguments to commands | |
2375 | .DE | |
2376 | .bp | |
2377 | .SH | |
2378 | Glossary | |
2379 | .PP | |
2380 | This glossary lists the most important terms introduced in the | |
2381 | introduction to the | |
2382 | shell and gives references to sections of the shell | |
2383 | document for further information about them. | |
2384 | References of the form | |
2385 | `pr (1)' | |
2386 | indicate that the command | |
2387 | .I pr | |
2388 | is in the \s-2UNIX\s0 programmers manual in section 1. | |
2389 | You can get an online copy of its manual page by doing | |
2390 | .DS | |
2391 | man 1 pr | |
2392 | .DE | |
2393 | References of the form (2.5) | |
2394 | indicate that more information can be found in section 2.5 of this | |
2395 | manual. | |
2396 | .IP \&\fB.\fR 15n | |
2397 | Your current directory has the name `.' as well as the name printed | |
2398 | by the command | |
2399 | .I pwd. | |
2400 | The current directory `.' is usually the first component of the search | |
2401 | path contained in the variable | |
2402 | .I path, | |
2403 | thus commands which are in `.' are found first (2.2). | |
2404 | The character `.' is also used in separating components of filenames | |
2405 | (1.6). | |
2406 | The character `.' at the beginning of a component of a pathname is | |
2407 | treated specially and not matched by the filename expansion | |
2408 | metacharacters `?', `*', and `[' `]' pairs (1.6). | |
2409 | .IP \&\fB..\fR | |
2410 | Each directory has a file `..' in it which is a reference to its | |
2411 | .I parent | |
2412 | directory. | |
2413 | After changing into the directory with | |
2414 | .I chdir, | |
2415 | i.e. | |
2416 | .DS | |
2417 | chdir paper | |
2418 | .DE | |
2419 | you can return to the parent directory by doing | |
2420 | .DS | |
2421 | chdir .. | |
2422 | .DE | |
2423 | The current directory is printed by | |
2424 | .I pwd | |
2425 | (2.6). | |
2426 | .IP alias | |
2427 | An | |
2428 | .I alias | |
2429 | specifies a shorter or different name for a \s-2UNIX\s0 | |
2430 | command, or a transformation on a command to be performed in | |
2431 | the shell. | |
2432 | The shell has a | |
2433 | command | |
2434 | .I alias | |
2435 | which establishes aliases and can print their current values. | |
2436 | The command | |
2437 | .I unalias | |
2438 | is used to remove aliases (2.6). | |
2439 | .IP argument | |
2440 | Commands in \s-2UNIX\s0 receive a list of argument words. | |
2441 | Thus the command | |
2442 | .DS | |
2443 | echo a b c | |
2444 | .DE | |
2445 | consists of a command name `echo' and three argument words `a', `b' and `c' (1.1). | |
2446 | .IP argv | |
2447 | The list of arguments to a command written in the shell language | |
2448 | (a shell script or shell procedure) is stored in a variable called | |
2449 | .I argv | |
2450 | within the shell. | |
2451 | This name is taken from the conventional name in the | |
2452 | C programming language (3.4). | |
2453 | .IP background | |
2454 | Commands started without waiting for them to complete are called | |
2455 | .I background | |
2456 | commands (1.5). | |
2457 | .IP bin | |
2458 | A directory containing binaries of programs and shell scripts to be | |
2459 | executed is typically called a `bin' directory. | |
2460 | The standard system `bin' directories are `/bin' containing the most | |
2461 | heavily used commands and `/usr/bin' which contains most other user | |
2462 | programs. | |
2463 | Other binaries are contained in directories such as `/usr/new' where | |
2464 | new programs are placed. | |
2465 | You can place binaries in any directory. | |
2466 | If you wish to execute them often, the name of the directories | |
2467 | should be a component of the variable | |
2468 | .I path. | |
2469 | .IP break | |
2470 | .I Break | |
2471 | is a built-in command used to exit from loops within the control | |
2472 | structure of the shell (3.6). | |
2473 | .IP builtin | |
2474 | A command executed directly by the shell is called a | |
2475 | .I builtin | |
2476 | command. | |
2477 | Most commands in \s-2UNIX\s0 are not built into the shell, | |
2478 | but rather exist as files in `bin' directories. | |
2479 | These commands are accessible because the directories in which | |
2480 | they reside are named in the | |
2481 | .I path | |
2482 | variable. | |
2483 | .IP case | |
2484 | A | |
2485 | .I case | |
2486 | command is used as a label in a | |
2487 | .I switch | |
2488 | statement in the shells control structure, similar to that of the | |
2489 | language C. | |
2490 | Details are given in the shells documentation `csh (NEW)' (3.7). | |
2491 | .IP cat | |
2492 | The | |
2493 | .I cat | |
2494 | program catenates a list of specified files on the standard output. | |
2495 | It is usually used to look at the contents of a single file on the terminal, | |
2496 | to `cat a file' (1.8, 2.3). | |
2497 | .IP cd | |
2498 | The | |
2499 | .I cd | |
2500 | command is used to change the working directory. | |
2501 | With no arguments, | |
2502 | .I cd | |
2503 | changes your working directory to be your | |
2504 | .I home | |
2505 | directory (2.3) (2.6). | |
2506 | .IP chdir | |
2507 | The | |
2508 | .I chdir | |
2509 | command is a synonym for | |
2510 | .I cd. | |
2511 | .I Cd | |
2512 | is usually used because it is easier to type. | |
2513 | .IP chsh | |
2514 | The | |
2515 | .I chsh | |
2516 | command is used to change the shell which you use on \s-2UNIX\s0. | |
2517 | By default, you use an older `standard' version of the shell | |
2518 | which resides in `/bin/sh'. | |
2519 | You can change your shell to `/bin/csh' by doing | |
2520 | .DS | |
2521 | chsh your-login-name /bin/csh | |
2522 | .DE | |
2523 | Thus I would do | |
2524 | .DS | |
2525 | chsh bill /bin/csh | |
2526 | .DE | |
2527 | It is only necessary to do this once. | |
2528 | The next time you log in to \s-2UNIX\s0 after doing this command, | |
2529 | you will be using | |
2530 | .I csh | |
2531 | rather than the shell in `/bin/sh' (1.9). | |
2532 | .IP cmp | |
2533 | .I Cmp | |
2534 | is a program which compares files. | |
2535 | It is usually used on binary files, or to see if two files are identical (3.6). | |
2536 | For comparing text files the program | |
2537 | .I diff, | |
2538 | described in `diff (1)' is used. | |
2539 | .IP command | |
2540 | A function performed by the system, either by the shell | |
2541 | (a builtin command) or by a program residing in a file in | |
2542 | a directory within the \s-2UNIX\s0 system is called a | |
2543 | .I command | |
2544 | (1.1). | |
2545 | .IP "command substitution" | |
2546 | .br | |
2547 | The replacement of a command enclosed in `\`' characters | |
2548 | by the text output by that command | |
2549 | is called | |
2550 | .I "command substitution" | |
2551 | (3.6, 4.3). | |
2552 | .IP component | |
2553 | A part of a | |
2554 | .I pathname | |
2555 | between `/' characters is called a | |
2556 | .I component | |
2557 | of that pathname. | |
2558 | A | |
2559 | .I variable | |
2560 | which has multiple strings as value is said to have | |
2561 | several | |
2562 | .I components, | |
2563 | each string is a | |
2564 | .I component | |
2565 | of the variable. | |
2566 | .IP continue | |
2567 | A builtin command which causes execution of the enclosing | |
2568 | .I foreach | |
2569 | or | |
2570 | .I while | |
2571 | loop to cycle prematurely. | |
2572 | Similar to the | |
2573 | .I continue | |
2574 | command in the programming language C (3.6). | |
2575 | .IP "core dump" | |
2576 | When a program terminates abnormally, the system places an image | |
2577 | of its current state in a file named `core'. | |
2578 | This `core dump' can be examined with the system debuggers `db (1)' | |
2579 | and `cdb (1)' in order to determine what went wrong with the program (1.8). | |
2580 | If the shell produces a message of the form: | |
2581 | .DS | |
2582 | commandname: Illegal instruction \-\- Core dumped | |
2583 | .DE | |
2584 | (where `Illegal instruction' is only one of several possible | |
2585 | messages) you should report this to the author of the program | |
2586 | and save the `core' file. | |
2587 | If this was a system program you should report this with the | |
2588 | .I trouble | |
2589 | command `trouble (1)'. | |
2590 | .IP cp | |
2591 | The | |
2592 | .I cp | |
2593 | (copy) program is used to copy the contents of one file into another | |
2594 | file. | |
2595 | It is one of the most commonly used \s-2UNIX\s0 commands (2.6). | |
2596 | .IP \&.cshrc | |
2597 | The file | |
2598 | .I \&.cshrc | |
2599 | in your | |
2600 | .I home | |
2601 | directory is read by each shell as it begins execution. | |
2602 | It is usually used to change the setting of the variable | |
2603 | .I path | |
2604 | and to set | |
2605 | .I alias | |
2606 | parameters which are to take effect globally (2.1). | |
2607 | .IP date | |
2608 | The | |
2609 | .I date | |
2610 | command prints the current date and time (1.3). | |
2611 | .IP debugging | |
2612 | .I Debugging | |
2613 | is the process of correcting mistakes in programs and shell scripts. | |
2614 | The shell has several options and variables which may be used | |
2615 | to aid in shell debugging (4.4). | |
2616 | .IP default | |
2617 | The label | |
2618 | .I default: | |
2619 | is used within shell | |
2620 | .I switch | |
2621 | statements, as it is in the C language | |
2622 | to label the code to be executed if none of the | |
2623 | .I case | |
2624 | labels matches the value switched on (3.7). | |
2625 | .IP \s-2DELETE\s0 | |
2626 | The | |
2627 | \s-2DELETE\s0 | |
2628 | or | |
2629 | \s-2RUBOUT\s0 | |
2630 | key on the terminal is used to generate an | |
2631 | \s-2INTERRUPT\s0 | |
2632 | signal in \s0UNIX\s0 | |
2633 | which stops the execution of most programs (2.6). | |
2634 | .IP detached | |
2635 | A command run without waiting for it to complete is said to be detached | |
2636 | (2.5). | |
2637 | .IP diagnostic | |
2638 | An error message produced by a program is often referred to as a | |
2639 | .I diagnostic. | |
2640 | Most error messages are not written to the standard output, | |
2641 | since that is often directed away from the terminal (1.3, 1.5). | |
2642 | Error messsages are instead written to the | |
2643 | .I "diagnostic output" | |
2644 | which may be directed away from the terminal, but usually is not. | |
2645 | Thus diagnostics will usually appear on the terminal (2.5). | |
2646 | .IP directory | |
2647 | A structure which contains files. | |
2648 | At any time you are in one particular directory whose | |
2649 | names can be printed by the command `pwd'. | |
2650 | The | |
2651 | .I chdir | |
2652 | command will change you to another directory, and make the files | |
2653 | in that directory visible. | |
2654 | The directory in which you are when you first login is your | |
2655 | .I home | |
2656 | directory (1.1, 1.6). | |
2657 | .IP echo | |
2658 | The | |
2659 | .I echo | |
2660 | command prints its arguments (1.6, 2.6, 3.6, 3.10). | |
2661 | .IP else | |
2662 | The | |
2663 | .I else | |
2664 | command is part of the `if-then-else-endif' control | |
2665 | command construct (3.6). | |
2666 | .IP \s-2EOF\s0 | |
2667 | An | |
2668 | .I "end-of-file" | |
2669 | is generated by the terminal by a control-d, | |
2670 | and whenever a command reads to the end of a file which | |
2671 | it has been given as input. | |
2672 | Commands receiving input from a | |
2673 | .I pipe | |
2674 | receive an end-of-file when the command sending them | |
2675 | input completes. | |
2676 | Most commands terminate when they receive an end-of-file. | |
2677 | The shell has an option to ignore end-of-file from a terminal | |
2678 | input which may help you keep from logging out accidentally | |
2679 | by typing too many control-d's (1.1, 1.8, 3.8). | |
2680 | .IP escape | |
2681 | A character \e used to prevent the special meaning of a metacharacter | |
2682 | is said to | |
2683 | .I escape | |
2684 | the character from its special meaning. | |
2685 | Thus | |
2686 | .DS | |
2687 | echo \e* | |
2688 | .DE | |
2689 | will echo the character `*' while just | |
2690 | .DS | |
2691 | echo * | |
2692 | .DE | |
2693 | will echo the names of the file in the current directory. | |
2694 | In this example, \e | |
2695 | .I escapes | |
2696 | `*' (1.7). | |
2697 | There is also a non-printing character called | |
2698 | .I escape, | |
2699 | usually labelled | |
2700 | \s-2ESC\s0 | |
2701 | or | |
2702 | \s-2ALTMODE\s0 | |
2703 | on terminal keyboards. | |
2704 | Some \s-2UNIX\s0 systems use this character to indicate that | |
2705 | output is to be suspended. | |
2706 | Other systems use control-s. | |
2707 | .IP /etc/passwd | |
2708 | This file contains information about the accounts currently on the | |
2709 | system. | |
2710 | If consists of a line for each account with fields separated by | |
2711 | `:' characters (2.3). | |
2712 | You can look at this file by saying | |
2713 | .DS | |
2714 | cat /etc/passwd | |
2715 | .DE | |
2716 | The command | |
2717 | .I grep | |
2718 | is often used to search for information in this file. | |
2719 | See `passwd (5)' and `grep (1)' for more details. | |
2720 | .IP exit | |
2721 | The | |
2722 | .I exit | |
2723 | command is used to force termination of a shell script, | |
2724 | and is built into the shell (3.9). | |
2725 | .IP "exit status" | |
2726 | A command which discovers a problem may reflect this back to the command | |
2727 | (such as a shell) which invoked (executed) it. | |
2728 | It does this by returning a non-zero number as its | |
2729 | .I "exit status," | |
2730 | a status of zero being considered | |
2731 | `normal termination'. | |
2732 | The | |
2733 | .I exit | |
2734 | command can be used to force a shell command script to give a non-zero | |
2735 | exit status (3.5). | |
2736 | .IP expansion | |
2737 | The replacement of strings in the shell input which contain metacharacters | |
2738 | by other strings is referred to as the process of | |
2739 | .I expansion. | |
2740 | Thus the replacement of the word `*' by a sorted list of files | |
2741 | in the current directory is a `filename expansion'. | |
2742 | Similarly the replacement of the characters `!!' by the text of | |
2743 | the last command is a `history expansion'. | |
2744 | Expansions are also referred to as | |
2745 | .I substitutions | |
2746 | (1.6, 3.4, 4.2). | |
2747 | .IP expressions | |
2748 | Expressions are used in the shell | |
2749 | to control the conditional structures used in the writing of shell | |
2750 | scripts and in calculating values for these scripts. | |
2751 | The operators available in shell expressions are those of the language | |
2752 | C (3.5). | |
2753 | .IP extension | |
2754 | Filenames often consist of a | |
2755 | .I root | |
2756 | name and an | |
2757 | .I extension | |
2758 | separated by the character `.'. | |
2759 | By convention, groups of related files often share the same root name. | |
2760 | Thus if `prog.c' were a C program, then the object file for this | |
2761 | program would be stored in `prog.o'. | |
2762 | Similarly a paper written with the | |
2763 | `\-me' | |
2764 | nroff macro package might be stored in | |
2765 | `paper.me' | |
2766 | while a formatted version of this paper might be kept in | |
2767 | `paper.out' and a list of spelling errors in | |
2768 | `paper.errs' (1.6). | |
2769 | .IP filename | |
2770 | Each file in \s-2UNIX\s0 has a name consisting of up to 14 characters | |
2771 | and not including the character `/' which is used in | |
2772 | .I pathname | |
2773 | building. | |
2774 | Most file names do not begin with the character `.', and contain | |
2775 | only letters and digits with perhaps a `.' separating the root | |
2776 | portion of the filename from an extension (1.6). | |
2777 | .IP "filename expansion" | |
2778 | .br | |
2779 | Filename expansion uses the metacharacters `*', `?' and `[' and `]' | |
2780 | to provide a convenient mechanism for naming files. | |
2781 | Using filename expansion it is easy to name all the files in | |
2782 | the current directory, or all files which have a common root name. | |
2783 | Other filename expansion mechanisms use the metacharacter `~' and allow | |
2784 | files in other users directories to be named easily (1.6, 4.2). | |
2785 | .IP flag | |
2786 | Many \s-2UNIX\s0 commands accept arguments which are not the names | |
2787 | of files or other users but are used to modify the action of the commands. | |
2788 | These are referred to as | |
2789 | .I flag | |
2790 | options, and by convention consists of one or more letters preceded by | |
2791 | the character `\-' (1.2). | |
2792 | Thus the | |
2793 | .I ls | |
2794 | list file commands has an option | |
2795 | `\-s' to list the sizes of files. | |
2796 | This is specified | |
2797 | .DS | |
2798 | ls \-s | |
2799 | .DE | |
2800 | .IP foreach | |
2801 | The | |
2802 | .I foreach | |
2803 | command is used in shell scripts and at the terminal to specify | |
2804 | repitition of a sequence of commands while the value of a certain | |
2805 | shell variable ranges through a specified list (3.6, 4.1). | |
2806 | .IP getty | |
2807 | The | |
2808 | .I getty | |
2809 | program is part of the system which determines the speed at which | |
2810 | your terminal is to run when you first log in. | |
2811 | It types the initial system banner and `login:'. | |
2812 | When no one is logged in on a terminal a | |
2813 | .I ps | |
2814 | command shows a command of the form | |
2815 | `- 7' | |
2816 | where `7' here is often some other single letter or digit. | |
2817 | This `7' is an option to the | |
2818 | .I getty | |
2819 | command, indicating the type of port which it is running on. | |
2820 | If you see a | |
2821 | .I getty | |
2822 | command running on a terminal in the output of | |
2823 | .I ps | |
2824 | you know that no one is logged in on that terminal (2.3). | |
2825 | .IP goto | |
2826 | The shell has a command | |
2827 | .I goto | |
2828 | used in shell scripts to transfer control to a given label (3.7). | |
2829 | .IP grep | |
2830 | The | |
2831 | .I grep | |
2832 | command searches through a list of argument files for a specified string. | |
2833 | Thus | |
2834 | .DS | |
2835 | grep bill /etc/passwd | |
2836 | .DE | |
2837 | will print each line in the file | |
2838 | `/etc/passwd' | |
2839 | which contains the string `bill'. | |
2840 | Actually, | |
2841 | .I grep | |
2842 | scans for | |
2843 | .I "regular expressions" | |
2844 | in the sense of the editors | |
2845 | `ed (1)' and `ex (1)' (2.3). | |
2846 | .I Grep | |
2847 | stands for | |
2848 | `globally find regular expression and print.' | |
2849 | .IP hangup | |
2850 | When you hangup a phone line, a \s-2HANGUP\s0 signal | |
2851 | is sent to all running processes on your terminal, causing | |
2852 | them to terminate execution prematurely. If you wish | |
2853 | to start commands to run after you log off a dialup you must | |
2854 | use the command | |
2855 | .I nohup | |
2856 | (2.6). | |
2857 | .IP head | |
2858 | The | |
2859 | .I head | |
2860 | command prints the first few lines of one or more files. | |
2861 | If you have a bunch of files containing text which you are wondering | |
2862 | about it is sometimes is useful to run | |
2863 | .I head | |
2864 | with these files as arguments. | |
2865 | This will usually show enough of what is in these files to let you decide | |
2866 | which you are interested in (1.5, 2.3). | |
2867 | .IP history | |
2868 | The | |
2869 | .I history | |
2870 | mechanism of the shell allows previous commands to be repeated, | |
2871 | possibly after modification to correct typing mistakes or to change | |
2872 | the meaning of the command. | |
2873 | The shell has a | |
2874 | .I "history list" | |
2875 | where these commands are kept, and a | |
2876 | .I history | |
2877 | variable which controls how large this list is (1.7, 2.6). | |
2878 | .IP "home directory" | |
2879 | Each user has a home directory, which is given in your entry | |
2880 | in the password file, | |
2881 | .I /etc/passwd. | |
2882 | This is the directory which you are placed in when you first log in. | |
2883 | The | |
2884 | .I cd | |
2885 | or | |
2886 | .I chdir | |
2887 | command with no arguments takes you back to this directory, whose | |
2888 | name is recorded in the shell variable | |
2889 | .I home. | |
2890 | You can also access the home directories of other users in forming | |
2891 | filenames using a file expansion notation and the character `~' (1.6). | |
2892 | .IP if | |
2893 | A conditional command within the shell, the | |
2894 | .I if | |
2895 | command is used in shell command scripts to make decisions | |
2896 | about what course of action to take next (3.6). | |
2897 | .IP ignoreeof | |
2898 | Normally, your shell will exit, printing | |
2899 | `logout' | |
2900 | if you type a control-d at a prompt of `% '. | |
2901 | This is the way you usually log off the system. | |
2902 | You can | |
2903 | .I set | |
2904 | the | |
2905 | .I ignoreeof | |
2906 | variable if you wish in your | |
2907 | .I \&.login | |
2908 | file and then use the command | |
2909 | .I logout | |
2910 | to logout. | |
2911 | This is useful if you sometimes accidentally type too many control-d | |
2912 | characters, logging yourself off. | |
2913 | If the system is slow, this can waste much time, as it may | |
2914 | take a long time to log in again (2.2, 2.6). | |
2915 | .IP input | |
2916 | Many commands on \s-2UNIX\s0 take information from the terminal or from | |
2917 | files which they then act on. | |
2918 | This information is called | |
2919 | .I input. | |
2920 | Commands normally read for input from their | |
2921 | .I "standard input" | |
2922 | which is, by default, the terminal. | |
2923 | This standard input can be redirected from a file using a shell metanotation | |
2924 | with the character `<'. | |
2925 | Many commands will also read from a file specified as argument. | |
2926 | Commands placed in pipelines will read from the output of the previous | |
2927 | command in the pipeline. | |
2928 | The leftmost command in a pipeline reads from the terminal if | |
2929 | you neither redirect its input nor give it a file name to use as | |
2930 | standard input. | |
2931 | Special mechanisms exist for suppling input to commands in shell | |
2932 | scripts (1.2, 1.6, 3.8). | |
2933 | .IP interrupt | |
2934 | An | |
2935 | .I interrupt | |
2936 | is a signal to a program that is generated by hitting the | |
2937 | \s-2RUBOUT\s0 or \s-2DELETE\s0 key. | |
2938 | It causes most programs to stop execution. | |
2939 | Certain programs such as the shell and the editors | |
2940 | handle an interrupt in special ways, usually by stopping what they | |
2941 | are doing and prompting for another command. | |
2942 | While the shell is executing another command and waiting for it | |
2943 | to finish, the shell does not listen to interrupts. | |
2944 | The shell often wakes up when you hit interrupt because many commands | |
2945 | die when they receive an interrupt (1.8, 2.6, 3.9). | |
2946 | .IP kill | |
2947 | A program which terminates processes run without waiting for them to | |
2948 | complete. (2.6) | |
2949 | .IP \&.login | |
2950 | The file | |
2951 | .I \&.login | |
2952 | in your | |
2953 | .I home | |
2954 | directory is read by the shell each time you log in to \s-2UNIX\s0 | |
2955 | and the commands there are executed. | |
2956 | There are a number of commands which are usefully placed here | |
2957 | especially | |
2958 | .I tset | |
2959 | commands and | |
2960 | .I set | |
2961 | commands to the shell itself (2.1). | |
2962 | .IP logout | |
2963 | The | |
2964 | .I logout | |
2965 | command causes a login shell to exit. | |
2966 | Normally, a login shell will exit when you hit control-d | |
2967 | generating an end-of-file, but if you have set | |
2968 | .I ignoreeof | |
2969 | in you | |
2970 | .I \&.login | |
2971 | file then this will not work and you must use | |
2972 | .I logout | |
2973 | to log off the \s-2UNIX\s0 system (2.2). | |
2974 | .IP \&.logout | |
2975 | When you log off of \s-2UNIX\s0 the shell will execute commands from | |
2976 | the file | |
2977 | .I \&.logout | |
2978 | in your | |
2979 | .I home | |
2980 | directory after it prints `logout'. | |
2981 | .IP lpr | |
2982 | The command | |
2983 | .I lpr | |
2984 | is the line printer daemon. | |
2985 | The standard input of | |
2986 | .I lpr | |
2987 | is spooled and printed on the \s-2UNIX\s0 line printer. | |
2988 | You can also give | |
2989 | .I lpr | |
2990 | a list of filenames as arguments to be printed. | |
2991 | It is most common to use | |
2992 | .I lpr | |
2993 | as the last component of a | |
2994 | .I pipeline | |
2995 | (2.3). | |
2996 | .IP ls | |
2997 | The | |
2998 | .I ls | |
2999 | list files command is one of the most commonly used \s-2UNIX\s0 | |
3000 | commands. | |
3001 | With no argument filenames it prints the names of the files in the | |
3002 | current directory. | |
3003 | It has a number of useful | |
3004 | .I flag | |
3005 | arguments, and can also be given the names of directories | |
3006 | as arguments, in which case it lists the names of the files in these | |
3007 | directories (1.2). | |
3008 | .IP mail | |
3009 | The | |
3010 | .I mail | |
3011 | program is used to send and receive messages from other \s-2UNIX\s0 | |
3012 | users (1.1, 2.2). | |
3013 | .IP make | |
3014 | The | |
3015 | .I make | |
3016 | command is used to maintain one or more related files and to | |
3017 | organize functions to be performed on these files. | |
3018 | In many ways | |
3019 | .I make | |
3020 | is easier to use, and more helpful | |
3021 | than | |
3022 | shell command scripts (3.2). | |
3023 | .IP makefile | |
3024 | The file containing command for | |
3025 | .I make | |
3026 | is called | |
3027 | `makefile' (3.2). | |
3028 | .IP manual | |
3029 | The `manual' often referred to is the | |
3030 | `\s-2UNIX\s0 programmers manual.' | |
3031 | It contains a number of sections and a description of each \s-2UNIX\s0 | |
3032 | program. | |
3033 | An online version of the manual is accessible through the | |
3034 | .I man | |
3035 | command. | |
3036 | Its documentation can be obtained online via | |
3037 | .DS | |
3038 | man man | |
3039 | .DE | |
3040 | .IP metacharacter | |
3041 | Many characters which are neither letters nor digits have special meaning | |
3042 | either to the shell or to \s-2UNIX\s0. | |
3043 | These characters are called | |
3044 | .I metacharacters. | |
3045 | If it is necessary to place these characters in arguments to commands | |
3046 | without them having their special meaning then they must be | |
3047 | .I quoted. | |
3048 | An example of a metacharacter is the character `>' which is used | |
3049 | to indicate placement of output into a file. | |
3050 | For the purposes of the | |
3051 | .I history | |
3052 | mechanism, | |
3053 | most unquoted metacharacters form separate words (1.4). | |
3054 | The appendix to this user's manual lists the metacharacters | |
3055 | in groups by their function. | |
3056 | .IP mkdir | |
3057 | The | |
3058 | .I mkdir | |
3059 | command is used to create a new directory (2.6). | |
3060 | .IP modifier | |
3061 | Substitutions with the history mechanism, keyed by the character `!' | |
3062 | or of variables using the metacharacter `$' are often subjected | |
3063 | to modifications, indicated by placing the character `:' after the | |
3064 | substitution and following this with the modifier itself. | |
3065 | The | |
3066 | .I "command substitution" | |
3067 | mechanism can also be used to perform modification in a similar way, | |
3068 | but this notation is less clear (3.6). | |
3069 | .IP noclobber | |
3070 | The shell has a variable | |
3071 | .I noclobber | |
3072 | which may be set in the file | |
3073 | .I \&.login | |
3074 | to prevent accidental destruction of files by the `>' output redirection | |
3075 | metasyntax of the shell (2.2, 2.5). | |
3076 | .IP nohup | |
3077 | A shell command used to allow background commands to run to completion | |
3078 | even if you log off a dialup before they complete. (2.5) | |
3079 | .IP nroff | |
3080 | The standard text formatter on \s-2UNIX\s0 is the program | |
3081 | .I nroff. | |
3082 | Using | |
3083 | .I nroff | |
3084 | and one of the available | |
3085 | .I macro | |
3086 | packages for it, it is possible to have documents automatically | |
3087 | formatted and to prepare them for phototypesetting using the | |
3088 | typesetter program | |
3089 | .I troff | |
3090 | (3.2). | |
3091 | .IP onintr | |
3092 | The | |
3093 | .I onintr | |
3094 | command is built into the shell and is used to control the action | |
3095 | of a shell command script when an interrupt signal is received (3.9). | |
3096 | .IP output | |
3097 | Many commands in \s-2UNIX\s0 result in some lines of text which are | |
3098 | called their | |
3099 | .I output. | |
3100 | This output is usually placed on what is known as the | |
3101 | .I "standard output" | |
3102 | which is normally connected to the users terminal. | |
3103 | The shell has a syntax using the metacharacter `>' for redirecting | |
3104 | the standard output of a command to a file (1.3). | |
3105 | Using the | |
3106 | .I pipe | |
3107 | mechanism and the metacharacter `|' it is also possible for | |
3108 | the standard output of one command to become the standard input of another | |
3109 | command (1.5). | |
3110 | Certain commands such as the line printer daemon | |
3111 | .I lpr | |
3112 | do not place their results on the standard output but rather in more | |
3113 | useful places such as on the line printer (2.3). | |
3114 | Similarly the | |
3115 | .I write | |
3116 | command places its output on another users terminal rather than its | |
3117 | standard output (2.3). | |
3118 | Commands also have a | |
3119 | .I "diagnostic output" | |
3120 | where they write their error messages. | |
3121 | Normally these go to the terminal even if the standard output has | |
3122 | been sent to a file or another command, but it is possible | |
3123 | to direct error diagnostics along with standard output using | |
3124 | a special metanotation (2.5). | |
3125 | .IP path | |
3126 | The shell has a variable | |
3127 | .I path | |
3128 | which gives the names of the directories in which it searches for | |
3129 | the commands which it is given. | |
3130 | It always checks first to see if the command it is given is | |
3131 | built into the shell. | |
3132 | If it is, then it need not search for the command as it can do it internally. | |
3133 | If the command is not builtin, then the shell searches for a file | |
3134 | with the name given in each of the directories in the | |
3135 | .I path | |
3136 | variable, left to right. | |
3137 | Since the normal definition of the | |
3138 | .I path | |
3139 | variable is | |
3140 | .DS | |
3141 | path (. /bin /usr/bin) | |
3142 | .DE | |
3143 | the shell normally looks in the current directory, and then in | |
3144 | the standard system directories `/bin' and `/usr/bin' for the named | |
3145 | command (2.2). | |
3146 | If the command cannot be found the shell will print an error diagnostic. | |
3147 | Scripts of shell commands will be executed using another shell to interpret | |
3148 | them if they have `execute' bits set. | |
3149 | This is normally true because a command of the form | |
3150 | .DS | |
3151 | chmod 755 script | |
3152 | .DE | |
3153 | was executed to turn these execute bits on (3.3). | |
3154 | .IP pathname | |
3155 | A list of names, separated by `/' characters forms a | |
3156 | .I pathname. | |
3157 | Each | |
3158 | .I component, | |
3159 | between successive `/' characters, names a directory | |
3160 | in which the next component file resides. | |
3161 | Pathnames which begin with the character `/' are interpreted relative | |
3162 | to the | |
3163 | .I root | |
3164 | directory in the filesystem. | |
3165 | Other pathnames are interpreted relative to the current directory | |
3166 | as reported by | |
3167 | .I pwd. | |
3168 | The last component of a pathname may name a directory, but | |
3169 | usually names a file. | |
3170 | .IP pipeline | |
3171 | A group of commands which are connected together, the standard | |
3172 | output of each connected to the standard input of the next | |
3173 | is called a | |
3174 | .I pipeline. | |
3175 | The | |
3176 | .I pipe | |
3177 | mechanism used to connect these commands is indicated by | |
3178 | the shell metacharacter `|' (1.5, 2.3). | |
3179 | .IP pr | |
3180 | The | |
3181 | .I pr | |
3182 | command is used to prepare listings of the contents of files | |
3183 | with headers giving the name of the file and the date and | |
3184 | time at which the file was last modified (2.3). | |
3185 | .IP printenv | |
3186 | The | |
3187 | .I printenv | |
3188 | command is used on version 7 \s-2UNIX\s0 systems | |
3189 | to print the current setting of variables in the environment. | |
3190 | As of this writing, only the \s-2VAX/UNIX\s0 system on the fifth | |
3191 | floor of Evans Hall is running a version 7 \s-2UNIX\s0 system. | |
3192 | The other systems are running version 6, which does not have | |
3193 | or need | |
3194 | .I printenv | |
3195 | (2.6). | |
3196 | .IP process | |
3197 | A instance of a running program is called a process (2.6). | |
3198 | The | |
3199 | numbers used by | |
3200 | .I kill | |
3201 | and printed by | |
3202 | .I wait | |
3203 | are unique numbers generated for these processes by \s-2UNIX\s0. | |
3204 | They are useful in | |
3205 | .I kill | |
3206 | commands which can be used to stop background processes. (2.6) | |
3207 | .IP program | |
3208 | Usually synonymous with | |
3209 | .I command; | |
3210 | a binary file or shell command script | |
3211 | which performs a useful function is often | |
3212 | called a program. | |
3213 | .IP "programmers manual" | |
3214 | .br | |
3215 | Also referred to as the | |
3216 | .I manual. | |
3217 | See the glossary entry for `manual'. | |
3218 | .IP prompt | |
3219 | Many programs will print a prompt on the terminal when they expect | |
3220 | input. | |
3221 | Thus the editor | |
3222 | `ex (NEW)' will print a `:' when it expects input. | |
3223 | The shell prompts for input with `% ' and occasionally with `? ' when | |
3224 | reading commands from the terminal (1.1). | |
3225 | The shell has a variable | |
3226 | .I prompt | |
3227 | which may be set to a different value to change the shells main prompt. | |
3228 | This is mostly used when debugging the shell (2.6). | |
3229 | .IP ps | |
3230 | The | |
3231 | .I ps | |
3232 | command is used to show the processes you are currently running. | |
3233 | Each process is shown with its unique process number, | |
3234 | an indication of the terminal name it is attached to, | |
3235 | and the amount of \s-2CPU\s0 time it has used so far. | |
3236 | The command is identified by printing some of the words used | |
3237 | when it was invoked (2.3, 2.6). | |
3238 | Login shells, such as the | |
3239 | .I csh | |
3240 | you get when you login are shown as `\-'. | |
3241 | .IP pwd | |
3242 | The | |
3243 | .I pwd | |
3244 | command prints the full pathname of the current (working) | |
3245 | directory. | |
3246 | .IP quit | |
3247 | The | |
3248 | .I quit | |
3249 | signal, | |
3250 | generated by a control-\e | |
3251 | is used to terminate programs which are behaving unreasonably. | |
3252 | It normally produces a core image file (1.8). | |
3253 | .IP quotation | |
3254 | The process by which metacharacters are prevented their special | |
3255 | meaning, usually by using the character `\' in pairs, or by | |
3256 | using the character `\e' is referred to as | |
3257 | .I quotation | |
3258 | (1.4). | |
3259 | .IP redirection | |
3260 | The routing of input or output from or to a file is known | |
3261 | as | |
3262 | .I redirection | |
3263 | of input or output (1.3). | |
3264 | .IP repeat | |
3265 | The | |
3266 | .I repeat | |
3267 | command iterates another command a specified number of times (2.6). | |
3268 | .IP \s-2RUBOUT\s0 | |
3269 | The \s-2RUBOUT\s0 or \s-2DELETE\s0 | |
3270 | key generates an interrupt signal which is used to stop programs | |
3271 | or to cause them to return and prompt for more input (2.6). | |
3272 | .IP script | |
3273 | Sequences of shell commands placed in a file are called shell command | |
3274 | scripts. | |
3275 | It is often possible to perform simple tasks using these scripts without | |
3276 | writing a program in a language such as C, by | |
3277 | using the shell to selectively run other programs (3.2, 3.3, 3.10). | |
3278 | .IP set | |
3279 | The builtin | |
3280 | .I set | |
3281 | command is used to assign new values to shell variables | |
3282 | and to show the values of the current variables. | |
3283 | Many shell variables have special meaning to the shell itself. | |
3284 | Thus by using the set command the behavior of the shell can be affected (2.1). | |
3285 | .IP setenv | |
3286 | On version 7 systems variables in the environment `environ (5)' | |
3287 | can be changed by using the | |
3288 | .I setenv | |
3289 | builtin command (2.6). | |
3290 | The | |
3291 | .I printenv | |
3292 | command can be used to print the value of the variables in the environment. | |
3293 | Currently, only the \s-2VAX/UNIX\s0 system on the fifth floor of Evans Hall | |
3294 | is running version 7 \s-2UNIX\s0. | |
3295 | The other systems are running version 6, where | |
3296 | .I setenv | |
3297 | is not necessary and does not exist. | |
3298 | .IP shell | |
3299 | A shell is a command language interpreter. | |
3300 | It is possible to write and run your own shell, | |
3301 | as shells are no different than any other programs as far as the | |
3302 | system is concerned. | |
3303 | This manual deals with the details of one particular shell, | |
3304 | called | |
3305 | .I csh. | |
3306 | .IP "shell script" | |
3307 | See | |
3308 | .I script | |
3309 | (3.2, 3.3, 3.10). | |
3310 | .IP sort | |
3311 | The | |
3312 | .I sort | |
3313 | program sorts a sequence of lines in ways that can be controlled | |
3314 | by argument flags (1.5). | |
3315 | .IP source | |
3316 | The | |
3317 | .I source | |
3318 | command causes the shell to read commands from a specified file. | |
3319 | It is most useful for reading files such as | |
3320 | .I \&.cshrc | |
3321 | after changing them (2.6). | |
3322 | .IP "special character" | |
3323 | .br | |
3324 | See | |
3325 | .I metacharacters | |
3326 | and the | |
3327 | appendix to this manual. | |
3328 | .IP standard | |
3329 | We refer often to the | |
3330 | .I "standard input" | |
3331 | and | |
3332 | .I "standard output" | |
3333 | of commands. | |
3334 | See | |
3335 | .I input | |
3336 | and | |
3337 | .I output | |
3338 | (1.3, 3.8). | |
3339 | .IP status | |
3340 | A command normally returns a | |
3341 | .I status | |
3342 | when it finishes. | |
3343 | By convention a | |
3344 | .I status | |
3345 | of zero indicates that the command succeeded. | |
3346 | Commands may return non-zero status to indicate that some abnormal | |
3347 | event has occurred. | |
3348 | The shell variable | |
3349 | .I status | |
3350 | is set to the status returned by the last command. | |
3351 | It is most useful in shell commmand scripts (3.5, 3.6). | |
3352 | .IP substitution | |
3353 | The shell implements a number of | |
3354 | .I substitutions | |
3355 | where sequences indicated by metacharacters are replaced by other sequences. | |
3356 | Notable examples of this are history substitution keyed by the | |
3357 | metacharacter `!' and variable substitution indicated by `$'. | |
3358 | We also refer to substitutions as | |
3359 | .I expansions | |
3360 | (3.4). | |
3361 | .IP switch | |
3362 | The | |
3363 | .I switch | |
3364 | command of the shell allows the shell | |
3365 | to select one of a number of sequences of commands based on an | |
3366 | argument string. | |
3367 | It is similar to the | |
3368 | .I switch | |
3369 | statement in the language C (3.7). | |
3370 | .IP termination | |
3371 | When a command which is being executed finished we say it undergoes | |
3372 | .I termination | |
3373 | or | |
3374 | .I terminates. | |
3375 | Commands normally terminate when they read an end-of-file | |
3376 | from their standard input. | |
3377 | It is also possible to terminate commands by sending them | |
3378 | an | |
3379 | .I interrupt | |
3380 | or | |
3381 | .I quit | |
3382 | signal (1.8). | |
3383 | The | |
3384 | .I kill | |
3385 | program terminates specified command whose numbers are given (2.6). | |
3386 | .IP then | |
3387 | The | |
3388 | .I then | |
3389 | command is part of the shells | |
3390 | `if-then-else-endif' control construct used in command scripts (3.6). | |
3391 | .IP time | |
3392 | The | |
3393 | .I time | |
3394 | command can be used to measure the amount of \s-2CPU\s0 | |
3395 | and real time consumed by a specified command (2.1, 2.6). | |
3396 | .IP troff | |
3397 | The | |
3398 | .I troff | |
3399 | program is used to typeset documents. | |
3400 | See also | |
3401 | .I nroff | |
3402 | (3.2). | |
3403 | .IP tset | |
3404 | The | |
3405 | .I tset | |
3406 | program is used to set standard erase and kill characters | |
3407 | and to tell the system what kind of terminal you are using. | |
3408 | It is often invoked in a | |
3409 | .I \&.login | |
3410 | file (2.1). | |
3411 | .IP unalias | |
3412 | The | |
3413 | .I unalias | |
3414 | command removes aliases (2.6). | |
3415 | .IP \s-2UNIX\s0 | |
3416 | \s-2UNIX\s0 is an operating system on which | |
3417 | .I csh | |
3418 | runs. | |
3419 | \s-2UNIX\s0 provides facilities which allow | |
3420 | .I csh | |
3421 | to invoke other programs such as editors and text formatters which | |
3422 | you may wish to use. | |
3423 | .IP unset | |
3424 | The | |
3425 | .I unset | |
3426 | command removes the definitions of shell variables (2.2, 2.6). | |
3427 | .IP "variable expansion" | |
3428 | .br | |
3429 | See | |
3430 | .I variables | |
3431 | and | |
3432 | .I expansion | |
3433 | (2.2, 3.4). | |
3434 | .IP variables | |
3435 | Variables in | |
3436 | .I csh | |
3437 | hold one or more strings as value. | |
3438 | The most common use of variables is in controlling the behavior | |
3439 | of the shell. | |
3440 | See | |
3441 | .I path, | |
3442 | .I noclobber, | |
3443 | and | |
3444 | .I ignoreeof | |
3445 | for examples. | |
3446 | Variables such as | |
3447 | .I argv | |
3448 | are also used in writing shell programs (shell command scripts) | |
3449 | (2.2). | |
3450 | .IP verbose | |
3451 | The | |
3452 | .I verbose | |
3453 | shell variable can be set to cause commands to be echoed | |
3454 | after they are history expanded. | |
3455 | This is often useful in debugging shell scripts. | |
3456 | The | |
3457 | .I verbose | |
3458 | variable is set by the shells | |
3459 | .I \-v | |
3460 | command line option (3.10). | |
3461 | .IP wait | |
3462 | The builtin command | |
3463 | .I wait | |
3464 | causes the shell to pause, and not prompt, | |
3465 | until all commands run in the background have terminated (2.6). | |
3466 | .IP where | |
3467 | The | |
3468 | .I where | |
3469 | command shows where the users named as arguments are logged into the | |
3470 | system (2.3). | |
3471 | .IP while | |
3472 | The | |
3473 | .I while | |
3474 | builtin control construct is used in shell command scripts (3.7). | |
3475 | .IP word | |
3476 | A sequence of characters which forms an argument to a command is called | |
3477 | a | |
3478 | .I word. | |
3479 | Many characters which are neither letters, digits, `\-', `.' or `/' | |
3480 | form words all by themselves even if they are not surrounded | |
3481 | by blanks. | |
3482 | Any sequence of character may be made into a word by surrounding it | |
3483 | with `\'' characters | |
3484 | except for the characters `\'' and `!' which require special treatment | |
3485 | (1.1, 1.6). | |
3486 | This process of placing special characters | |
3487 | in words without their special meaning is called | |
3488 | .I quoting. | |
3489 | .IP "working directory" | |
3490 | .br | |
3491 | At an given time you are in one particular directory, called | |
3492 | your working directory. | |
3493 | This directories name is printed by the | |
3494 | .I pwd | |
3495 | command and the files listed by | |
3496 | .I ls | |
3497 | are the ones in this directory. | |
3498 | You can change working directories using | |
3499 | .I chdir. | |
3500 | .IP write | |
3501 | The | |
3502 | .I write | |
3503 | command is used to communicate with other users who are logged in to | |
3504 | \s-2UNIX\s0 (2.3). |