Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group command 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0

How would I find the groups for 4 users? I can type in the 'group' commandfor myself but want to build a script that will give me each persons name and all the groups they are in.

I am on a Solaris 7 os.
 
I meant to say I used the 'groups' command which works but I want to write a script to get several users names and their groups.

Do I need root priviledge for this?
 
Here's one way.

#!/usr/bin/ksh

USER_ID=$1

echo "$USER_ID belongs to the following groups:"
grep $USER_ID /etc/group|while read LINE
do
echo $LINE|awk -F: '{print $1}'
done

example

# ./groupls.ksh root
root belongs to the following groups:
root
other
bin
sys
adm
daemon
mail
lp
users


Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
Here's another version that will list the group
for every user on the system.

Robert

#!/usr/bin/ksh

cat /etc/passwd|awk -F: '{print $1}'|while read USER_ID
do
echo "-----------------------------------------"
echo "[$USER_ID] belongs to the following groups:"
grep $USER_ID /etc/group|while read LINE
do
echo $LINE|awk -F: '{print $1}'
done
done
Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
Hi trapr99,

this small script will do what you need:

#!/bin/ksh

$USERS=`cat /etc/passwd | cut -d: -f1`

for user in $USERS
do
echo "$user is in the following groups :`groups $user`"
done

exit 0

mrjazz [pc2]
 
Thanks but I need it to list everyones name and the groups they are in after running script once. I tried something like this but cant get it to work:


[tt]
names=Smith Jones Rivers

for x in $check
do
groups $names
done [/tt]



Output should be:

Smith belongs to following groups
root
bin
users

Jones belongs to following groups
root
bin

Rivers belongs to following groups
root
bin
users

 
Thanks you guys are quick. It works now. Please ignore my last response. Your anwers came so quick I didnt realize they were there.

Thanks again!
 
#!/usr/bin/ksh

cat /etc/passwd|while read LINE
do
USER_ID=`echo $LINE|awk -F: '{print $1}'`
REAL_NAME=`echo $LINE|awk -F: '{print $5}'|awk -F, '{print $1}'`
echo "-----------------------------------------"
if [ ${#REAL_NAME} -eq 0 ]
then
echo "$USER_ID belongs to the following groups:"
else
echo "$USER_ID [$REAL_NAME] belongs to the following groups:"
fi
groups $USER_ID
done
Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
So how could this be changed to give the output below? What is the fastest way to use awk to do it? I appreciate it.

Root Group Members:
root
John Smith
Jack Johnson
Sue Z. Que
Jane Doe
Robert Johnson

sendmail Group member:
root
John Smith
Ace Williams
Jennifer Smith
John Henry
 
Hmm... this is not trivial! I think this does what you want. You also have to consider:
[ul][li]Users in /etc/group who are not in /etc/passwd
[li]Users' primary group ID in /etc/passwd. They are not necessarily also listed in the /etc/group file beside their primary group.
[li]Not listing users twice if they are listed beside their primary group in /etc/group.
[li]Blank GECOS (a.k.a. Comment) fields.[/ul]

Perhaps I should have done this in PERL!

Code:
#!/bin/ksh
nawk -F: '
        BEGIN {
                # Populate the GECOS and primary
                # group ID arrays.
                while (getline < &quot;/etc/passwd&quot;) {
                        if ($5 == &quot;&quot;) {
                                GECOS[$1]=$1
                        } else {
                                GECOS[$1]=$5
                        }
                        pgid[$1]=$4
                }
                close &quot;/etc/passwd&quot;
        }

        function printuser(user) {
                if (user in GECOS) {
                        if (GECOS[user] == &quot;&quot;) {
                                # Handle /etc/passwd entries with blank
                                # GECOS field.
                                print &quot;    &quot; user
                        } else {
                                print &quot;    &quot; GECOS[user]
                        }
                } else {
                        print &quot;    WARNING: User \&quot;&quot; members[i] &quot;\&quot; does not exist in /etc/passwd.&quot;
                }
        }

        {
                gid=$3
                print $1 &quot; group members:&quot;

                split($4,members,&quot;,&quot;)

                # Print users for whom this is the primary group.
                for (user in GECOS) {
                        if (! (user in members) && pgid[user] == gid) {
                                printuser(user)
                        }
                }

                # Print users for whom this is a supplementary group.
                for (i in members) {
                        printuser(members[i])
                }
                print &quot;&quot;
        }
' /etc/group

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top