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