-->
Page 96
a job number of 1 to the mail program. The sc spreadsheet was then suspended, assigned job number 2, and returned to the mail program by specifying its job number with the fg %1 command.
If you run and suspend many jobs in the background, you may not remember a program by its job number, or remember which programs are suspended. You can get a list of the suspended programs by using the bash shell's jobs command:
# jobs [1] Stopped (signal) pine [2]- Stopped sc [3]+ Stopped emacs-nox # fg %sc ... sc program is running ...
This shows that there are three jobs, the pine mailer, the sc spreadsheet, and the emacs editor, currently suspended in the shell. You should also note that instead of restarting the sc spreadsheet job by referring by its job number, the program was brought back to the foreground by using the fg % command with the name of the suspended job.
You also can stop programs using this same approach. Instead of using the ps command to find a program's process number, then issuing a kill command, you can use the kill with the % operator:
# kill %1 [1]- Stopped (signal) pine # kill %emacs-nox [3]+ Stopped emacs-nox
Here you see how to use the kill command to stop a program by its number or name. This is much easier than using the ps command, especially if there are a large number of programs or other processes running in the background.
Using your shell's job control facilities is a powerful way to work efficiently with multiple programs. All of the shells included on your CD-ROM include job control in one form or another. In the next section, you'll be shown another powerful way to use your shell to run multiple programs on a single command line.
You've already seen how to redirect the output of a program into a file, and how to then redirect the contents of that file into another program. But you can do this all at once, without the use of a temporary file, by using the |, or vertical bar character, called a pipe. Using pipes to string commands together on the command line is a quick and powerful way to enhance the power of individual commands, and represents a unique strength of Linux
Page 97
and other versions of UNIX.
You'll definitely use piped commands as you begin to learn how to use Linux. Not only do pipes save you time, but you'll use different combinations of piped commands to tackle computing tasks particular to the way you work and the programs you run. At first your pipe commands may be simple, but as you gain confidence and understanding, you'll be able to construct fairly complex pipelines.
Pipes work well under Linux because many commands are also filters, which accept your shell's standard input and send output to the shell's standard output. Pipes may be used in nearly any computing task, and can be used to quickly find information, generate reports, transform data, or view results. First look at four simple examples.
# ls | lpr # printenv | fgrep EDITOR # nroff -man mymanpage.1 | less # cat document.txt | wc | mail -s "Document.txt Report" bball
The first command line pipes a listing of the current directory through the line printer command to print a report. The second example searches through a listing of your current shell environment and prints the value of the default text editor. The third example prints the formatted output of a manual page to your display so you can browse the document to check for errors. The last example pipes a text document through the word count command, wc, and then electronically mails a report on the number of characters, words, and lines in the document to the user bball.
You'll use pipes to confront and solve everyday problems not usually solved by individual programs. For example, if you have a lot of documents on your system, but can't remember which document contains a certain phrase, you can find this information quickly. Instead of running your word processor and opening each file, you could try typing the following:
# find /home -name *.doc | xargs fgrep administration | less
This command line uses the find command to search the /home directory for all files ending in .doc, then pipes the names into the xargs command. The xargs command then runs the fgrep command to search for the word administration in each file, with the results piped through the less pager. You also can use pipes to not only find information, but to process data, and create new files.
# find *.doc | xargs cat | tr ` ` `\n' | sort | uniq | tee dict | less
The preceding command line builds a file called dict that contains a sorted list of unique words contained in all your word processing files. What do you call this type of file? A dictionary! Although to be honest, not all the dictionary words may be spelled correctly. This command works by piping each found file through the tr command, which translates each character space into a carriage return to break the stream into one word per line. The
Page 98
stream of lines is then sorted, and the uniq command removes all occurrences of similar lines except for one. You'll notice that the tee command has also been used to save the output of the stream to a file.
JUST A MINUTE |
The zsh shell contains some improvements on input and output redirection, so you may not need the tee command if you're using pipes on the zsh shell command line. See the zsh shell documentation for details. |
The tee command is used to save the results of a pipe at a particular juncture. This is handy when you want to test your results when building pipes, or save results in a complex pipe. Look at the following example:
# xwd -out wd.xwd # xwdtopnm < wd.xwd | ppmtogif | tee wd.gif | giftopnm | tee wd.pnm | pnmtotiff Â>wd.tif
In this example, a window dump graphic has been created using the X11 xwd command, which captures and saves the contents of an X window or desktop. Then a single command line has been used to first convert this graphic into a portable bitmap graphic. The output of the xwdtopnm command is then fed into the ppmtogif command. The tee command is used to save the output, in GIF format, to a file, and then convert the file back into the portable bitmap format. This output is then saved as a portable bitmap graphic file, and also fed into the pnmtotiff command, which saves the file as a TIF graphic. One command line and four graphic conversions!
Using pipes with Linux is an easy way to get work done. As you continue to work with Linux, you'll soon develop your own set of favorite command lines. Once you've developed your favorites, you'll then want to build your own shell commands, which you'll read about in the next section.
You don't have to be a programmer to write commands for Linux. After you become familiar with different programs and find yourself typing the same command lines over and over, save them into text files and turn them into commands. In the simplest form, a shell command may simply be one or several command lines you frequently use. Look at the following example
rxvt -geometry 80x11+803+375 -bg white -fg black -e pico & rxvt -geometry 80x24+806+2 -bg white -fg black -e pine &