On 12/21/2006 02:59 AM, Chad Perrin wrote:
On Wed, Dec 20, 2006 at 10:47:25PM -0800, John W. Krahn wrote:
Mumia W. wrote:
On 12/20/2006 04:14 PM, Tom Smith wrote:
[...]
So is there a better way to do this, or perhaps a cleaner way?
use strict;
use warnings;
$_ = `id $ARGV[0]`;
s/.*?groups=// && print "@{[ /\((\w+)\)/g ]}\n";
print $1 if `groups $ARGV[0] 2>/dev/null` =~ /$ARGV[0]\s*:\s*(.+)/;
According to the manpage for groups on FreeBSD Release 6.1, it's
deprecated in favor of the id utility -- and with the -Gn switches, it
works identically to the groups command. Just thought I'd mention.
I wish I'd looked at the man-page for "id." Oh well, here's another way
to do it within Perl:
use strict;
use warnings;
my @groups;
push @groups, (getpwnam $ARGV[0])[3];
while (my @gr = getgrent) {
push @groups, $gr[2] if ($gr[3] =~ /\b\Q$ARGV[0]\E\b/);
}
@groups = map scalar getgrgid($_), @groups;
print "@groups\n";
__END__
Let's hope that's platform-independent enough :-)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>