Outputting group members

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Outputting group members

Postby shifty_ben » Thu Apr 06, 2006 12:37 pm

Hi all,

Im currently writing a CLI frontend to manage my webserver, it has a few users on it that are all members of the group webhosting. What I seem to have come stuck on is building an offline 'database' of this, the reason database is in apostraphies is because it will jsut be a text file with the relevant details in.
I can't seem to find any way of running a command that will output all the users in a specific group, in this case webhosting.
Im using BASH for the rest of the frontend but if any suggestions are in other languages then I should be able to incorporate that.

Any ideas?

cheers
Ben
Need a New Signature
User avatar
shifty_ben
LXF regular
 
Posts: 1292
Joined: Tue Oct 04, 2005 9:56 am
Location: Ipswich

RE: Outputting group members

Postby nelz » Thu Apr 06, 2006 1:34 pm

If they are members of webhosting ass a secondary group, you can grep this information from /etc/group

Code: Select all
grep ^${GROUP} /etc/group | cut -d: -f4


To find those that are members of the group as their primary group, you'd have to convert the group name to a numeric ID then grep /etc/passwd.

Code: Select all
GID=$(grep ^${GROUP} /etc/group | cut -d: -f3)
egrep ".*?:.*?:.*?:${GID}:.*" /etc/passwd | cut -d: -f1


should do it, although some error checking might be a good idea.
Last edited by nelz on Thu Apr 06, 2006 6:52 pm, edited 1 time in total.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8525
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: Outputting group members

Postby shifty_ben » Thu Apr 06, 2006 2:01 pm

Excellent Thank you :D just what I was needing, they are all in there as a secondary group so that makes life much easier :D
Need a New Signature
User avatar
shifty_ben
LXF regular
 
Posts: 1292
Joined: Tue Oct 04, 2005 9:56 am
Location: Ipswich


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests