On Wed, Dec 20, 2006 at 05:28:46PM -0800, 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'm surprised I missed that.  Let this be a lesson to Tom
and others who may follow: testing your code is important.  To solve the
problem, you may want to ensure that your $ARGV[0] is anchored to the
beginning of the string and immediately followed by a colon when doing
your regex matching.


> 
> 
> >                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.

Singling out the primary group wasn't a requirement for the Perl script,
as far as I recall.  If I'm mistaken, then yeah, you might want to check
/etc/passwd for the primary group.  If not, you'll get the primary group
along with the rest of them from /etc/group (but it won't be identified
as any different from the rest of them).

At least, that's how it works here.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the Lucid

-- 
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