[CentOS] Shell script to list group members
Bob Beers
bob.beers at gmail.com
Wed Sep 24 12:54:49 UTC 2008
Part 1:
On Tue, Sep 23, 2008 at 7:37 PM, Ian Blackwell <ian at ikel.id.au> wrote:
> Bob Beers wrote:
>> grep <group_name>: /etc/group | cut -d: -f4
>>
>> will give a comma separated list, provided <group_name> is a valid group name.
>>
>>
> There is one problem with this approach, which is the assumption that
> all users' primary group is the same as their login id - which I agree
> is typically the RHEL way, but it doesn't have to be the case. If
> however you have users with their primary group set to something other
> than the login id - e.g. "admin" or "marketing" - then you need to look
> in the /etc/passwd file as well because these users don't appear in the
> comma separated list outlined above. To check the /etc/passwd file, you
> have to determine the group id value, and then scan the /etc/passwd file
> looking for that value in column 4. This will give you a list of users
> whose primary group is the group value you're interested in.
You have a valid point, but the OP's question was:
"I am looking for a (simple) shell command to run from a bash script
that will allow me to list user accounts that belong to a particular
group."
Part 2:
On Tue, Sep 23, 2008 at 6:43 PM, Barry Brimer <lists at brimer.org> wrote:
>
> The egrep is using a leading anchor (^) to make sure the grep matches the
> beginning of the line. If not, and the group pattern matched as one of the
> users it would print those lines too .. which is probably undesirable.
>
My instinct is that by specifying the groupname as an argument as in:
'getent group groupname',
( rather than asking for all groups with 'getent group', and then
(e)grep'ing, )
that the result would not match for users in the groups list.
But I may be wrong. I have not looked at the source code.
But I tested on my system and I did not see the behavior you
warn of. If I am correct about the getent program, then there
is also the added benefit of avoiding the pipe.
:-)
-Bob
More information about the CentOS
mailing list