Java Reference
In-Depth Information
Note there is no output visible from
ls
. The error message,
ls: bogus:
No such file or directory
, has been written to the file
save.out
.
In a similar way standard input (
stdin
) can be redirected from its default
source, the keyboard.
As an example, we'll run the
sort
program. Unless you tell it otherwise,
sort
will read from
stdin
—that is, the keyboard. We type a short list of
phrases and then type a
^D
(a Control-D) which won't really echo to the screen
as we have shown but will tell Linux that it has reached the end of the input.
The lines of text are then printed back out, now sorted by the first character of
each line. (This is just the tip of the iceberg of what
sort
can do.)
$ sort
once upon a time
a small creature
came to live in
the forest.
^D
a small creature
came to live in
once upon a time
the forest.
Now let's assume that we already have our text inside a file called
story.txt
. We can use that file as input to the
sort
program by redirecting
the input with the “
<
” character. The
sort
doesn't know the difference. Our
output is the same:
$ sort < story.txt
a small creature
came to live in
once upon a time
the forest.
1.3.1.2
The output from one command can also be sent directly to the input of another
command. Such a connection is called a
pipe
. Linux command-line users also
use “pipe” as a verb, describing a sequence of commands as
piping
the output
of one command into another. Some examples:
Pipes
$ ls | wc > wc.fields
$ java MyCommand < data.file | grep -i total > out.put