Combining Commands Together


So far, we have only been using commands individually, and for the most part that's what you'll be doing in practice. What gives Linux its real power, though, is when users string commands together, to build on the results of one tool to get even more done. There are even commands that serve as glue to hold these more complex tool chains together.

All the commands we have looked at here print their information to the screen, but the target can be flexible. There are two ways to control where output should go: piping and redirection. Pipes connect one program's output and the next program's input. Instead of displaying results on the screen, a command will send results to another program, which manipulates the information it gains to get even better results for the user. Output redirection works in a similar way, but works best when creating new files. We'll look first at pipes, and then at redirection.

There's a Freeciv game going on, and you need to know who's in it. Let's take two of the commands we learned about earlier in this chapter at find out:

ps aux | grep freeciv 

What you did was create a list of processes live on the system right now. That list, including additional information about the processes (including the owner of each process), was sent to grep. It looked at that list and looked for lines containing the word "freeciv." grep then identified the matching lines and told you who owned the active clients, and who owned the server as well.

Earlier, we used du to develop a good list of all the files on the system. Wouldn't it be nice to have that in a file that we could then manipulate further down the road? Simple enough:

du  ahc /home/mikemc > du.txt 

If we wanted a running total of who was playing Freeciv at any given moment, we could start a cron job to run our ps | grep pipe, and then add a third command:

ps aux | grep freeciv > players.txt 

Simple, yet a pretty good illustration of just what we can do.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

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