This is only my second go at writing something that works.
It was my idea to have a program look up usernames and return info on them
them including a harsh email address.
Beware if you run it in a NIS environment, the csv-file could get large.
I am aware of the global variable issue.
#!/usr/bin/perl -w
#######################################
#you need to create a file called #
#users.csv in the same dir as #
#you run the script in. #
# no strict 'vars' isn't that a good #
#idea to use. #
#######################################
use strict; no strict 'vars';
$userfile = "users.csv";
$yourhost = "\@yourdomain.??";
main();
sub main {
create();
my $input = input();
print "You are looking for $input\n";
match();
exit;
}
sub match {
open FILE, "<$userfile"
or die "Can't read $userfile; $! \n";
my ( $name, $uid, $dir );
my $found;
search: while (<FILE>) {
chop;
( $name, $uid, $dir) = split( /:/, $_ );
if ($name eq $youare) {
$found = $.;
print "Located person: $youare on line $found\n\n";
print "Person: $youare\n";
print "UID: $uid\n";
print "Homedir: $dir\n";
print "Email: $name$yourhost\n";
}
}
exit;
}
sub input {
print "Enter username to look for\n";
$youare = <STDIN>;
chomp $youare;
return $youare;
}
sub create {
if ((-M $userfile > 3) or (-z $userfile)) {
while (($name, undef, $uid, undef, undef, undef, undef, $dir,)
+ = getpwent) {
open USERDATA, ">>$userfile"
or die "Can't open $userfile for Appending: $!\n";
my $record = join( ":", $name, $uid, $dir );
print(USERDATA "$record\n")
or die "Error writing to $userfile: $!\n";
}
}
}