LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2005, 11:47 AM   #1
cookie_ie
LQ Newbie
 
Registered: Mar 2005
Posts: 13

Rep: Reputation: 0
number of users in a group


I know i have been asking loads but need to get this stuff done and up on my server soon. Is there a way to display the number of users in a group?on its own?i know who -q counts the users but it lists the names aswell, can I remove this just to show the number using script?
 
Old 03-24-2005, 12:11 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
If you know the group id, something like
Code:
cat /etc/passwd | grep "502:" | wc -l
will tell you the count of users in that group (502 given as example). You could also parse the group file first, to get the #, and then plug that into the previous example.. assuming that they will pass the group name into a script, something like:
Code:
#!/bin/bash

groupid=`cat /etc/group | grep "${1}:" | awk -F ':' '{print $3;}'`
cat /etc/passwd | grep "${groupid}:" | wc -l
Probably not the most effective/stream lined way to do this, but it works. One flaw of this method is that if the user id matches or ends with the group ID returned, it will call it a match. But at least it's a start. (=
 
Old 03-24-2005, 12:28 PM   #3
cookie_ie
LQ Newbie
 
Registered: Mar 2005
Posts: 13

Original Poster
Rep: Reputation: 0
my bad

I want to show the number of users logged on at one time and the number of users in the same group logged on, is all this possible?
 
Old 03-24-2005, 12:32 PM   #4
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 53
you can use the "id -G" command on each user detected by "who" and count the matches...

Last edited by gbonvehi; 03-24-2005 at 12:34 PM.
 
Old 03-24-2005, 12:57 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
logins -g groupname | wc -l
Not sure if linux has the logins command, but this works on Solaris.
 
Old 03-24-2005, 01:12 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally posted by TheLinuxDuck
Code:
cat /etc/passwd | grep "502:" | wc -l
That has an underlying assumption in it... Specifically, that the user will only be part of a single group; their initial/login group.

If the script/tool is supposed to count all users logged in that belong to a specific group (as an example: "developer"), then there's the possibility that a user belonging to groups "users" and "developer" will be missed (if that user's primary group is "user").

Getting a true count would probably require listing the users on the system, checking the group entry in /etc/passwd, and also looking for that user's entries in /etc/group.
 
Old 03-24-2005, 01:55 PM   #7
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Quote:
Originally posted by Dark_Helmet
That has an underlying assumption in it... Specifically, that the user will only be part of a single group; their initial/login group.
yes.. I was just feeling lazy and didn't want to parse /etc/group, too. I'm sure that my method is probably not even close to the best way to handle it.. it was merely the first idea that came to mind.. but props for calling it out. (=
 
Old 03-24-2005, 03:43 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: my bad

Quote:
Originally posted by cookie_ie
I want to show the number of users logged on at one time
Just the command "who" will give you all users currently logged in. But it will print the same user more than once if (s)he is logged in more than once.

If you want unique logged in user:
Code:
who | cut -d' ' -f1 | sort | uniq
Add the "-c" option to "uniq" to count the number of times each user is logged in:
Code:
who | cut -d' ' -f1 | sort | uniq -c

Quote:
[...] and the number of users in the same group logged on, is all this possible?
Here's a long one-liner that does this:
Code:
who | cut -d' ' -f1 | sort | uniq | xargs groups | sed -e 's/.*: \(.*\)/\1/' -e 's/ /\n/g' | sort | uniq -c
It prints something like:
Code:
      1 audio
      1 cdrom
      1 dialout
      2 disk
      1 heiko
      1 video
      1 weg
Which means there are 2 user logged in who are member of the group "disk". this is for unique usernames, i.e. if "heiko' is member of "dialout", and "heiko" is logged in 5 times, it will only print "1 dailout". I you want it to print "5 dailout" in this case, remove the first "sort | uniq":
Code:
who | cut -d' ' -f1 | xargs groups | sed -e 's/.*: \(.*\)/\1/' -e 's/ /\n/g' | sort | uniq -c
Which printed in the same situation on my computer:
Code:
      5 audio
      5 cdrom
      5 dialout
      6 disk
      5 heiko
      5 video
      1 weg
Note: All commands above do not count/print user sessions started with the "su" command.

Last edited by Hko; 03-24-2005 at 03:49 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
number of users edgjerp Linux - Software 6 11-04-2005 02:34 AM
/etc/group - the group users empty Artanicus Linux - General 2 02-22-2005 04:25 AM
get users in group hindrik Linux - General 2 08-12-2004 08:05 AM
number of users? Rick Winters Debian 5 06-21-2004 10:22 AM
LVM Problem - vgimport - wrong number of physical volumes to import volume group Anything1 Linux - Software 0 09-06-2003 07:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration