Sorry, I sent these to the poster rather than the list--my bad.
-------- Original Message --------
Subject: Re: Displaying a user's group memberships
Date: Wed, 20 Dec 2006 20:23:53 -0700
From: Tom Smith <[EMAIL PROTECTED]>
To: John W. Krahn <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
<[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
<[EMAIL PROTECTED]>
John W. Krahn wrote:
Tom Smith wrote:
Thank Chad (and John) for your input on this. I thought I'd post the
portion of the script that I was trying to work out to see if there's
room for improvement. This should work on any *nix system. The format of
the command is simple: `test.pl username`, where username is a real
username on the system in question. Here's the script:
test.pl:
#!/usr/bin/env perl
use strict;
use warnings;
# Determine which Linux groups the user belongs to.
open(FILE,'</etc/group') or die "Can't open /etc/group.";
my @memberof;
while ($_ = <FILE>) {
if($_ =~ /$ARGV[0]/) {
Say that you have two users 'ron' and 'ronald'. If $ARGV[0] contains 'ron'
then this will get you the group names for *both* 'ron' *and* 'ronald' (and
any other group names where the string 'ron' is found.)
Good point... I hadn't thought of that. I'll look in to resolving it
tomorrow (should be an easy fix, I would think :-) ).
my @groups = split(/:/,$_);
push(@memberof,$groups[0]);
}
}
close FILE;
print "[EMAIL PROTECTED]";
So is there a better way to do this, or perhaps a cleaner way?
Since you are only reading from /etc/group you are not picking up the primary
group stored in /etc/passwd.
I did think of that, but the primary group is still a group--even if
it's the user's username. And thus it's listed in /etc/group. So I'm not
sure that there's really an issue here--is there?
~ Tom