215 218




Linux Unleashed, Third Edition:bash





-->















Previous
Table of Contents
Next




Input Redirection
Input redirection changes the source of input for a command. When a command is entered in bash, the command is expecting some kind of input in order to do its job. Some of the simpler commands must get all of the information they need passed to them on the command line. For example, the rm command requires arguments on the command line. You must tell rm which files are to be deleted on the command line or it issues a prompt telling you to enter rm -h for help.
Other commands require more elaborate input than a simple directory name. The input for these commands can be found in a file. For example, the wc (word count) command counts the number of characters, words, and lines in the input that has been given to it. If you type wc <enter> at the command line, wc will wait for you to tell it what it should be counting and a prompt then appears on the command line asking for more information. Because the prompt is sometimes not easily identifiable, it may not be obvious what is happening. (It may even appear as though bash has died because, although everything you type shows up onscreen, nothing else appears to be happening.)
What is actually occurring is that the wc command is collecting input for itself. If you press Ctrl+D, the results of the wc command are written to the screen. If you enter the wc command with a filename as an argument as shown in the following example, wc returns the number of characters, words, and lines contained in that file:


wc test
11 2 1


Another way to pass the contents of the test file to wc is to redirect the input of the wc command from the terminal to the test file. This results in the same output. The < symbol is used by bash to mean “redirect the input to the current command from the specified file.” So, redirecting wc’s input from the terminal to the test file can be done by entering the following command:


wc < test
11 2 1


Input redirection is not used all that often because most commands that require input from a file have the option to specify a filename on the command line. There are times, however, when you come across a program that does not accept a filename as an input parameter, and yet the input that you want to give exists in a file. Whenever this situation occurs, you can use input redirection to get around the problem.

Output Redirection
Output redirection is more commonly used than input redirection. Output redirection enables you to redirect the output from a command into a file, as opposed to having the output displayed onscreen.
There are many situations in which this can be useful. For example, if the output of a command is quite large and does not fit on the screen, you may want to redirect it to a file so that you can view it later using a text editor. There also may be cases where you want to keep the output of a command to show to someone else or to print the results. Finally, output redirection is also useful if you want to use the output from one command as input for another. (There is an easier way to use the output of one command as input to a second command. This is shown in the “Pipes” section.)
Output redirection is done in much the same way as input redirection. Instead of using the < symbol, the > symbol is used.

Tip:  The best way to remember which symbol is input or output redirection is to think of the < as a funnel that is funneling input into the command (because the command receiving the input is on the left-hand side of the <) and the > as a funnel that is funneling the output from the command into a file.

As an example of output redirection, you can redirect the output of the ls command into a file named directory.out using the following command:


ls > directory.out


Pipes
Pipes (often called pipelines) are a way to string together a series of commands. This means that the output from the first command in the pipeline is used as the input to the second command in the pipeline. The output from the second command in the pipeline is used as input to the third command in the pipeline, and so on. The output from the last command in the pipeline is the output that actually displays onscreen (or is put into a file if output redirection is specified on the command line).
You can tell bash to create a pipeline by typing two or more commands separated by the vertical bar or “pipe” character, |. The following example illustrates the use of a pipeline:


cat sample.text | grep “High” | wc -l


This pipeline takes the output from the cat command (which lists the contents of a file) and sends it into the grep command. The grep command searches for each occurrence of the word “High” in its input. The grep command’s output then consists of each line in the file that contains the word “High.” This output is then sent to the wc command. The wc command with the -l option prints the number of lines contained in its input.

To show the results on a real file, suppose the contents of sample.text appeared as follows:


Things to do today:
Low: Go grocery shopping
High: Return movie
High: Clear level 3 in Alien vs. Predator
Medium: Pick up clothes from dry cleaner


The pipeline then returns the result 2, indicating that you have two things of importance to complete today:


cat sample.text | grep “High” | wc -l
2






Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
215 218
215 218
218 (2)
218 219
215 Bibliografia załącznikowa II
Dz U 2008 nr 215 poz 1366
v 04 218
214 215
212 215
215 Rozporz dzenie Rady Ministr w w sprawie sposobu i trybu zawarcia

więcej podobnych podstron