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 | .\" | |
78ed81a3 | 32 | .\" from: @(#)csh.4 6.2 (Berkeley) 4/17/91 |
33 | .\" csh.4,v 1.2 1993/08/01 07:37:42 mycroft Exp | |
15637ed4 RG |
34 | .\" |
35 | .nr H1 3 | |
36 | .NH | |
37 | Other, less commonly used, shell features | |
38 | .NH 2 | |
39 | Loops at the terminal; variables as vectors | |
40 | .PP | |
41 | It is occasionally useful to use the | |
42 | .I foreach | |
43 | control structure at the terminal to aid in performing a number | |
44 | of similar commands. | |
45 | For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0 | |
46 | system at Cory Hall, | |
47 | `/bin/sh', | |
48 | `/bin/nsh', | |
49 | and | |
50 | `/bin/csh'. | |
51 | To count the number of persons using each shell one could have issued | |
52 | the commands | |
53 | .DS | |
54 | % grep \-c csh$ /etc/passwd | |
55 | 27 | |
56 | % grep \-c nsh$ /etc/passwd | |
57 | 128 | |
58 | % grep \-c \-v sh$ /etc/passwd | |
59 | 430 | |
60 | % | |
61 | .DE | |
62 | Since these commands are very similar we can use | |
63 | .I foreach | |
64 | to do this more easily. | |
65 | .DS | |
66 | % foreach i (\'sh$\' \'csh$\' \'\-v sh$\') | |
67 | ? grep \-c $i /etc/passwd | |
68 | ? end | |
69 | 27 | |
70 | 128 | |
71 | 430 | |
72 | % | |
73 | .DE | |
74 | Note here that the shell prompts for | |
75 | input with `? ' when reading the body of the loop. | |
76 | .PP | |
77 | Very useful with loops are variables which contain lists of filenames | |
78 | or other words. | |
79 | You can, for example, do | |
80 | .DS | |
81 | % set a=(\`ls\`) | |
82 | % echo $a | |
83 | csh.n csh.rm | |
84 | % ls | |
85 | csh.n | |
86 | csh.rm | |
87 | % echo $#a | |
88 | 2 | |
89 | % | |
90 | .DE | |
91 | The | |
92 | .I set | |
93 | command here gave the variable | |
94 | .I a | |
95 | a list of all the filenames in the current directory as value. | |
96 | We can then iterate over these names to perform any chosen function. | |
97 | .PP | |
98 | The output of a command within `\`' characters is converted by | |
99 | the shell to a list of words. | |
100 | You can also place the `\`' quoted string within `"' characters | |
101 | to take each (non-empty) line as a component of the variable; | |
102 | preventing the lines from being split into words at blanks and tabs. | |
103 | A modifier `:x' exists which can be used later to expand each component | |
104 | of the variable into another variable splitting it into separate words | |
105 | at embedded blanks and tabs. | |
106 | .NH 2 | |
107 | Braces { ... } in argument expansion | |
108 | .PP | |
109 | Another form of filename expansion, alluded | |
110 | to before involves the characters `{' and `}'. | |
111 | These characters specify that the contained strings, separated by `,' | |
112 | are to be consecutively substituted into the containing characters | |
113 | and the results expanded left to right. | |
114 | Thus | |
115 | .DS | |
116 | A{str1,str2,...strn}B | |
117 | .DE | |
118 | expands to | |
119 | .DS | |
120 | Astr1B Astr2B ... AstrnB | |
121 | .DE | |
122 | This expansion occurs before the other filename expansions, and may | |
123 | be applied recursively (i.e. nested). | |
124 | The results of each expanded string are sorted separately, left | |
125 | to right order being preserved. | |
126 | The resulting filenames are not required to exist if no other expansion | |
127 | mechanisms are used. | |
128 | This means that this mechanism can be used to generate arguments which are | |
129 | not filenames, but which have common parts. | |
130 | .PP | |
131 | A typical use of this would be | |
132 | .DS | |
133 | mkdir ~/{hdrs,retrofit,csh} | |
134 | .DE | |
135 | to make subdirectories `hdrs', `retrofit' and `csh' | |
136 | in your home directory. | |
137 | This mechanism is most useful when the common prefix is longer | |
138 | than in this example, i.e. | |
139 | .DS | |
140 | chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} | |
141 | .DE | |
142 | .NH 2 | |
143 | Command substitution | |
144 | .PP | |
145 | A command enclosed in `\`' characters is replaced, just before | |
146 | filenames are expanded, by the output from that command. | |
147 | Thus it is possible to do | |
148 | .DS | |
149 | set pwd=\`pwd\` | |
150 | .DE | |
151 | to save the current directory in the variable | |
152 | .I pwd | |
153 | or to do | |
154 | .DS | |
155 | ex \`grep \-l TRACE *.c\` | |
156 | .DE | |
157 | to run the editor | |
158 | .I ex | |
159 | supplying as arguments those files whose names end in `.c' | |
160 | which have the string `TRACE' in them.* | |
161 | .FS | |
162 | *Command expansion also occurs in input redirected with `<<' | |
163 | and within `"' quotations. | |
164 | Refer to the shell manual section for full details. | |
165 | .FE | |
166 | .NH 2 | |
167 | Other details not covered here | |
168 | .PP | |
169 | In particular circumstances it may be necessary to know the exact | |
170 | nature and order of different substitutions performed by the shell. | |
171 | The exact meaning of certain combinations of quotations is also | |
172 | occasionally important. | |
173 | These are detailed fully in its manual section. | |
174 | .PP | |
175 | The shell has a number of command line option flags mostly of use | |
176 | in writing \s-2UNIX\s0 programs, | |
177 | and debugging shell scripts. | |
178 | See the csh(1) manual section for a list of these options. | |
179 | .bp |