📄 0096-0098.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Using the Shell:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=06 //-->
<!-- PAGES=0083-0102 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0093-0095.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0099-0101.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-96"><P>Page 96</P></A>
<P>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.
</P>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# jobs
[1] Stopped (signal) pine
[2]- Stopped sc
[3]+ Stopped emacs-nox
# fg %sc
... sc program is running ...
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# kill %1
[1]- Stopped (signal) pine
# kill %emacs-nox
[3]+ Stopped emacs-nox
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>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.
</P>
<H4><A NAME="ch06_ 14">
How to Use Pipes
</A></H4>
<P>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</P>
<A NAME="PAGENUM-97"><P>Page 97</P></A>
<P>and other versions of UNIX.
</P>
<P>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.
</P>
<P>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.
</P>
<!-- CODE SNIP //-->
<PRE>
# ls | lpr
# printenv | fgrep EDITOR
# nroff -man mymanpage.1 | less
# cat document.txt | wc | mail -s "Document.txt Report" bball
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# find /home -name *.doc | xargs fgrep administration | less
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<!-- CODE SNIP //-->
<PRE>
# find *.doc | xargs cat | tr ` ` `\n' | sort | uniq | tee dict | less
</PRE>
<!-- END CODE SNIP //-->
<P>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</P>
<A NAME="PAGENUM-98"><P>Page 98</P></A>
<P>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.
</P>
<TABLE BGCOLOR=#FFFF99><TR><TD>JUST A MINUTE</TD></TR><TR><TD>
<BLOCKQUOTE>
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.
</BLOCKQUOTE></TD></TR></TABLE>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# xwd -out wd.xwd
# xwdtopnm < wd.xwd | ppmtogif | tee wd.gif | giftopnm | tee wd.pnm | pnmtotiff
Â>wd.tif
</PRE>
<!-- END CODE SNIP //-->
<P>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!
</P>
<P>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.
</P>
<H3><A NAME="ch06_ 15">
Building Shell Commands
</A></H3>
<P>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
</P>
<!-- CODE SNIP //-->
<PRE>
rxvt -geometry 80x11+803+375 -bg white -fg black -e pico &
rxvt -geometry 80x24+806+2 -bg white -fg black -e pine &
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="0093-0095.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0099-0101.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -