[aklug] Re: Old Skool SED help requested

From: Michael Fowler <michael@shoebox.net>
Date: Mon Oct 18 2010 - 10:01:47 AKDT

Jim,

If you can, I'd recommend using something other than scanning across
group and passwd files.

Getting the groups of a given user:

as name:
> id -n -G michael
    michael dialout cdrom floppy audio src video plugdev netdev powerdev

> perl -wle '
        $u = shift;
        @gs = scalar getgrgid((getpwnam $u)[2]);
        setgrent;
        while (@g = getgrent) {
            push @gs, $g[0] if grep { $u eq $_ } split " ", $g[3]
        }
        endgrent;
        print "@gs";
    ' michael

    dialout cdrom floppy audio src video plugdev netdev powerdev

as gid:
> id -G michael
    1000 20 24 25 29 40 44 46 111 116

> perl -wle '
        $u = shift;
        @gs = (getpwnam $u)[2];
        setgrent;
        while (@g = getgrent) {
            push @gs, $g[2] if grep { $u eq $_ } split " ", $g[3]
        }
        endgrent;
        print "@gs";
    ' michael
    1000 20 24 25 29 40 44 46 111 116

If, as I suspect, you just want all of the users that belong to a given
group:

> perl -wle 'print join " ", (getgrnam shift)[3]' powerdev
    michael root

Clearly the id command is simplest, but the Perl is the most portable.
If you shove the Perl into a separate script and just run it, perhaps
with switches to indicate if you want numbers or names, you can make
yourself a portable little id command.

The problem with scanning across /etc/group and /etc/passwd is that it
misses things. Not all of your authentication may come from those
files. Perhaps in your case it does, but cases change, and it's always
a pain to have to debug why some script is not seeing a given user.

--
Michael Fowler
www.shoebox.net
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Mon Oct 18 10:04:27 2010

This archive was generated by hypermail 2.1.8 : Mon Oct 18 2010 - 10:04:28 AKDT