[CentOS] Shell script to list group members

Wed Sep 24 23:19:14 UTC 2008
Ian Blackwell <ian at ikel.id.au>

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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3617 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.centos.org/pipermail/centos/attachments/20080925/33030434/attachment-0005.bin>