Introducing UNIX and Linux |
Overview |
Using filtersAny command that reads data from standard input, and writes data to the standard output stream, is known as a filter. Most UNIX utilities are filters, or can be used as such. Most utilities are also simple in their functionality. Much UNIX programming involves transforming input, of a known form, to output, also of a known form. This output depends on the input in a specified manner, and often involves creating a pipeline. In order to use UNIX effectively, you must develop the skill of being able to choose utilities to pipe together. It is not always obvious why the filters that are 'standard' for UNIX have been developed, but they have been found to be very powerful 'building blocks'. We now examine three filters, all significantly more complex than those we have met before, but whose relevance will be striking. The editor Vi is an interactive tool, and is one of the few UNIX utilities that cannot be used as a filter. The commands we now look at perform tasks you might consider suitable for an editor such as Vi, but are designed so that they can work as filters. You may be wondering why, having discussed detailed syntax for
the shell, we now need to introduce further commands. After all, we
did claim that the shell was a 'full programming language'. In UNIX
there are rarely unique solutions to problems, although some
techniques are arguably 'better' than others. Most tasks have many
possible ways a UNIX programmer can solve them, but an experienced
programmer will be able to chose a solution that can be implemented
fast and efficiently. The purpose of commands such as
Whenever you write a shell script that takes more than a few
lines, stand back from the problem for a moment and ask yourself
whether some utility will do the job for you. Maybe there isn't,
but often you will be able to save yourself time and trouble by
recalling a utility you'd almost forgotten about. When you have
finished reading this book, go back to the earlier chapters and
remind yourself of the commands - such as |
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck