Introducing UNIX and Linux |
Processes and devicesOverview |
DevicesA device is any piece of equipment connected to a computer system which performs communication between 'the outside world' and the system, such as a printer or a terminal. Although normally hardware, a device might be software that behaves, from the perspective of UNIX, in the same way as hardware. When you have run commands that use input and output streams, their behaviour as 'streams of characters' does not depend upon where they originate or are directed to. They are simply streams of characters. It does not matter whether input comes from a terminal or from a file, nor whether output is piped to a printer, sent to the terminal, or redirected to a file. In fact, UNIX treats devices exactly the same as files. From the perspective of a UNIX programmer, every device is a file. Type
This looks very much like an ordinary file, except that the
first character in the output of
and the date will appear on your screen just as if you had typed
Every device has a filename in the directory
If you attempt to write to a device owned by another user, it
won't work. If you have several windows on your terminal you will
normally be allowed to write to other windows - use
would produce the same output as above. Standard input is received from, and standard output and
standard error are sent to, the file that is your terminal, unless
you redirect them elsewhere. They are not files, they are simply
concepts to enable redirection of streams to take place. At the
ends of pipelines, unless these streams are redirected, they are
automatically directed at
is equivalent to
Note that you can overload a device and direct input and/or more than one output at it Other devices you may encounter include
but this would be wasteful of filespace. You can discard this
stream by redirecting it to a file (device) known as
This file behaves in the same way as any other file or device,
but it simply junks any output sent to it, and if you try to read
from it it is always at end-of-file. Use Worked exampleWrite a script to read in the name of a file and display a
message only if it cannot be read.
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck