Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | .\" Copyright (c) 1980 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
4 | .\" Redistribution and use in source and binary forms, with or without | |
5 | .\" modification, are permitted provided that the following conditions | |
6 | .\" are met: | |
7 | .\" 1. Redistributions of source code must retain the above copyright | |
8 | .\" notice, this list of conditions and the following disclaimer. | |
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer in the | |
11 | .\" documentation and/or other materials provided with the distribution. | |
12 | .\" 3. All advertising materials mentioning features or use of this software | |
13 | .\" must display the following acknowledgement: | |
14 | .\" This product includes software developed by the University of | |
15 | .\" California, Berkeley and its contributors. | |
16 | .\" 4. Neither the name of the University nor the names of its contributors | |
17 | .\" may be used to endorse or promote products derived from this software | |
18 | .\" without specific prior written permission. | |
19 | .\" | |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | .\" SUCH DAMAGE. | |
31 | .\" | |
32 | .\" @(#)csh.4 6.2 (Berkeley) 4/17/91 | |
33 | .\" | |
34 | .nr H1 3 | |
35 | .NH | |
36 | Other, less commonly used, shell features | |
37 | .NH 2 | |
38 | Loops at the terminal; variables as vectors | |
39 | .PP | |
40 | It is occasionally useful to use the | |
41 | .I foreach | |
42 | control structure at the terminal to aid in performing a number | |
43 | of similar commands. | |
44 | For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0 | |
45 | system at Cory Hall, | |
46 | `/bin/sh', | |
47 | `/bin/nsh', | |
48 | and | |
49 | `/bin/csh'. | |
50 | To count the number of persons using each shell one could have issued | |
51 | the commands | |
52 | .DS | |
53 | % grep \-c csh$ /etc/passwd | |
54 | 27 | |
55 | % grep \-c nsh$ /etc/passwd | |
56 | 128 | |
57 | % grep \-c \-v sh$ /etc/passwd | |
58 | 430 | |
59 | % | |
60 | .DE | |
61 | Since these commands are very similar we can use | |
62 | .I foreach | |
63 | to do this more easily. | |
64 | .DS | |
65 | % foreach i (\'sh$\' \'csh$\' \'\-v sh$\') | |
66 | ? grep \-c $i /etc/passwd | |
67 | ? end | |
68 | 27 | |
69 | 128 | |
70 | 430 | |
71 | % | |
72 | .DE | |
73 | Note here that the shell prompts for | |
74 | input with `? ' when reading the body of the loop. | |
75 | .PP | |
76 | Very useful with loops are variables which contain lists of filenames | |
77 | or other words. | |
78 | You can, for example, do | |
79 | .DS | |
80 | % set a=(\`ls\`) | |
81 | % echo $a | |
82 | csh.n csh.rm | |
83 | % ls | |
84 | csh.n | |
85 | csh.rm | |
86 | % echo $#a | |
87 | 2 | |
88 | % | |
89 | .DE | |
90 | The | |
91 | .I set | |
92 | command here gave the variable | |
93 | .I a | |
94 | a list of all the filenames in the current directory as value. | |
95 | We can then iterate over these names to perform any chosen function. | |
96 | .PP | |
97 | The output of a command within `\`' characters is converted by | |
98 | the shell to a list of words. | |
99 | You can also place the `\`' quoted string within `"' characters | |
100 | to take each (non-empty) line as a component of the variable; | |
101 | preventing the lines from being split into words at blanks and tabs. | |
102 | A modifier `:x' exists which can be used later to expand each component | |
103 | of the variable into another variable splitting it into separate words | |
104 | at embedded blanks and tabs. | |
105 | .NH 2 | |
106 | Braces { ... } in argument expansion | |
107 | .PP | |
108 | Another form of filename expansion, alluded | |
109 | to before involves the characters `{' and `}'. | |
110 | These characters specify that the contained strings, separated by `,' | |
111 | are to be consecutively substituted into the containing characters | |
112 | and the results expanded left to right. | |
113 | Thus | |
114 | .DS | |
115 | A{str1,str2,...strn}B | |
116 | .DE | |
117 | expands to | |
118 | .DS | |
119 | Astr1B Astr2B ... AstrnB | |
120 | .DE | |
121 | This expansion occurs before the other filename expansions, and may | |
122 | be applied recursively (i.e. nested). | |
123 | The results of each expanded string are sorted separately, left | |
124 | to right order being preserved. | |
125 | The resulting filenames are not required to exist if no other expansion | |
126 | mechanisms are used. | |
127 | This means that this mechanism can be used to generate arguments which are | |
128 | not filenames, but which have common parts. | |
129 | .PP | |
130 | A typical use of this would be | |
131 | .DS | |
132 | mkdir ~/{hdrs,retrofit,csh} | |
133 | .DE | |
134 | to make subdirectories `hdrs', `retrofit' and `csh' | |
135 | in your home directory. | |
136 | This mechanism is most useful when the common prefix is longer | |
137 | than in this example, i.e. | |
138 | .DS | |
139 | chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} | |
140 | .DE | |
141 | .NH 2 | |
142 | Command substitution | |
143 | .PP | |
144 | A command enclosed in `\`' characters is replaced, just before | |
145 | filenames are expanded, by the output from that command. | |
146 | Thus it is possible to do | |
147 | .DS | |
148 | set pwd=\`pwd\` | |
149 | .DE | |
150 | to save the current directory in the variable | |
151 | .I pwd | |
152 | or to do | |
153 | .DS | |
154 | ex \`grep \-l TRACE *.c\` | |
155 | .DE | |
156 | to run the editor | |
157 | .I ex | |
158 | supplying as arguments those files whose names end in `.c' | |
159 | which have the string `TRACE' in them.* | |
160 | .FS | |
161 | *Command expansion also occurs in input redirected with `<<' | |
162 | and within `"' quotations. | |
163 | Refer to the shell manual section for full details. | |
164 | .FE | |
165 | .NH 2 | |
166 | Other details not covered here | |
167 | .PP | |
168 | In particular circumstances it may be necessary to know the exact | |
169 | nature and order of different substitutions performed by the shell. | |
170 | The exact meaning of certain combinations of quotations is also | |
171 | occasionally important. | |
172 | These are detailed fully in its manual section. | |
173 | .PP | |
174 | The shell has a number of command line option flags mostly of use | |
175 | in writing \s-2UNIX\s0 programs, | |
176 | and debugging shell scripts. | |
177 | See the csh(1) manual section for a list of these options. | |
178 | .bp |