Didn’t know it’s not so easy to list group’s members before I read this thread. A C program, lsgrp, is written and a reason for such program:
A user’s primary group is only listed in the passwd file, so to get a full list of group members, you have to check both the passwd file and the group file. The usual approach is to parse these files using grep, sed, cut, etc. That requires the invocation of subshells and it is prone to error, especially in cases where the set of user names intersects with the set of group names.
(Its source code is listed in the archives/, it is not an ArchLinux-specfic program, just run GCC to compile it.)
Let me explain how to find out the users in a group. First, you look up in /etc/group for group ID by the group name. There is a list of usernames at the end of each group, a comma-separated list. Then, you need to look up in /etc/passwd for users who has that group ID listed as their primary group.
The problem here is the users list in each group in /etc/group may not include users whose primary group is that group. So, you need to look into /etc/passwd for completely list, not just the list in /etc/group.
I wrote this function as alternative:
lsgrp() { read GID USERS <<< "$(grep "^$1:" /etc/group | cut -d: -f3,4 | tr ':,' ' ')" echo -e "${USERS// /\n}" | egrep -v "^($1)?$" egrep :[0-9]+:$GID: /etc/passwd | cut -d: -f1 }
There is also a command lid mentioned on the Internet, but I can’t find it on my system nor the source code or project page. I have compared the outputs of my function and lsgrp with groups on my system, there are the same except the order.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.