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

📄 ch15.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
postmaster           14

<BR>

ppp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;504

<BR>

root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0

<BR>

shutdown&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6

<BR>

sync&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5

<BR>

tparker&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;503

<BR>

uucp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10

<BR>

uzma&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;505

<BR>

walter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;502</FONT></TT>

</BLOCKQUOTE>

<P>

Two sister functions exist for getting the same nine-item list

about a user in the <TT><FONT FACE="Courier">/etc/passwd</FONT></TT>

file: <TT><FONT FACE="Courier">getpwnam()</FONT></TT> and <TT><FONT FACE="Courier">getpwuid()</FONT></TT>.

The items in the returned list are in the same type as those returned

by a call to <TT><FONT FACE="Courier">getpwent()</FONT></TT>.

The <TT><FONT FACE="Courier">getpwnam()</FONT></TT> function returns

the list given a user name, whereas the <TT><FONT FACE="Courier">getpwuid()</FONT></TT>

function returns the list given a user ID.

<P>

The <TT><FONT FACE="Courier">getpwnam()</FONT></TT> and <TT><FONT FACE="Courier">getpwuid()</FONT></TT>

functions have the following syntax:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">(<I>$username, $password, $userid, $groupid,

<BR>

$quota, $comment, $userInfo, $userHome, $loginShell</I>)<BR>

</FONT></TT>&nbsp;&nbsp;&nbsp;&nbsp;<TT><FONT FACE="Courier">=

getpwnam ($name);<BR>

<BR>

<I>($username, $password, $userid, $groupid,<BR>

$quota, $comment, $userInfo, $userHome, $loginShell)<BR>

</I></FONT></TT>&nbsp;&nbsp;&nbsp;&nbsp;<TT><FONT FACE="Courier">=

getpwuid ($id);</FONT></TT>

</BLOCKQUOTE>

<P>

An empty list is returned if no matching entry is found in <TT><FONT FACE="Courier">/etc/passwd</FONT></TT>.

A common use for <TT><FONT FACE="Courier">getpwnam</FONT></TT>

is to get a user ID for a user in a Perl script and use that value

for creating temporary files.

<H3><A NAME="GettingGroupRelatedInformationwithg">Getting Group-Related

Information with <TT><FONT SIZE=4 FACE="Courier">getgrent()</FONT></TT><FONT SIZE=4>

and </FONT><TT><FONT SIZE=4 FACE="Courier">getgrnam()</FONT></TT></A>

</H3>

<P>

The <TT><FONT FACE="Courier">getgrent</FONT></TT> function is

used to list the contents of an entry in the <TT><FONT FACE="Courier">/etc/group</FONT></TT>

file. The following information is provided for each entry:

<UL>

<LI><FONT COLOR=#000000>The user group name</FONT>

<LI><FONT COLOR=#000000>The user group password, if any</FONT>

<LI><FONT COLOR=#000000>The group ID</FONT>

<LI><FONT COLOR=#000000>A list of the user IDs in this group</FONT>

</UL>

<P>

Here's the syntax for the <TT><FONT FACE="Courier">getgrent</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($gname, $gpasswd, $gid, $gmembers) =

getgrent;</FONT></TT>

</BLOCKQUOTE>

<P>

This function returns four items corresponding to the previous

list. The name, password, and ID fields are all scalar values.

The value of <TT><FONT FACE="Courier">gmembers</FONT></TT> is

a list of user IDs separated by spaces.

<P>

The <TT><FONT FACE="Courier">setgrent</FONT></TT> function sets

the pointer in the <TT><FONT FACE="Courier">/etc/group</FONT></TT>

file back to the top. After <TT><FONT FACE="Courier">setgrent</FONT></TT>

is called, the next call to <TT><FONT FACE="Courier">getgrent</FONT></TT>

retrieves the first element of the <TT><FONT FACE="Courier">/etc/group</FONT></TT>

file. The <TT><FONT FACE="Courier">endgrent</FONT></TT> function

stops further access to the elements in the <TT><FONT FACE="Courier">/etc/group</FONT></TT>

file and frees up the memory used to store group information.

<P>

Here's the syntax for these functions:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">setgrent();<BR>

endgrent();</FONT></TT>

</BLOCKQUOTE>

<P>

Each call to <TT><FONT FACE="Courier">getgrent</FONT></TT> returns

one line from the <TT><FONT FACE="Courier">/etc/group</FONT></TT>

file. A <TT><FONT FACE="Courier">NULL</FONT></TT> value (that

is, an empty list) is returned when the last item is read. To

print the contents of the group file, use a <TT><FONT FACE="Courier">while</FONT></TT>

loop like the one shown in Listing 15.2.

<HR>

<BLOCKQUOTE>

<B>Listing 15.2. Getting group information.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">1 while (($gname, $gpasswd, $gid, $gmembers)

= getgrent) {<BR>

2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$groupsFound{$gname}

= $gmembers;<BR>

3 }<BR>

4 foreach $i (sort keys (%groupsFound)) {<BR>

5&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\n User IDs for group:&quot;,

$groupsFound{$i} ;<BR>

6 }</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The <TT><FONT FACE="Courier">getgrnam</FONT></TT> function returns

an <TT><FONT FACE="Courier">/etc/group</FONT></TT> file entry

when given a group name. Here's the syntax for the <TT><FONT FACE="Courier">getgrnam</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($gname, $gpasswd, $gid, $gmembers) =

getgrnam ($name);</FONT></TT>

</BLOCKQUOTE>

<P>

The variable <TT><FONT FACE="Courier">$gname</FONT></TT> is the

group name to search for. The <TT><FONT FACE="Courier">$getgrnam</FONT></TT>

returns the same four-element list that <TT><FONT FACE="Courier">getgrent</FONT></TT>

returns. Here is the output:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">adm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4

<BR>

bin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1

<BR>

daemon&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2

<BR>

disk&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6

<BR>

floppy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11

<BR>

kmem&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5

<BR>

users&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100

<BR>

uucp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14

<BR>

wheel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Here's another sample of how to list users given a group name.

Listing 15.3 shows a simple script that prints the users in a

group.

<HR>

<BLOCKQUOTE>

<B>Listing 15.3. A program that uses </B><TT><B><FONT FACE="Courier">getgrnam</FONT></B></TT><B>.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 print (&quot;Please enter name of the group:\n&quot;);

<BR>

&nbsp;3 $name = &lt;STDIN&gt;;

<BR>

&nbsp;4 chop($name); #<BR>

&nbsp;5<BR>

&nbsp;6 if (!(($gname, $gpasswd, $gid, $gmembers) = getgrnam ($name)))

{<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die

(&quot;There is no&nbsp;&nbsp;$name group!. \n&quot;);<BR>

&nbsp;8 }<BR>

&nbsp;9<BR>

10 $count = 0;<BR>

11 while (1) {<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last if

($gmembers eq &quot;&quot;);<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($uid,

$gmembers) = split (/\s+/, $gmembers, 2);<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf

(&quot;&nbsp;&nbsp;%-15s&quot;, $uid);<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$count++;

<BR>

16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (($count

% 3) == 0) {<BR>

17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print (&quot;\n&quot;);<BR>

18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

19 }<BR>

20 if ($count % 4) {&nbsp;&nbsp;&nbsp;# finish it off.<BR>

21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

(&quot;\n&quot;);<BR>

22 }</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Lines 16 and 20 print the output four items per line. <TT><FONT FACE="Courier">getgrid()</FONT></TT>

retrieves the user information as returned by <TT><FONT FACE="Courier">getgrnam()</FONT></TT>,

except that it retrieves it by group ID. Here's the syntax for

the <TT><FONT FACE="Courier">getgrid</FONT></TT> function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($gname, $gpasswd, $gid, $gmembers) =

getgrid ($gid);</FONT></TT>

</BLOCKQUOTE>

<P>

Generally, the call is just used to get the group name given a

group ID:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($gname) = getgrid (3);</FONT></TT>

</BLOCKQUOTE>

<P>

Be careful, though, to parenthesize the <TT><FONT FACE="Courier">$gname</FONT></TT>

variable to indicate that the <TT><FONT FACE="Courier">$gname</FONT></TT>

variable is an element in a list and not a list itself<FONT SIZE=1>&nbsp;</FONT>!

If you make the call like this:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$gname = getgrid (3);</FONT></TT>

</BLOCKQUOTE>

<P>

the value of <TT><FONT FACE="Courier">$gname</FONT></TT> is the

returned list, not the first element of the array.

<H2><A NAME="GettingInformationinNetworkFiles"><FONT SIZE=5 COLOR=#FF0000>Getting

Information in Network Files</FONT></A></H2>

<P>

Perl offers several functions to get information about networking

files and items in the files on your system. By using these functions,

you can create very powerful networking applications.

<H3><A NAME="ThegetnetentFunction">The <TT><FONT SIZE=4 FACE="Courier">getnetent</FONT></TT><FONT SIZE=4>

Function</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">getnetent</FONT></TT> function enables

you to read entries in the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>

file for all the names and addresses recognized as valid names

by the domain name server for your machine. Here's the syntax

for <TT><FONT FACE="Courier">getnetent()</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">($name, $aliases, $addrType, $inet) =

getnetent();</FONT></TT>

</BLOCKQUOTE>

<P>

Four items are returned by this function:

<UL>

<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$name</FONT></TT>

variable is the name of a network.

<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$aliases</FONT></TT>

⌨️ 快捷键说明

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