Find a users group (The smart way?).

Find a users group (The smart way?).

Post by Tony Higgin » Wed, 27 Sep 2000 04:00:00



Hi,
I am trying to figure out the smartest way of getting a users group. I
have thought of several ways:

1) ID - then do some sed / awk to pick it out.
2)  /usr/xpg4/bin/id -g outputs group id not group name and may not be
supported on all platforms.
      Then I guess you get name from /etc/groups.
3) /bin/groups - again with awk (pick out first group (primary).

This needs to be under sh on all platforms, opinions to the best method
or a better one would be appreciate

--
Tony

 
 
 

Find a users group (The smart way?).

Post by Ken Pizzi » Thu, 28 Sep 2000 04:00:00


On Tue, 26 Sep 2000 11:08:07 -0700,


>I am trying to figure out the smartest way of getting a users group. I
>have thought of several ways:

>1) ID - then do some sed / awk to pick it out.
>2)  /usr/xpg4/bin/id -g outputs group id not group name and may not be
>supported on all platforms.
>      Then I guess you get name from /etc/groups.
>3) /bin/groups - again with awk (pick out first group (primary).

For a typical, reasonably modern, Linux system, you can get away with:
   id -gn

Quote:>This needs to be under sh on all platforms, opinions to the best method
>or a better one would be appreciate

On _all_ platforms?  That becomes rather challenging, because in
that case you can't count on "id" or "/bin/groups" being
available, and furthermore you need to worry about NIS maps and
other not-in-a-file-in-/etc methods of storing user information.

For your typical v7 unix system, a decent way would be:
   user=`who am i | awk '{print $1}'`
   gid=`awk -F: "\\\$1 == \"$user\" { print \\\$4 }" /etc/passwd`
   awk -F: "\$3 == $gid { print \$1 }" /etc/group
but that's only because v7 unix didn't have variations on where the
user account information was stored.

                --Ken Pizzini