📄 ch15.htm
字号:
postmaster 14
<BR>
ppp 504
<BR>
root 0
<BR>
shutdown 6
<BR>
sync 5
<BR>
tparker 503
<BR>
uucp 10
<BR>
uzma 505
<BR>
walter 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> <TT><FONT FACE="Courier">=
getpwnam ($name);<BR>
<BR>
<I>($username, $password, $userid, $groupid,<BR>
$quota, $comment, $userInfo, $userHome, $loginShell)<BR>
</I></FONT></TT> <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 $groupsFound{$gname}
= $gmembers;<BR>
3 }<BR>
4 foreach $i (sort keys (%groupsFound)) {<BR>
5 print "\n User IDs for group:",
$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 4
<BR>
bin 1
<BR>
daemon 2
<BR>
disk 6
<BR>
floppy 11
<BR>
kmem 9
<BR>
lp 7
<BR>
mail 12
<BR>
man 15
<BR>
mem 8
<BR>
news 13
<BR>
nogroup 65535
<BR>
root 0
<BR>
sys 3
<BR>
tty 5
<BR>
users 100
<BR>
uucp 14
<BR>
wheel 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"> 1 #!/usr/bin/perl<BR>
2 print ("Please enter name of the group:\n");
<BR>
3 $name = <STDIN>;
<BR>
4 chop($name); #<BR>
5<BR>
6 if (!(($gname, $gpasswd, $gid, $gmembers) = getgrnam ($name)))
{<BR>
7 die
("There is no $name group!. \n");<BR>
8 }<BR>
9<BR>
10 $count = 0;<BR>
11 while (1) {<BR>
12 last if
($gmembers eq "");<BR>
13 ($uid,
$gmembers) = split (/\s+/, $gmembers, 2);<BR>
14 printf
(" %-15s", $uid);<BR>
15 $count++;
<BR>
16 if (($count
% 3) == 0) {<BR>
17
print ("\n");<BR>
18 }<BR>
19 }<BR>
20 if ($count % 4) { # finish it off.<BR>
21 print
("\n");<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> </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 + -