1834406 Members
2809 Online
110067 Solutions
New Discussion

group check ??

 
SOLVED
Go to solution
MikeL_4
Super Advisor

group check ??

Is there a way to get a count of the number of users assigned to a particular group within the /etc/group file ??
Trying to head off a possible problem of to many users on a line before having to split it up.
11 REPLIES 11
Steven E. Protter
Exalted Contributor

Re: group check ??

number=$(cat /etc/passwd | grep -i group_number | wc -l)

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
MikeL_4
Super Advisor

Re: group check ??

Need to get it more out of the group file entry in /etc/group. We don't put the login id into the default group in /etc/group.

What I was after is a way to count the login id's on a particular line within /etc/group.
Helen French
Honored Contributor
Solution

Re: group check ??

There might be more simple ways, but this will also be useful to get the result:

# cat /etc/group | grep $group_name | sed "s/,/ /g" | wc -w
Life is a promise, fulfill it!
Kent Ostby
Honored Contributor

Re: group check ??

Below is an awk script which you would run as follows:

awk -f groupcount.awk < /etc/group > outputfile

Create a file called groupcount.awk and put the following into it:

{idx1=index($0,":");
use1=substr($0,idx1+1);
groupname=substr($0,1,idx1-1);
idx2=index(use1,":");
use2=substr(use1,idx2+1);
idx3=index(use2,":");
use3=substr(use2,idx3+1);
sidx=idx1+idx2+idx3;
if (sidx==length($0))
{print " 0 "groupname;next;}
count=0;
for (idx4=1;idx4 { if (index(substr(use3,idx4),",")>0)
{idx4+=index(substr(use3,idx4),",") ;
count++}
else
{count++;idx4=length(use3);} }
printf ("%4d %s \n", count,groupname);}
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Michael Schulte zur Sur
Honored Contributor

Re: group check ??

Hi,

here comes another one.

greetings,

Michael
Jeff Schussele
Honored Contributor

Re: group check ??

Hi Mike,

Don't forget that this will not count ALL users within a group. Users that have that group as primary have that data in the /etc/passwd file. /etc/group will only count users that have that group as secondary.

My 2 cents,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Helen French
Honored Contributor

Re: group check ??

Jeff,

Correct me if I am wrong: I think the group file will have all users listed there evenif they are primary or secondary groups. I checked my systems and the users are listed under both their primary and secondary groups.
Life is a promise, fulfill it!
Jeff Schussele
Honored Contributor

Re: group check ??

Hi Shiju,

I believe you're seeing that because you run such a tight ship my friend.
Adding users via SAM or useradd do not - by default - add them to the /etc/group file.
What *does* add them to the group file is Modifying Secondary Group Membership via SAM or the -G of useradd.
System pulls your group ID solely out of /etc/passwd at login.

Cheers,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Helen French
Honored Contributor

Re: group check ??

Thanks Jeff. I was looking at the system created accounts (root, bin, sys etc) and found it on group files too (I thought that will be the standard). I never observed that from SAM since almost all User administration will be manual in my shop.
Life is a promise, fulfill it!
Bill Hassell
Honored Contributor

Re: group check ??

There was a change somewhere in version 8 to 9 where /etc/group no longer had to have the primary group listed. There was a bug in SAM that made it keep on adding users to a single line and after several hundred users were added, things broke because a line in /etc/group was thousands of characters long. The workaround was to manually split the lines until the login environment would automatically inherit the primary group (such as users). Still, for large alternate group membership, SAM would create a long line.

Then SAM was changed to add one user per line. Now it appears that SAM does an intelligent line, creating a duplicate group entry when the line gets too long. Technically, SAM isn't really doing the mods, it is /usr/lbin/usermod.sam so the /usr/sbin/usermod may do something similar.


Bill Hassell, sysadmin
Helen French
Honored Contributor

Re: group check ??

Thanks Bill. I remember the *character* limitation in /etc/group file on 10.20 systems. We failed creating new NIS maps cuz of this issue.
Life is a promise, fulfill it!