modern syntax for asgops & inits cause Donn's latest ccom rejects the old.
[unix-history] / usr / src / bin / csh / USD.doc / csh.4
CommitLineData
2eb7a57f
KD
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5db1b32c 5.\" @(#)csh.4 6.1 (Berkeley) %G%
2eb7a57f
KD
6.\"
7.nr H1 3
8.NH
9Other, less commonly used, shell features
10.NH 2
11Loops at the terminal; variables as vectors
12.PP
13It is occasionally useful to use the
14.I foreach
15control structure at the terminal to aid in performing a number
16of similar commands.
17For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
18system at Cory Hall,
19`/bin/sh',
20`/bin/nsh',
21and
22`/bin/csh'.
23To count the number of persons using each shell one could have issued
24the commands
25.DS
26% grep \-c csh$ /etc/passwd
2727
28% grep \-c nsh$ /etc/passwd
29128
30% grep \-c \-v sh$ /etc/passwd
31430
32%
33.DE
34Since these commands are very similar we can use
35.I foreach
36to do this more easily.
37.DS
38% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
39? grep \-c $i /etc/passwd
40? end
4127
42128
43430
44%
45.DE
46Note here that the shell prompts for
47input with `? ' when reading the body of the loop.
48.PP
49Very useful with loops are variables which contain lists of filenames
50or other words.
51You can, for example, do
52.DS
53% set a=(\`ls\`)
54% echo $a
55csh.n csh.rm
56% ls
57csh.n
58csh.rm
59% echo $#a
602
61%
62.DE
63The
64.I set
65command here gave the variable
66.I a
67a list of all the filenames in the current directory as value.
68We can then iterate over these names to perform any chosen function.
69.PP
70The output of a command within `\`' characters is converted by
71the shell to a list of words.
72You can also place the `\`' quoted string within `"' characters
73to take each (non-empty) line as a component of the variable;
74preventing the lines from being split into words at blanks and tabs.
75A modifier `:x' exists which can be used later to expand each component
76of the variable into another variable splitting it into separate words
77at embedded blanks and tabs.
78.NH 2
79Braces { ... } in argument expansion
80.PP
81Another form of filename expansion, alluded
82to before involves the characters `{' and `}'.
83These characters specify that the contained strings, separated by `,'
84are to be consecutively substituted into the containing characters
85and the results expanded left to right.
86Thus
87.DS
88A{str1,str2,...strn}B
89.DE
90expands to
91.DS
92Astr1B Astr2B ... AstrnB
93.DE
94This expansion occurs before the other filename expansions, and may
95be applied recursively (i.e. nested).
96The results of each expanded string are sorted separately, left
97to right order being preserved.
98The resulting filenames are not required to exist if no other expansion
99mechanisms are used.
100This means that this mechanism can be used to generate arguments which are
101not filenames, but which have common parts.
102.PP
103A typical use of this would be
104.DS
105mkdir ~/{hdrs,retrofit,csh}
106.DE
107to make subdirectories `hdrs', `retrofit' and `csh'
108in your home directory.
109This mechanism is most useful when the common prefix is longer
110than in this example, i.e.
111.DS
112chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
113.DE
114.NH 2
115Command substitution
116.PP
117A command enclosed in `\`' characters is replaced, just before
118filenames are expanded, by the output from that command.
119Thus it is possible to do
120.DS
121set pwd=\`pwd\`
122.DE
123to save the current directory in the variable
124.I pwd
125or to do
126.DS
127ex \`grep \-l TRACE *.c\`
128.DE
129to run the editor
130.I ex
131supplying as arguments those files whose names end in `.c'
132which have the string `TRACE' in them.*
133.FS
134*Command expansion also occurs in input redirected with `<<'
135and within `"' quotations.
136Refer to the shell manual section for full details.
137.FE
138.NH 2
139Other details not covered here
140.PP
141In particular circumstances it may be necessary to know the exact
142nature and order of different substitutions performed by the shell.
143The exact meaning of certain combinations of quotations is also
144occasionally important.
145These are detailed fully in its manual section.
146.PP
147The shell has a number of command line option flags mostly of use
148in writing \s-2UNIX\s0 programs,
149and debugging shell scripts.
5db1b32c 150See the csh(1) manual section for a list of these options.
2eb7a57f 151.bp