On Fri, 4 Jun 2004 09:52:15 -0500, Charles K. Clarkson wrote:
> Show us the example from the book. Or give us a link to it on line.
> (When you cut and paste code please prune excess spacing.) A sample
> file would be nice to.

Here you go.
Example: listing users
To start with, let’s produce a list of all of the real names of all of the users on the
system. As that would be a little too simple we’ll introduce a couple of refinements.
First, included in the list of users in /etc/passwd are a number of special accounts
that aren’t for real users. These will include root (the superuser), lp (a user ID
which is often used to carry out printer administration tasks) and a number of other
task-oriented users. Assuming that we can detect these uses by the fact that their full
names will be empty, we’ll exclude them from the output. Secondly, in the original
file, the full names are in the format <forename> <surname>. We’ll print them out
as <surname>, <forename>, and sort them in surname order. Here’s the script:
use strict;
my $users = read_passwd();
my @names;
foreach (keys %{$users}) {
next unless $users->{$_}{fullname};
my ($forename, $surname) = split(/\s+/, $users->{$_}{fullname}, 2);
push @names, "$surname, $forename";
}
print map { "$_\n" } sort @names;
Example: reading /etc/passwd
Let’s start by writing a routine to read the data into internal data structures. This
routine can then be used by any of the following examples. As always, for flexibility,
we’ll assume that the data is coming in via STDIN.
sub read_passwd {
my %users;
my @fields = qw/name pword uid gid fullname home shell/;
while (<STDIN>) {
chomp;
my %rec;
@[EMAIL PROTECTED]) = split(/:/);
$users{$rec->{name}} = \%rec;
}
return \%users;
}
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to