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