21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

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.

18.2 Unix 261<br />

tells us how many lines the file file contains. (The majority of Unix commands offer<br />

a choice of parameters to modify their output). Putting these two tools together, the<br />

Unix command:<br />

ls | wc -l<br />

pipes the output from the filter ls into the input <strong>for</strong> the filter wc. The final output is<br />

there<strong>for</strong>e the number of files in the current directory.<br />

SELF-TEST QUESTION<br />

18.1 The command:<br />

grep "Alice" < file<br />

outputs those lines in the file file that contain the string Alice. Write<br />

a pipelined command to count how many lines in the file contain the<br />

string Alice.<br />

The vertical bar symbol in a Unix command signifies that the output stream from<br />

one filter is to be directed not to its default file, but to the input of another filter.<br />

We have seen that Unix provides a useful but limited number of facilities as filters,<br />

plus a facility to combine filters. Thus when some new software is required, there are<br />

three options:<br />

1. use an existing filter<br />

2. combine the existing filters using pipes<br />

3. write a new filter and combine it with existing filters using pipes.<br />

Combining filters is rather like writing programs, but at a higher level – the Unix<br />

filters are the language primitives, and pipes provide the mechanism <strong>for</strong> combining<br />

them to produce more powerful facilities. The command language describes the<br />

interactions.<br />

Filters tend to be short and simple – 90 of the standard Unix filters (other than<br />

compilers) are less than 1,200 lines (about 20 pages) of high-level programming<br />

language statements. An individual filter is usually written in the programming language<br />

C, which is the core language of Unix. A filter reads from an input pipe and<br />

outputs to an output pipe. It opens an input pipe just as if it was opening a file,<br />

then it reads data serially from the pipe (or file) until the end of data (file) is<br />

encountered. The filter sends its output to an output stream by writing data as if<br />

to a serial file.<br />

All the Unix tools are designed to do a specific single task well, rather than supporting<br />

many, optional features. However, any options are specified using parameters.

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

Saved successfully!

Ooh no, something went wrong!