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