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