Created
April 23, 2009 14:32
-
-
Save antonlindstrom/100518 to your computer and use it in GitHub Desktop.
School assignment #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# | |
# Print out users in system to a html file. | |
# | |
# Opens files passwd and group, checks group for users in staff | |
# and assigns others in another array. Users names are assigned | |
# to a hash and then printed out as names in html list. Mail is | |
# [email protected], hashes are sorted on users names | |
# before printout. | |
# Open file. | |
sub openfile { | |
($file) = @_; | |
open(FILE, $file) || die("Could not open $file!\n$!\n"); | |
@filecontents = <FILE>; | |
close(FILE); | |
return @filecontents; | |
} | |
# Use subroutine openfile and assign to array. | |
@passwd = &openfile("passwd"); | |
@group = &openfile("group"); | |
# Determine which group a user is part of. | |
foreach $row(@group) { | |
# Split and chomp @gpost. | |
@gpost = split(/:/, $row); | |
@gmember = split(/:/, $row); | |
chomp(@gpost, @gmember); | |
# Set staff to $staff and others to $others. | |
if ($gpost[0] eq "staff") { | |
push(@staff, $gpost[3]); | |
} else { | |
push(@others, $gpost[3]); | |
} | |
} | |
# Go through passwd file, checks if user is in group and adds full name to hash. | |
foreach $prow(@passwd) { | |
# Split and chomp. | |
@user = split(/:/, $prow); | |
chomp(@user); | |
$username = $user[0]; | |
# Conditions if user is member of staff or other group. | |
$memberofstaff = grep (/$username/, @staff); | |
$memberofother = grep (/$username/, @others); | |
# Skip if member is not in group file. | |
next if not ( $memberofstaff || $memberofother ); | |
# Only name, not numbers. | |
$user[4] =~ m/([a-zåäöÅÄÖ\ ]+)/i; | |
$fullname = $1; | |
# Username as key in hash and all text in comment field. | |
if($memberofstaff) { | |
$infstaff{$username} = $fullname; | |
} else { | |
$infother{$username} = $fullname; | |
} | |
} | |
# Sort hash %infstaff and assign to stafflink. | |
foreach $val(sort {$infstaff{$a} cmp $infstaff{$b} } keys %infstaff) { | |
$stafflink .= '<li><a href="mailto:'.$val.'@universitet.se">'.$infstaff{$val}.'</a></li>'; | |
} | |
# Sort hash %infother and assign to otherlink. | |
foreach $val2(sort {$infother{$a} cmp $infother{$b} } keys %infother) { | |
$otherlink .= '<li><a href="mailto:'.$val2.'@universitet.se">'.$infother{$val2}.'</a></li>'; | |
} | |
# Create template for HTML. | |
$htmldoc = "<html>\n <head><title>Users</title</head> | |
<body>\n <h1>Users</h1> | |
<h2>Staff</h2>\n <ul>$stafflink</ul>\n <h2>Others</h2>\n <ul>$otherlink</ul> | |
</body> | |
</html>"; | |
# Open users.html and print template ($htmldoc) to it. | |
open (OUTPUTF, "> users.html") or die ("File users.html could not be opened. $!"); | |
print OUTPUTF $htmldoc; | |
close (OUTPUTF); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment