6. CONCLUSIONES Y RECOMENDACIONES
6.2 Recomendaciones
3.9
Linux plumbing.
3.9.1
Standard input and standard output.
Many Linux commands get input from what is called standard input and send their output to standard output (often abbreviated asstdinandstdout). Your shell sets things up so that standard input is your keyboard, and standard output is the screen.
Here’s an example using thecatcommand. Normally,catreads data from all of the files specified by the command line, and sends this data directly tostdout. Therefore, using the command
/home/larry/papers# cat history-final masters-thesis
displays the contents of the filehistory-finalfollowed bymasters-thesis. However, if you don’t specify a filename, catreads data fromstdinand sends it back tostdout. Here’s an example:
/home/larry/papers# cat Hello there. Hello there. Bye. Bye. Ctrl-D /home/larry/papers#
Each line that you type is immediately echoed back by cat. When reading from stan- dard input, you indicate the input is “finished” by sending an EOT (end-of-text) signal, in general, generated by pressing Ctrl-D.
Here’s another example. Thesortcommand reads lines of text (again, from stdin, unless you specify one or more filenames) and sends the sorted output to stdout. Try the following. /home/larry/papers# sort bananas carrots apples Ctrl-D apples bananas
158 Linux Tutorial
carrots
/home/larry/papers#
Now we can alphabetize our shopping list... isn’t Linux useful?
3.9.2
Redirecting input and output.
Now, let’s say that you want to send the output ofsortto a file, to save our shopping list on disk. The shell lets you redirect standard output to a filename, by using the “>” symbol. Here’s how it works:
/home/larry/papers# sort > shopping-list bananas
carrots apples
Ctrl-D
/home/larry/papers#
As you can see, the result of thesortcommand isn’t displayed, but is saved to the file namedshopping-list. Let’s look at this file:
/home/larry/papers# cat shopping-list apples
bananas carrots
/home/larry/papers#
Now you can sort your shopping list, and save it, too! But let’s suppose that you are storing the unsorted, original shopping list in the fileitems. One way of sorting the information and saving it to a file would be to givesortthe name of the file to read, in lieu of standard input, and redirect standard output as we did above, as follows:
/home/larry/papers# sort items > shopping-list /home/larry/papers# cat shopping-list
apples bananas carrots
/home/larry/papers#
However, there’s another way to do this. Not only can you redirect standard output, you can redirect standard input as well, using the “<” symbol.
Linux Plumbing 159
/home/larry/papers# sort < items apples
bananas carrots
/home/larry/papers#
Technically, sort < itemsis equivalent tosort items, but lets you demonstrate the following point: sort < itemsbehaves as if the data in the fileitemswas typed to standard input. The shell handles the redirection. sortwasn’t given the name of the file (items) to read; as far assortis concerned, it still reads from standard input as if you had typed the data from your keyboard.
This introduces the concept of a filter. A filter is a program that reads data from stan- dard input, processes it in some way, and sends the processed data to standard output. Using redirection, standard input and standard output can be referenced from files. As mentioned above,stdinandstdoutdefault to the keyboard and screen respectively. sortis a simple filter. It sorts the incoming data and sends the result to standard output.catis even simpler. It doesn’t do anything with the incoming data, it simply outputs whatever is given to it.
3.9.3
Using pipes.
We already demonstrated how to usesortas a filter. However, these examples assume that you have data stored in a file somewhere or are willing to type the data from the standard input yourself. What if the data that you wanted to sort came from the output of another command, likels?
The-roption tosortsorts the data in reverse-alphabetical order. If you want to list the files in your current directory in reverse order, one way to do it is follows:
/home/larry/papers# ls english-list
history-final masters-thesis notes
Now redirect the output of thelscommand into a file calledfile-list: /home/larry/papers# ls > file-list
160 Linux Tutorial notes masters-thesis history-final english-list /home/larry/papers#
Here, you save the output oflsin a file, and then runsort -ron that file. But this is unwieldy and uses a temporary file to save the data fromls.
The solution is pipelining. This is a shell feature that connects a string of commands via a “pipe.” Thestdoutof the first command is sent to thestdinof the second command. In this case, we want to send thestdoutof lsto thestdinof sort. Use the “|” symbol to create a pipe, as follows:
/home/larry/papers# ls j sort -r notes masters-thesis history-final english-list /home/larry/papers#
This command is shorter and easier to type. Here’s another useful example, the command
/home/larry/papers# ls /usr/bin
displays a long list of files, most of which fly past the screen too quickly for you to read. So, let’s usemoreto display the list of files in/usr/bin.
/home/larry/papers# ls /usr/bin j more
Now you can page down the list of files at your leisure.
But the fun doesn’t stop here! You can pipe more than two commands together. The commandheadis a filter that displays the first lines from an input stream (in this case, input from a pipe). If you want to display the last filename in alphabetical order in the current directory, use commands like the following:
/home/larry/papers# ls j sort -r j head -1 notes
/home/larry/papers#
wherehead -1displays the first line of input that it receives (in this case, the stream of reverse-sorted data fromls).