1 .\" $NetBSD: csh.3,v 1.5 2003/08/07 09:05:08 agc Exp $
3 .\" Copyright (c) 1980, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)csh.3 8.1 (Berkeley) 6/8/93
34 Shell control structures and command scripts
38 It is possible to place commands in files and to cause shells to be
39 invoked to read and execute commands from these files,
42 We here detail those features of the shell useful to the writers of such
47 It is important to first note what shell scripts are
50 There is a program called
52 which is very useful for maintaining a group of related files
53 or performing sets of operations on related files.
54 For instance a large program consisting of one or more files
55 can have its dependencies described in a
57 which contains definitions of the commands used to create these
58 different files when changes occur.
59 Definitions of the means for printing listings, cleaning up the directory
60 in which the files reside, and installing the resultant programs
61 are easily, and most appropriately placed in this
63 This format is superior and preferable to maintaining a group of shell
64 procedures to maintain these files.
66 Similarly when working on a document a
68 may be created which defines how different versions of the document
69 are to be created and which options of
75 Invocation and the argv variable
79 command script may be interpreted by saying
85 is the name of the file containing a group of
88 `\&...' is replaced by a sequence of arguments.
89 The shell places these arguments in the variable
91 and then begins to read commands from the script.
92 These parameters are then available through the same mechanisms
93 which are used to reference any other shell variables.
101 and place a shell comment at the beginning of the shell script
102 (i.e. begin the file with a `#' character)
103 then a `/bin/csh' will automatically be invoked to execute `script' when
108 If the file does not begin with a `#' then the standard shell
109 `/bin/sh' will be used to execute it.
110 This allows you to convert your older shell scripts to use
114 Variable substitution
116 After each input line is broken into words and history substitutions
117 are done on it, the input line is parsed into distinct commands.
118 Before each command is executed a mechanism know as
119 .I "variable substitution"
120 is done on these words.
121 Keyed by the character `$' this substitution replaces the names
122 of variables by their values.
127 when placed in a command script would cause the current value of the
130 to be echoed to the output of the shell script.
133 to be unset at this point.
135 A number of notations are provided for accessing components and attributes
141 expands to `1' if name is
146 It is the fundamental mechanism used for checking whether particular
147 variables have been assigned values.
148 All other forms of reference to undefined variables cause errors.
154 expands to the number of elements in the variable
167 Undefined variable: argv.
171 It is also possible to access the components of a variable
172 which has several values.
177 gives the first component of
179 or in the example above `a'.
189 would give `a b'. Other notations useful in shell scripts are
195 is an integer as a shorthand for
205 which is a shorthand for
213 expands to the process number of the current shell.
214 Since this process number is unique in the system it can
215 be used in generation of unique temporary file names.
220 is quite special and is replaced by the next line of input read from
221 the shell's standard input (not the script it is reading). This is
222 useful for writing shell scripts that are interactive, reading
223 commands from the terminal, or even writing a shell script that
224 acts as a filter, reading lines from its input file. Thus the sequence
229 would write out the prompt `yes or no?' without a newline and then
230 read the answer into the variable `a'. In this case `$#a' would be
231 `0' if either a blank line or end-of-file (^D) was typed.
233 One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'
234 should be noted here.
237 will yield an error if
242 will never yield an out of range subscript error.
243 This is for compatibility with the way older shells handled parameters.
245 Another important point is that it is never an error to give a subrange
246 of the form `n\-'; if there are less than
248 components of the given variable then no words are substituted.
249 A range of the form `m\-n' likewise returns an empty vector without giving
250 an error when \fIm\fR exceeds the number of elements of the given variable,
251 provided the subscript \fIn\fR is in range.
255 In order for interesting shell scripts to be constructed it
256 must be possible to evaluate expressions in the shell based on the
258 In fact, all the arithmetic operations of the language C are available
260 with the same precedence that they have in C.
261 In particular, the operations `==' and `!=' compare strings
262 and the operators `&&' and `|\|\||' implement the boolean and/or operations.
263 The special operators `=~' and `!~' are similar to `==' and `!=' except
264 that the string on the right side can have pattern matching characters
265 (like *, ? or []) and the test is whether the string on the left matches
266 the pattern on the right.
268 The shell also allows file enquiries of the form
272 where `?' is replace by a number of single characters.
273 For instance the expression primitive
277 tell whether the file
280 Other primitives test for read, write and execute access to the file,
281 whether it is a directory, or has non-zero length.
283 It is possible to test whether a command terminates normally,
284 by a primitive of the
285 form `{ command }' which returns true, i.e. `1' if the command
286 succeeds exiting normally with exit status 0, or `0' if the command
287 terminates abnormally or with exit status non-zero.
288 If more detailed information about the execution status of a command
289 is required, it can be executed and the variable `$status' examined
291 Since `$status' is set by every command, it is very transient.
292 It can be saved if it is inconvenient to use it only in the single
293 immediately following command.
295 For a full list of expression components available see the manual
296 section for the shell.
300 A sample shell script which makes use of the expression mechanism
301 of the shell and some of its control structure follows:
305 # Copyc copies those C programs in the specified list
306 # to the directory ~/backup if they differ from the files
307 # already in ~/backup
312 if ($i !~ *.c) continue # not a .c file so do nothing
314 if (! \-r ~/backup/$i:t) then
315 echo $i:t not in backup... not cp\e\'ed
319 cmp \-s $i ~/backup/$i:t # to set $status
321 if ($status != 0) then
322 echo new backup of $i
328 This script makes use of the
330 command, which causes the shell to execute the commands between the
334 for each of the values given between `(' and `)' with the named
335 variable, in this case `i' set to successive values in the list.
336 Within this loop we may use the command
338 to stop executing the loop
341 to prematurely terminate one iteration
345 loop the iteration variable
346 (\fIi\fR in this case)
347 has the value at the last iteration.
351 here to prevent filename expansion of the members of
353 This is a good idea, in general, if the arguments to a shell script
354 are filenames which have already been expanded or if the arguments
355 may contain filename expansion metacharacters.
356 It is also possible to quote each use of a `$' variable expansion,
357 but this is harder and less reliable.
359 The other control construct used here is a statement of the form
361 \fBif\fR ( expression ) \fBthen\fR
366 The placement of the keywords here is
368 flexible due to the current implementation of the shell.\(dg
370 \(dgThe following two formats are not currently acceptable to the shell:
374 \fBif\fR ( expression ) # \fBWon't work!\fR
386 \fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR
391 The shell does have another form of the if statement of the form
393 \fBif\fR ( expression ) \fBcommand\fR
397 \fBif\fR ( expression ) \e
400 Here we have escaped the newline for the sake of appearance.
401 The command must not involve `\||\|', `&' or `;'
402 and must not be another control command.
403 The second form requires the final `\e' to
405 precede the end-of-line.
409 statements above also admit a sequence of
411 pairs followed by a single
417 \fBif\fR ( expression ) \fBthen\fR
419 \fBelse\fR \fBif\fR (expression ) \fBthen\fR
428 Another important mechanism used in shell scripts is the `:' modifier.
429 We can use the modifier `:r' here to extract a root of a filename or
441 /mnt/foo.bar /mnt/foo bar
446 shows how the `:r' modifier strips off the trailing `.bar' and the
447 the `:e' modifier leaves only the `bar'.
448 Other modifiers will take off the last component of a pathname leaving
449 the head `:h' or all but the last component of a pathname leaving the
451 These modifiers are fully described in the
453 manual pages in the User's Reference Manual.
454 It is also possible to use the
455 .I "command substitution"
456 mechanism described in the next major section to perform modifications
457 on strings to then reenter the shell's environment.
458 Since each usage of this mechanism involves the creation of a new process,
459 it is much more expensive to use than the `:' modification mechanism.\(dd
461 \(dd It is also important to note that
462 the current implementation of the shell limits the number of `:' modifiers
463 on a `$' substitution to 1.
474 does not do what one would expect.
476 Finally, we note that the character `#' lexically introduces a shell
477 comment in shell scripts (but not from the terminal).
478 All subsequent characters on the input line after a `#' are discarded
480 This character can be quoted using `\'' or `\e' to place it in
483 Other control structures
485 The shell also has control structures
489 similar to those of C.
492 \fBwhile\fR ( expression )
498 \fBswitch\fR ( word )
516 For details see the manual section for
518 C programmers should note that we use
529 A common mistake to make in
541 statement, with labels looking like they do in C, i.e.:
548 Supplying input to commands
550 Commands run from shell scripts receive by default the standard
551 input of the shell which is running the script.
552 This is different from previous shells running
553 under \s-2UNIX\s0. It allows shell scripts to fully participate
554 in pipelines, but mandates extra notation for commands which are to take
557 Thus we need a metanotation for supplying inline data to commands in
559 As an example, consider this script which runs the editor to
560 delete leading blanks from the lines in each argument file:
563 # deblank \-\- remove leading blanks
573 The notation `<< \'EOF\''
574 means that the standard input for the
576 command is to come from the text in the shell script file
577 up to the next line consisting of exactly `\'EOF\''.
578 The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,
579 causes the shell to not perform variable substitution on the
581 In general, if any part of the word following the `<<' which the
582 shell uses to terminate the text to be given to the command is quoted
583 then these substitutions will not be performed.
584 In this case since we used the form `1,$' in our editor script
585 we needed to insure that this `$' was not variable substituted.
586 We could also have insured this by preceding the `$' here with a `\e',
591 but quoting the `EOF' terminator is a more reliable way of achieving the
596 If our shell script creates temporary files, we may wish to catch
597 interruptions of the shell script so that we can clean up
605 is a label in our program.
606 If an interrupt is received the shell will do a
608 and we can remove the temporary files and then do an
610 command (which is built in to the shell)
611 to exit from the shell script.
612 If we wish to exit with a non-zero status we can do
616 e.g. to exit with status `1'.
620 There are other features of the shell useful to writers of shell
626 options and the related
630 command line options can be used to help trace the actions of the shell.
633 option causes the shell only to read commands and not to execute
634 them and may sometimes be of use.
636 One other thing to note is that
638 will not execute shell scripts which do not begin with the
639 character `#', that is shell scripts that do not begin with a comment.
640 Similarly, the `/bin/sh' on your system may well defer to `csh'
641 to interpret shell scripts which begin with `#'.
642 This allows shell scripts for both shells to live in harmony.
644 There is also another quotation mechanism using `"' which allows
645 only some of the expansion mechanisms we have so far discussed to occur
646 on the quoted string and serves to make this string into a single word