Most UNIX commands expect input to come from a file(s), and
produce output to anther file(s). “One
of these files is the standard input, and is the place from which a program
expects to read its input. Another is
called the standard output, and it is the file to which the program writes its
results. The third file is the
diagnostic output (also called Standard error), and it is a file to which
program writes any error responses.
Generally, the standard input is taken to be the keyboard (input is
typed by the user), the standard output is the terminal screen.
Concept of Redirection
(1) Redirection Input:
There are
two possible sources of input for UNIX commands. Programs such as ls and find get their input
from the command line in the form of options and filenames. Other programs, such as cat, can get their
data from the standard input as well as from the command line. Try the cat command with no options on the
command line:
$cat
There is no
response. Because n files are specified
with the command, cat waits to get its input from your keyboard, the standard
input file. The program will accept input
lines from the keyboard until it sees a line which begins with Ctrl+D, which is
the end of file signal for standard input.
To redirect
the standard input, you use the < operator.
For example, if you wanted cat to get its input from output.txt you could
use the command.
$cat<output.txt
The
difference between this command and
$cat output.txt
is a subtle
one. In filenames provided as options to
a command, you can use filename substitution.
When redirecting input, you must use the name of an existing file or
device.
(2) Redirection Output:
Now, let’s
say that you want to send the output of cat to a file, to save our shopping
list on disk. The shell lets you redirect
standard output to a file name, by using the “>” symbol. Here’s how it works:
$cat
amg.txt>output.txt
The > operator causes a new file to be created.
If you had already created a file named output.txt, it would be deleted
and replaced with the new data. If you
wanted to add the new data to the old output, you could use the >>
operator. For example.
$cat
amg.txt>>output.txt
This will
add content of amg.txt to output.txt file both file must be exists at specified
path.
Concept of Piping
Suppose that you wanted a directory listing that was sorted
by the mode file type plus permissions.
To accomplish this, you might redirect the output from ls to a data file
and then sort that data file. For
example.
$ls –l>tempfile
$sort<tempfile
-rw-rw-r- 1 marsha adept 1024 Jan
20 14:14 Lines.dat
-rw-rw-r- 1 marsha adept 3072 Jan
20 14:14 Lines.idx
-rw-rw-r- 1 marsha adept 256 Jan
20 14:14 Pages.dat
Although
you get the result that you wanted, there are three drawback to this method:
You might
end up with a lot of temporary file in your directory. You would have to go back and remove
them. The sort program does not begins
its work until the first command is complete.
This is not too significant with the small amount of data used in this
example, but it can make a considerable difference with larger files. The final output contains the name of your
temp file, which might not be what you had in mind.
Fortunately,
there is a better way.
The pipe
symbol (|) causes the standard output of the program on the left side of the
pipe to be passed directly to the standard input of the program on the right
side of the pipe symbol. Therefore, to
get the same results as before, you can use the pipe symbol. For example
$ls –l|sort
-rw-rw-r- 1 marsha adept 1024 Jan
20 14:14 Lines.dat
-rw-rw-r- 1 marsha adept 3072 Jan
20 14:14 Lines.idx
-rw-rw-r- 1 marsha adept 256 Jan
20 14:14 Pages.dat
To connect
two or more operation within the same stream at that time pipe sign is used to
perform such a operation in unix.
0 comments:
Post a Comment