Link to home
Start Free TrialLog in
Avatar of gopikrish
gopikrish

asked on

Problem in Getting members of a group using "grep" command

I wanted to see the members of a particular group and a person suggested me to do
"grep neighbor /etc/group" where neighbor is the group name.But when I did that it showed "neighbor:*:205:"
 So it didn't show any names?
 He also said I can do in a script as follows...
 #!/bin/bash
# Pass group name as an argument.
echo -n "Members of $1: "
grep $1 /etc/group| cut -d: -f4
 
   When I executed the above script from my home directory using "bash scriptname" it asked for "Members of :" and when I entered group name as neighbor, nothing happened ?

  So any ideas why that problem is occuring please?

ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gopikrish
gopikrish

ASKER

No actually someone mentioned one cool command which listed all member names of a particular group but I forgot that :(
  It was said by a member from Grex shell account.So if anyone knows that please share it here.Thanks.
cut -d: -f1 /etc/passwd |xargs -n 1 id |grep neighbor
Line extracted from /etc/password:

angela:x:504:504::/home/angela:/bin/bash

This gives username:x means a shadowed password:userid:PRIMARY groupid::home directory:login shell

Line extracted from /etc/group:

mail:x:12:mail,postfix

This gives group name:x means shadowed password(man gpasswd):group id:comma seperated members of the group

postfix:x:89:

also exists in /etc/group

...but because it requires access to some files that mail owns, it is made a member of the mail group as well.

Traditionally, there were circumstances where all users were made members of the users group, and so the approach that you were using (for the neighbor group ) would have worked in thos circumstances. Your approach would ahve got:

users:x:500:user1,user2,user3,user4 etc

Nowadays, due to heightened security concerns etc, whenever a new person is added, they get created as a default a brand new group for each person. It therefore requires a concious effort to enable them to have the same group privileges as someone else.

HTH:)

I suppose that I'd better mention that this appears to be the default approach for RH 8.0, before someone starts flaming....and there is no reason why everyone shouldn't have group 500 in the /etc/passwd file, instead of the

users:x:500:user1,user2,user3,user4 etc

in /etc/group

I guess it depends on the distribution?
Um, are you looking for something like the AIX command "lsgroup" then? There is work in progress on something along those lines in the GNU sysutils project (http://savannah.gnu.org/projects/sysutils ... and look at http://savannah.gnu.org/cgi-bin/viewcvs/sysutils/sysutils/src/ for (alpha grade) source)...
One could probably make some more or less intelligent script to get hols of and list group memberships (both primary and secondary), to somewhat emulate this functionality:-).

-- Glenn
Well, "members groupname" as well as
"cat groupname /etc/passwd | grep :205"
where 205 is the group name id
They both worked ! No one was able to mention that :) Anyway thanks for suggestions.
That doesn't actually solve the problem of displaying all the members of a group though.

/etc/passwd  gives login group only
/etc/groups   lists only additional group memberships of users

cat groupname /etc/passwd...   isn't right, that should give an error message about the file
"groupname" not existing

grep ':205'  /etc/passwd    would list login groups, but you can also get spurious entries, for
example if a user has UID 205 or realname 205  they will be shown.

awk -F: '{if ($4=="205"){print $0}}' /etc/passwd

Will show you all passwd file entries where GID is 205, and no spurious entries
I see.So can you explain what it does please? I dont understand that and also why 4 is used in that $4 and 0 is used in $0 ? Thanks.
In awk, $4 is the 4th field in the line (record) and $0 is the whole line
JUst curious is:

members

a part of any specifc distribution. I've never seen it before. Having said that, we all have our own way of doing things and perhaps mine jut doesn't need the members command?
Hm, "members"... Not usually part of any install I've seen recently, but I have this very very faint recollection of seeing some odd shell builtin like that some time.... long long time ago:-).

-- Glenn