Page 410 - DCAP103_Principle of operating system
P. 410
Unit 14: Case Study of Linux Operating System
(for reading), a file called standard output (for writing normal output), and a file called standard Notes
error (for writing error messages). Normally, all three default to the terminal, so that reads from
standard input come from the keyboard and writes to standard output or standard error go to
the screen. Many Linux programs read from standard input and write to standard output as the
default. For example, sort invokes the sort program, which reads lines from the terminal (until
the user types a CTRL-D, to indicate end of file), sorts them alphabetically, and writes the result
to the screen. It is also possible to redirect standard input and output, as is often useful. The
syntax for redirecting standard input uses a less than sign (<) followed by the input file name.
Similarly, standard output is redirected using a greater than sign (>). It is permitted to redirect
both in the same command. For example, the command sort <in >out causes sort to take its input
from the file in and write its output to the file out. Since standard error has not been redirected,
any error messages go to the screen. A program that reads its input from standard input, does
some processing on it, and writes its output to standard output is called a filter. Consider the
following command line consisting of three separate commands:
sort <in >temp; head –30 <temp; rm temp
It first runs sort, taking the input from in and writing the output to temp. When that has been
completed, the shell runs head, telling it to print the first 30 lines of temp and print them on
standard output, which defaults to the terminal. Finally, the temporary file is removed. It
frequently occurs that the first program in a command line produces output that is used as the
input on the next program. In the above example, we used the file temp to hold this output.
However, Linux provides a simpler construction to do the same thing.
In
sort <in | head –30
the vertical bar, called the pipe symbol, says to take the output from sort and use it as the input
to head, eliminating the need for creating, using, and removing the temporary file. A collection
of commands connected by pipe symbols, called a pipeline, may contain arbitrarily many
commands. A four-component pipeline is shown by the following example:
grep ter *.t | sort | head –20 | tail –5 >foo
Here all the lines containing the string ‘’ter’’ in all the files ending in .t are written to standard
output, where they are sorted. The first 20 of these are selected out by head which passes then
to tail, which writes the last five (i.e., lines 16 to 20 in the sorted list) to foo. This is an example
of how Linux provides basic building blocks (numerous filters), each of which does one job,
along with a mechanism for them to be put together in almost limitless ways.
Linux is a general-purpose multiprogramming system. A single user can run several programs
at once, each as a separate process. The shell syntax for running a process in the background is
to follow its command with an ampersand. Thus wc -l <a >b & runs the word count program,
wc, to count the number of lines (-l flag) in its input, a, writing the result to b, but does it in
the background. As soon as the command has been typed, the shell types the prompt and is
ready to accept and handle the next command. Pipelines can also be put in the background, for
example, by sort <x | head & Multiple pipelines can run in the background simultaneously.
It is possible to put a list of shell commands in a file and then start a shell with this file as
standard input. The (second) shell just processes them in order, the same as it would with
commands typed on the keyboard. Files containing shell commands are called shell scripts.
Shell scripts may assign values to shell variables and then read them later. They may also have
parameters, and use if, for, while, and case constructs. Thus a shell script is really a program
LOVELY PROFESSIONAL UNIVERSITY 403