4.5 Pipes and How They Are Used

   

Look at Figure 4-6 carefully . It shows another powerful feature of the UNIX shell, using the output of one command as input of another command. We call this process piping due to its similarity to the real-world use of a pipe. At the command line, both of the processes (commands) are connected using the vertical bar symbol " ". This symbol is often called a pipe symbol. When two commands are connected through a pipe, the first command sends its output to the pipe instead of sending it to the terminal screen. The second command reads its input from the pipe rather than from the keyboard. Both of the commands still send error messages to the terminal screen, as shown in the figure. The first command takes input from the keyboard (stdin), and the second command sends its output to the terminal screen ( stdout ). If the second command needs input data but nothing is available in the pipe, it just waits for the first command to send something into the pipe.

Figure 4-6. Use of pipes.

graphics/04fig06.gif

Pipes are often used to filter, modify, or manipulate data output of one command. Multiple levels of pipes can be used in one command line. Similarly, pipes can also be used in combination with I/O redirection symbols.

Use of Pipes as Filters

Many times we don't need all of the output produced by a command. In such a case, we can filter the desired information from the output produced by a command. Filtering means extracting useful data and throwing away the rest. We have already studied the who command, which is used to see the names of logged-in users. In large systems, where hundreds of users are logged in simultaneously , it is difficult to find out whether a particular user is currently logged in. In this situation, we use the filter to get the desired information. We can use the who command with the grep command, where grep acts as a filter. Consider the next example, where we want to find if a user " mike " is logged in. First we use only the who command and then we combine the who and grep commands.

 $  who  operator   pts/ta       Aug 30 16:05 boota      pts/tb       Aug 30 15:59 mike       pts/tc       Aug 30 15:44 linda      pts/td       Aug 30 14:34 $ 

Now we use a pipe to filter out our required information.

 $  who  grep mike  mike       pts/tc       Aug 30 15:44 $ 

As you can see, only the line containing the word " mike " is now displayed. We have used the grep command previously to find a string from one or multiple files. The grep commands, at that time, used file names as input. In this example, it did the same thing but took its input from the pipe.

How did grep know that no more data were coming from the pipe and that it should stop processing? Well, this is quite simple. The who command sends an end of file (EOF) character when it has completed sending output to the pipe. The grep command checks the EOF character and stops execution when it finds the character. In case there are no data in the pipe and the grep command has not received the EOF character, it will just wait until it gets more data or the EOF character.

As another example, we can get only login names from the who command by using another filter known as cut . We will discuss the cut command in more detail in the last chapter, but for the time being just see how we use it to extract the first word of each line and throw away the rest.

 $  who  cut -f 1 -d " "  operator boota mike linda $ 

The cut command takes its input as fields separated by space characters and picks the first field from each input line. Since the first field of all output lines is the login name , we got the login names only in the output.

You can also use multiple levels of pipes as shown below.

 $  who  cut -f 1 -d " " grep mike  mike $ 

Try to explain what is happening here. We have filtered the output of one command and then again filtered the output of the second command. You can continue this process as far as you want.

Use of Pipes for Data Manipulation

As we have used pipes for filtering data, we can also use them for reorganizing and manipulating data. What if you need to get output of a command in sorted form? Yes, it is quite simple if you pass it through the sort command using a pipe. Consider the above example of using the who command. See how the output changes without and with a sort pipe.

 $  who  operator   pts/ta       Aug 30 16:05 boota      pts/tb       Aug 30 15:59 mike       pts/tc       Aug 30 15:44 linda      pts/td       Aug 30 14:34 $ 

Now we use a pipe with the sort command.

 $  who  sort  boota      pts/tb       Aug 30 15:59 linda      pts/td       Aug 30 14:34 mike       pts/tc       Aug 30 15:44 operator   pts/ta       Aug 30 16:05 $ 

The sort command has arranged the output of the who command in alphabetical order. If there are many users logged in, the output of the who command just scrolls up and you see only the last page. In that case, you can use the more command as a filter to stop the scrolling at the end of each page.

 $  who  more  

Filters can do many things for you in a very simple way. If you were using some other operating system, you might need to write separate programs!


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net