Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Getting a GID from /etc/group

by c (Hermit)
on Mar 25, 2002 at 17:03 UTC ( #154141=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

I see that there is indeed a module for accessing unix system files. However, is there a non-module method similar to getpwnam that will return a GID for a groupname?

humbly c

Replies are listed 'Best First'.
Re: Getting a GID from /etc/group
by Zaxo (Archbishop) on Mar 25, 2002 at 17:09 UTC

    getgrnam. getgrent and getgrgid are also of interest. perldoc -f on your local machine will get the most accurate description for your installation, though I wouldn't expect them to change much.

    After Compline,

Re: Getting a GID from /etc/group
by davis (Vicar) on Mar 25, 2002 at 17:10 UTC
    #!/usr/bin/perl -w use strict; my ($name,$passwd,$gid,$members) = getgrnam("www"); print "Name: ", $name, "\n"; print "GID: ", $gid, "\n";
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
    Update: Pah - beaten again. I really *have* to ignore the phone ;-)
Re: Getting a GID from /etc/group
by particle (Vicar) on Mar 25, 2002 at 17:22 UTC
    this one caches, to make future lookups faster...
    { my %gid; sub gid($) { my $group = shift; defined $gid{$group} && return $gid{$group}; my $t = getgrnam($group); $gid{$group} = ${ $t }[2]; return $gid{$group}; } }

    ~Particle ;

Re: Getting a GID from /etc/group
by gryphon (Abbot) on Mar 25, 2002 at 17:25 UTC

    Greetings c,

    What others have posted already would be smarter than what I'm going to suggest. However, in the interests of contribution of all ideas, even ones that are fairly basic, here's another option:

    #!/usr/bin/perl -w use strict; my $group_name = 'monks'; open(GROUPS, '/etc/group'); my $gid = (split /:/, (grep /^$group_name:/, <GROUPS>)[0])[2]; close GROUPS; print $gid, "\n"; exit;

    Using getgrnam is a much better option, though. My way here assumes way too much and could therefore fail in many situations.

    code('Perl') || die;

Re: Getting a GID from /etc/group
by CukiMnstr (Deacon) on Mar 25, 2002 at 18:20 UTC
    Yet another way of doing it would be to use User::grent by Tom Christiansen.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://154141]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2023-05-30 17:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found