⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0441-0443.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:


<HTML>

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Automating Tasks</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=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=22 //-->

<!-- PAGES=0437-0454 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0437-0440.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0444-0446.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-441"><P>Page 441</P></A>











<P>

of the work is handed off to other commands; the shell just &quot;glues&quot; together results. This

is typical, and a correct style you should adopt for your own scripting.

</P>









<P>Certainly it was pleasant when the filenames changed and I realized I could rework one

word of the script, rather than retyping the two hundred entries. As satisfying as this was, the

total benefit of automation is still more profound. Even greater than saving my time are the

improvements in quality, traceability, and reusability this affords. With the script, I control

the data entering the database at a higher level and eliminate whole categories of error:

mistyping, accidentally pushing a wrong button in a graphical user interface, and so on. Also, the script

in Listing 22.1 records my procedure, in case it's later useful to audit the data. Suppose, for

example, that next year it's decided I shouldn't have inserted any of these references to the

database's Picture attribute. How many will have to be backed out? Useful answers&#151;at most, the

count of $DIR/*.jpg&#151;can be read directly from the script; there's no need to rely on memory

or speculate.

</P>









<H3><A NAME="ch22_ 6">

Tips for Improving Automation Technique

</A></H3>









<P>You're in charge of your career in automation. Along with everything else this chapter

advises, you'll go farthest if you do the following:

</P>









<UL>

<LI>          Improve your automation technique.

<LI>          Engineer well.

</UL>









<P>These tips have specific meaning in the rest of this chapter. Look for ways to apply them in

all that follows.

</P>









<H4><A NAME="ch22_ 7">

Continuing Education

</A></H4>









<P>There are three important ways to improve your skill with these techniques, which apply

equally well whether you're using Perl, cron, Expect, or another mechanism:

</P>









<UL>

<LI>          Scan the documentation.

<LI>          Read good scripts.

<LI>          Practice writing scripts.

</UL>









<P>Documentation has the reputation of being dry and even unreadable. It's very important

you learn how to employ it. All the tools presented here have man pages, which you need to

be comfortable using. Read these documents and reread them. Authors of the tools faced many

of the challenges you do. Often, reading through the lists of options or keywords, you'll

realize that particular capabilities apply exactly to your situation. Study the

documentation with this in mind; look for the ideas that you can use. Give particular attention to commands you

don't recognize. If some of them&#151;cu, perhaps, or

od&#151;are largely superannuated, you'll realize

in reading about others&#151;such as tput, ulimit,

bc, nice, or wait&#151;that earlier users were

confronted with just the situations that confound your own work. Stand on their shoulders and see <BR>

farther.

</P>





<A NAME="PAGENUM-442"><P>Page 442</P></A>











<P>It's important to read good programming. Aspiring literary authors find inspiration in

Pushkin and Pynchon, not grammar primers; similarly, you'll go farthest when you read the best

work of the best programmers. Look in the columns of computer magazines and, most

importantly, the archives of software with freely available source. Good examples of coding occasionally

turn up in Usenet discussions. Prize these; read them and learn from the masters.

</P>









<P>All the examples in this chapter are written to be easy to use. They typically do one small

task completely; this is one of the best ways to demonstrate a new concept. Although

exception handling, and argument validation in particular, is important, it is beyond the scope of

this chapter.

</P>









<P>Crystallize your learning by writing your own scripts. All the documents you read will

make more sense after you put the knowledge in place with your own experience.

</P>









<H4><A NAME="ch22_ 8">

Good Engineering

</A></H4>









<P>The other advice for those pursuing automation is to practice

good engineering. This always starts with a clear, well-defined goal. Automation isn't an absolute good; it's only a method

for achieving human goals. Part of what you'll learn in working through this chapter is how

much, and how little, to automate.

</P>









<P>When your goal is set, move as close to it as you can with components that are already

written. &quot;Glue&quot; existing programs together with small, understandable scripting modules.

Choose meaningful variable names. Define interfaces carefully. Write comments.

</P>









<H3><A NAME="ch22_ 9">

Shell Scripts

</A></H3>









<P>You already learned many of the elements of automation in Part IV, &quot;System

Administration and Management,&quot; particularly in Chapter 14, &quot;Getting Started with Red Hat Linux&quot;;

and Appendix B supplements it. Let's look at a few additional examples of scripts that are

often useful in day-to-day operation.

</P>









<H5><A NAME="ch22_ 10">

chstr

</A></H5>









<P>Users who maintain source code, client lists, and other records often want to launch a

find-and-replace operation from the command line. It's useful to have a variant of

chstr on UNIX hosts. Listing 22.2 gives one example.

</P>









<P>Listing 22.2. chstr&#151;a simple find-and-replace operation.

</P>



<!-- CODE //-->

<PRE>

########

#

# See usage() definition, below, for more details.

#

# This implementation doesn't do well with complicated escape

#     sequences. That has been no more than a minor problem in

</PRE>

<!-- END CODE //-->





<A NAME="PAGENUM-443"><P>Page 443</P></A>





<!-- CODE //-->

<PRE>

#     the real world.

#

########

usage() {

     echo \

&quot;chstr BEFORE AFTER &lt;filenames&gt;

     changes the first instance of BEFORE to AFTER in each line of &lt;filenames&gt;,

     and reports on the differences.

Examples:

     chstr TX Texas */addresses.*

     chstr ii counter2 *.c&quot;

     exit 0

}



case $1 in

     -h|-help)     usage;;

esac



if test $# -lt 3

then

     usage

fi



TMPDIR=/tmp

     # It's OK if more than one instance of chstr is run simultaneously.

     #     The TMPFILE names are specific to each invocation, so there's

     #     no conflict.

TMPFILE=$TMPDIR/chstr.$$



BEFORE=$1

AFTER=$2



     # Toss the BEFORE and AFTER arguments out of the argument list.

shift;shift



for FILE in $*

do

     sed -e &quot;s/$BEFORE/$AFTER/&quot; $FILE &gt;$TMPFILE

     echo &quot;$FILE:&quot;

     diff $FILE $TMPFILE

     echo &quot;&quot;

     mv $TMPFILE $FILE



done

</PRE>

<!-- END CODE //-->









<P>Most interactive editors permit a form of global search-and-replace, and some even make

it easy to operate on more than one file. Perhaps that's a superior automation for your needs.

If not, chstr is a minimal command-line alternative that is maximally simple to use.

</P>









<H4><A NAME="ch22_ 11">

WWW Retrieval

</A></H4>









<P>A question that arises frequently is how to automate retrieval of pages from the World

Wide Web. This section shows the simplest of many techniques.

</P>





<P><CENTER>

<a href="0437-0440.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0444-0446.html">Next</A>

</CENTER></P>









</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -