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

📄 474-477.html

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

<HEAD>

<TITLE>Linux Configuration and Installation:Programming in Linux</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=1558285660//-->

<!--TITLE=Linux Configuration and Installation//-->

<!--AUTHOR=Patrick Volkerding//-->

<!--AUTHOR=Kevin Reichard//-->

<!--AUTHOR=Eric Foster//-->

<!--PUBLISHER=IDG Books Worldwide, Inc.//-->

<!--IMPRINT=M & T Books//-->

<!--CHAPTER=10//-->

<!--PAGES=474-477//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="470-473.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="477-480.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">Perl</FONT></H4>

<P>Perl is a freeware scripting language developed to handle a number of system administration tasks. <I>Perl</I> stands for Practical Extraction and Report Language. The whole point of the language is to make it easier for you to extract data from UNIX and output reports on things such as Usenet news disk usage and a list of all users on your systems, sorted in order of largest disk usage. (Perl tends to excel at tasks that revolve around reporting system information.)</P>

<P>To make sure you&#146;ve installed perl when you install Linux, type in the following:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:/$ perl -v

</PRE>

<!-- END CODE SNIP //-->

<P>If you have perl on your system, you should see the version number for perl. If not, you need to run the <B>setup</B> program again. (The <B>setup</B> program installs Perl 5.002.)</P>

<P><FONT SIZE="+1"><B>A First Perl Script</B></FONT></P>

<P>Perl, like most Tcl and other UNIX scripting languages, uses the # as a comment marker. Any line with # is ignored from the # onward. To print data in a perl script, use the <I>print</I> statement:</P>

<!-- CODE SNIP //-->

<PRE>

     #! /usr/bin/perl

     print "Linux runs perl!\n";

     print "Oh, joy!\n";

</PRE>

<!-- END CODE SNIP //-->

<P>When you run this script, you&#146;ll see the following output, as you&#146;d expect:

</P>

<!-- CODE SNIP //-->

<PRE>

     Linux runs perl!

     Oh, joy!

</PRE>

<!-- END CODE SNIP //-->

<P>The <I>\n</I> stands for a new line, or linefeed character, and is typical UNIX parlance as we described in the section on C programming.</P>

<P>You can also prompt for data in Perl, using the following odd syntax:</P>

<!-- CODE //-->

<PRE>

     #! /usr/bin/perl

     # Prompting for input in perl.



     print "What is your first name: ";



     # &lt;STDIN&gt; stands for standard input: the keyboard.

     $first_name = &lt;STDIN&gt;;



     # Remove trailing linefeed.

     chomp($first_name);



     printf "What is your last name: ";

     $last_name = &lt;STDIN&gt;;



     chomp($last_name);



     print "Your name is $first_name $last_name.\n";

</PRE>

<!-- END CODE //-->

<P>When you run this script, you&#146;ll see the following prompts:

</P>

<!-- CODE SNIP //-->

<PRE>

     What is your first name: Eric

     What is your last name: Johnson

     Your name is Eric Johnson.

</PRE>

<!-- END CODE SNIP //-->

<P>Perl provides a lot of support for arrays, UNIX process control, and string handling. Perl offers a string set of array operations, which allow you to have a set of data treated as one unit, for example:

</P>

<!-- CODE SNIP //-->

<PRE>

     (1,2,3,4,5,6)

</PRE>

<!-- END CODE SNIP //-->

<P>This array has the values 1 through 6. You can also mix text and numeric values, as shown here:

</P>

<!-- CODE SNIP //-->

<PRE>

     (1, 2, 3, 4, "Linux is out the door")

</PRE>

<!-- END CODE SNIP //-->

<P>You can assign this array to a variable and then access any element in the array. A great strength of perl is its associative arrays, where you can use a key value for an array index and associate this with a data value. For example, you can have a perl array for a first name, last name, and street address. You could then access the street address as shown here:

</P>

<!-- CODE //-->

<PRE>

     #! /usr/bin/perl

     # Associative arrays in perl.



     # zippy is an associative array.



     $zippy&#123;"firstname"&#125; = "Zippy";

     $zippy&#123;"address"&#125; = "1600 Pennsylvania Ave.";



     # Print the data.

     print $zippy&#123;"firstname"&#125;;

     print "'s address is ";

     print $zippy&#123;"address"&#125;;



     # End with a carriage return.

     print "\n";

</PRE>

<!-- END CODE //-->

<P>This example stores a first name and an address in the associative array named <I>zippy</I>. Associative arrays form a very powerful feature and can be used effectively in a lot of system administration tasks.</P>

<P>The output of the preceding script looks something like the following (and predicts the results of the next election):</P>

<!-- CODE SNIP //-->

<PRE>

     Zippy's address is 1600 Pennsylvania Av.

</PRE>

<!-- END CODE SNIP //-->

<P>In addition to associative arrays, perl has a lot of commands to format text to allow you to create reports (the original reason for perl&#146;s existence). perl is intimately tied in with UNIX and provides a number of shortcuts for common UNIX activities, like accessing the <B>password</B> file, as we show here:</P>

<!-- CODE //-->

<PRE>

     #! /usr/bin/perl

     # Accessing the password file.



     # Get Eric's password entry and print it.



     @erc_entry = getpwnam("erc");

     ($username, $realname, $homedir) = @erc_entry[0,6,7];



     print "User $realname has";

     print " a home directory of $homedir";

     print " and a username of $username.\n";

</PRE>

<!-- END CODE //-->

<P>When you run this script, you&#146;ll see output like the following:

</P>

<!-- CODE SNIP //-->

<PRE>

     User Eric F. Johnson has a home directory of /home/erc

     and a username of erc.

</PRE>

<!-- END CODE SNIP //-->

<P>Naturally, you&#146;ll want to use a username available on your system.

</P>

<P>There&#146;s a lot more to perl, which fills more than one book on the subject. If you&#146;re interested in learning more about perl, see Appendix A.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="470-473.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="477-480.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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