Reading Input From a File or From the Terminal
Redirecting input is similar to redirecting output. To redirect
input from a file, use the less-than symbol (<).
The example here uses the sort command
which simply sorts the input in order.
$ more
required_packages.txt
glibc-2.3.4-2.41
glibc-common-2.3.4-2.41
libaio-0.3.105-2
libgcc-3.4.6-10
libstdc++-3.4.6-10
make-3.80-7.EL4
pdksh-5.2.14-30.6
elfutils-libelf-devel-0.97.1-5.i386.rpm
glibc-headers-2.3.4-2.41.i386.rpm
libaio-devel-0.3.105-2.i386.rpm
sysstat-5.0.5-19.el4.i386.rpm
unixODBC-devel-2.2.11-1.RHEL4.1.i386.rpm
$ sort < required_packages.txt
elfutils-libelf-devel-0.97.1-5.i386.rpm
glibc-2.3.4-2.41
glibc-common-2.3.4-2.41
glibc-headers-2.3.4-2.41.i386.rpm
libaio-0.3.105-2
libaio-devel-0.3.105-2.i386.rpm
libgcc-3.4.6-10
libstdc++-3.4.6-10
make-3.80-7.EL4
pdksh-5.2.14-30.6
sysstat-5.0.5-19.el4.i386.rpm
unixODBC-devel-2.2.11-1.RHEL4.1.i386.rpm
The shell reads the contents of the required_packages.txt file
into the standard input of the sort command which then orders the
text and displays it on standard output.
Another way that input can be redirected is to move it from
standard input using file markers and the double less-than
symbols. Though less frequently used at the command line, this is
a useful technique for scripting.
$ sort <<
EOF
> charlie
> delta
> bravo
> alpha
> EOF
alpha
bravo
charlie
delta
After the <<, a file marker is named, in this case EOF. Any name
can be used, but EOF is often used to stand for End Of File. When
the shell sees this, it continues to read from the standard input
until it encounters a matching file marker alone on a line. Once it
sees that, it will take the input given and pass it to the standard
in for the given program.
Redirecting from One Command to Another
Using the tools that were just covered, you could easily send the
output from one command to a file to be manipulated by another
command; but in most cases, the middleman can be cut out and streams
redirected straight from one command to another.
The pipe (|) allows connecting the standard output from one
command to the standard input of the next. This practice is called
pipelining. Say, for example, that only the lines in the
required_packages.txt file that contain the string lib need to be
seen, but they should be seen in alphabetical order. Use grepto display only the lines that contain the string lib,
then pipe the output of grep directly into the input of the sort
command.
$ grep lib
required_packages.txt | sort
elfutils-libelf-devel-0.97.1-5.i386.rpm
glibc-2.3.4-2.41
glibc-common-2.3.4-2.41
glibc-headers-2.3.4-2.41.i386.rpm
libaio-0.3.105-2
libaio-devel-0.3.105-2.i386.rpm
libgcc-3.4.6-10
libstdc++-3.4.6-10
Like other methods that have more than one command on a line,
this set of commands is interpreted from left to right. The output
is also fed from left to right, from the standard output of the grep
command into the standard input of the sort command, which is then
displayed on the screen.
Redirectionand pipelining can
be confusing, especially when more than one method is used in a
single command. If there is trouble in identifying what is going
on, simply work from left to right and interpret one symbol at a
time.
|
|
|
Get the Complete Details on
Linux System Management for Oracle DBAs
The landmark book
"Linux for the Oracle DBA: The
Definitive Reference"
provides comprehensive yet specific
knowledge on administering Oracle on Linux. A
must-have reference for every DBA running or planning to run
Oracle on a Linux platform.
Buy it
for 30% off directly from the publisher.
|
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|