[CentOS] Shell script to list group members

Barry Brimer lists at brimer.org
Wed Sep 24 01:43:34 UTC 2008



On Tue, 23 Sep 2008, Bob Beers wrote:

> On Tue, Sep 23, 2008 at 7:26 PM, Barry Brimer <lists at brimer.org> wrote:
>> Quoting Tim Alberts <talberts at msiscales.com>:
>>
>>> Barry Brimer wrote:
>>>> With spaces separating groups:
>>>>
>>>> egrep -e '^groupname:' /etc/group | awk -F : '{ print $4 }' | sed -e 's/,/
>>> /g'
>>>>
>>>> With commas separating groups:
>>>>
>>>> egrep -e '^groupname:' /etc/group | awk -F : '{ print $4 }'
>>>
>>> I'm sorry, I didn't specify, I'm using LDAP for user/group management.
>>> Ideally a command like 'groups' would be nice, except it would be the
>>> inverse, it would print the users in a group, not the groups a user
>>> belongs to.
>>
>> With commas separating groups:
>>
>> getent group | egrep -i '^groupname:' | awk -F : '{ print $4}'
>>
>> With spaces separating groups:
>>
>> getent group | egrep -i '^groupname:' | awk -F : '{ print $4}' | sed -e 's/,/
>> /g'
>>
>
>
> ok, Barry wins. :)
>
> But, I don't think you need the egrep ...
>
> getent group groupname | ...
>
> works ok for me.

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.

Barry


More information about the CentOS mailing list