09.06.2013 Views

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>XENIX</strong> Programming csh: C Shell<br />

Starting a Loop at a Terminal<br />

It is occasionally useful to use the foreach control structure at the terminal to aid in<br />

performing a number of similar commands. For instance, if we want to know how many<br />

people have the Bourne shell (/bin/sh) as their login shell, we can give this command:<br />

% grep -c /bin/sh$ /e<strong>tc</strong>/passwd<br />

if we want to know how many people have the C shell {/bin/csh) as their login shell, we<br />

can give this command:<br />

% grep -c /bin/csh$ /e<strong>tc</strong>/passwd<br />

Since these commands are very similar, we can invoke this foreach command at the<br />

terminal to find how many have the Bourne shell and how many have the C shell:<br />

% foreach i ('/bin/sh$' '/bin/csh$')<br />

? grep -c $i /e<strong>tc</strong>/passwd<br />

? end<br />

Note: When the foreach command is entered at the terminal, the C shell prompts for<br />

input with "?" until the end com mand is given.<br />

Also useful with loops are variables that contain lists of file names or other words. For<br />

example, examine the following terminal session:<br />

% set a= (ls)<br />

% echo $a<br />

csh.n csh.rm<br />

% Is<br />

csh.n<br />

csh.rm<br />

% echo $#a<br />

2<br />

The set command here gave the variable a a list of all the file names in the current<br />

directory as its value. We can then iterate over these names to perform any chosen<br />

function.<br />

When the C shell encounters a command enclosed in accent marks (), it executes the<br />

command and takes the standard output of the command as the value of the expression<br />

formed by the delimited command.<br />

The output of a command within accent marks () is converted by the C shell to a list of<br />

words. You can also place the quoted string within double quotation marks (") to take<br />

each (nonempty) line as a component of the variable. This prevents the lines from being<br />

split into words at blanks and tabs. A modifier :x exists which can be used later to<br />

expand each component of the variable into another variable by splitting the original<br />

variable into separate words at embedded blanks and tabs.<br />

8-19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!