FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 09-23-2008, 11:37 PM
Ian Blackwell
 
Default Shell script to list group members

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.

Cheers,

Ian
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2008, 01:43 AM
Barry Brimer
 
Default Shell script to list group members

On Tue, 23 Sep 2008, Bob Beers wrote:


On Tue, Sep 23, 2008 at 7:26 PM, Barry Brimer <lists@brimer.org> wrote:

Quoting Tim Alberts <talberts@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
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2008, 04:52 AM
MHR
 
Default Shell script to list group members

On Tue, Sep 23, 2008 at 6:43 PM, Barry Brimer <lists@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.
>

Grep understands the '^', so egrep is not needed. Typically, you only
need egrep for patterns that involve alternative re's, like looking
for one of "abc|def|ghi" which grep does not recognize.

Also, it might be helpful to trim your replies so we don't need to
wade through the whole thread to see your reply.

mhr
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2008, 12:54 PM
"Bob Beers"
 
Default Shell script to list group members

Part 1:

On Tue, Sep 23, 2008 at 7:37 PM, Ian Blackwell <ian@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@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
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-24-2008, 11:19 PM
Ian Blackwell
 
Default Shell script to list group members

Bob Beers wrote:
> Part 1:
>
>
> 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."
>
>
In all likelihood the system follows the default approach of setting the
primary group to be the user's personal ground. If that is the case
then you're correct in providing a simple solution as requested. I just
wanted to make Tim aware that if his user's have primary groups other
than their personal groups - e.g. "admin" or "marketing" - then there
isn't a simple answer (not that the answer is all that hard).

Here's a script I knocked up to do it - although there can be
duplication and output formatting isn't perfect:-

#!/bin/bash
#set -x
# $1 is the group to test
if [ "$1" = "" ]; then
echo "Which group?"
exit 1
fi
groupid=$(getent group $1 | cut -d: -f3)
grouplst=$(getent group $1 | cut -d: -f4)
for User in $(cat /etc/passwd | cut -f1 -d
do
if [ $(id -g $User) = $groupid ]; then
grouplst="$(echo $grouplst),$User"
fi
done
echo "Members of group $1 are: $grouplst"
exit 0

Regards,

Ian
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 

Thread Tools




All times are GMT. The time now is 06:29 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org