Example program: pwget
#! /usr/bin/perl -w
use Getopt::Long;
use strict;
sub usage {
die "usage: $0 [-n name] [-u uid]\n";
}
GetOptions("name|n=s" => \my $name, "uid|u=i" => \my $uid) or usage;
usage if @ARGV;
while (my @pw = getpwent()) {
next if defined($name) && $name ne $pw[0];
next if defined($uid) && $uid != $pw[2];
print join(":", @pw), "\n";
}
__END__
NAME
pwget - get password and group information
SYNOPSIS
pwget [-n name | -u uid]
DESCRIPTION
pwget locates and displays information from /etc/passwd
The standard output of pwget contains lines of colon-separated
password information whose format is the same as that used in the
/etc/passwd file (see passwd(4)).
With no options, pwget gets all entries with getpwent() and
outputs a line for each entry found.
Options
When an option is given, only a single entry is printed.
-n name Output the first entry that matches name using
getpwnam() (see getpwent(3C)).
-u uid Output the first entry that matches uid using
getpwuid() (see getpwent(3C)).