Script to list primary group of users

Dear All

I am facing a problem with my script.
I have to found the primary group of users .

So first I selected all the groups and users register from a specific user : ONE
Then I am making a file with all groups attached to the user : ONE
Then I am making a file with all users from each group of that user : ONE Then for each user I have to found his primary group

Ex : Groups of USER ONE

GRP1 ::111 :ONE,TWO,THRE
GRP2 ::2848 :SIX ;ONE,SEVEN
and so one

Here is the script that i have writen :

#!/bin/ksh -p

touch fic_ListGRP_tmp.log
touch fic_ListUSR_tmp.log
touch fic_results_tmp.log

cat /etc/group | grep ONE | awk 'BEGIN{FS=":"}{print $1}' >> fic_ListGRP_tmp.log
for groupe in `cat fic_ListGRP_tmp.log` ;
do
cat /etc/group | grep ${groupe} | awk 'BEGIN{FS=":"} {print $4}' | sed 's/,/ /g' > fic_ListUSR_tmp.log
for user in `cat fic_ListUSR_tmp.log`
do
group_prim=`cat /etc/passwd | grep $user: | awk 'BEGIN{FS=":"}{print $4}' | head -1`
name_groupe=`cat /etc/group | grep ":$group_prim:" | awk 'BEGIN{FS=":"}{print $1}'`
echo "$user $group_prim $name_groupe" | tee -a >> fic_results_tmp.log

\rm ${fic_ListGRP_tmp.log}
\rm ${fic_ListUSR_tmp.log}



done
done


My problem is the number of users listed is by far beyond of the userlist.

I've got 151 users but at the end the list contain 298 entries .... I can't find what is wrong.

Thanks for your help.

I am not sure what you are trying to achieve. Why can't you use:

id -gn ONE
1 Like

Hello Scrutinizer,

thanks for your quick reply. I've to give to my boss all primary group of all users from all groups where the user ONE exist.

Exemple if the user ONE exist on the GRP1 and GRP2 I have to see if there some other users in GRP1 and GRP2 and if yes, what is the primary group of thoses users.

I found that the problem come from that specific line :

cat /etc/group | grep ${groupe} | awk 'BEGIN{FS=":"} {print $4}' | sed 's/,/ /g'

I found that there is some exact lines two or three times.

I need a specific pattern on result as I have to give an excel file.

Thanks for your help

First off, there are several syntactical problems in your script:

\rm ${fic_ListGRP_tmp.log}
\rm ${fic_ListUSR_tmp.log}

I don't know what this is supposed to do, but most probably it won't do it, whatever "it" is.

Second, i don't know which system you are working on. If it is some Linux or Linuxoid system you can simply get all the primary groups of all the users by reading field 4 in the file /etc/passwd. If you need the groups name just refer to /etc/group for a name-GID relation. See the code-snippet below:

cat /etc/passwd |\
cut -d':' -f1,4 |\
while read chUser chPGID ; do
     chPGIDName="$(grep ":${chPGID}:" /etc/group | cut -d':' -f1)"
     print - "User: $chUser \t Prim. Group: $chPGID \t Groups Name: $chPGIDName"
done

The reason why your code might have failed is not clear. One possibility is that you counted users more than one times: suppose user ONE has groups A, B, C, D, user B has groups "A, C, D". Per your algorithm you would find user account B three times and count it for three distinct users. To avoid this you will have to make your list with users unique by filtering out all the doubles, probably by filtering the fnal list through "sort -u" or something such.

I hope this helps.

bakunin

1 Like

@bakunin \rm is a way to bypass alias definitions, but the ${} is incorrect indeed.

1 Like

Dear bakunin,

thanks for your quick reply.

First :

\rm ${fic_ListGRP_tmp.log}
\rm ${fic_ListUSR_tmp.log}

your right I don't need those lines.

Then

I am on solaris 9. I also believed the line code below is not good as there is two or three times the exact same lines with the same users. This why my result log contains more users than I have if I am making manually the print screen of the command. I belived the for loop can't work as I need.

cat /etc/group | grep ${groupe} | awk 'BEGIN{FS=":"} {print $4}' | sed 's/,/ /g'

I hope you may be help me to make it right.

Thanks

---------- Post updated at 08:00 AM ---------- Previous update was at 07:13 AM ----------

Dear Friends,

thanks a lot for your help. I solved my problem. Sorry for making all of you searching to help me. I am so glad to have you for help.

My problem was my brain ; )

#!/bin/ksh -p

touch fic_ListUSR_tmp.log
touch fic_results_tmp.log

cat /etc/group | grep ONE | awk 'BEGIN{FS=":"} {print $4}' | sed 's/,/ /g' > fic_ListUSR_tmp.log
for user in `cat fic_ListUSR_tmp.log`
do
group_prim=`cat /etc/passwd | grep $user: | awk 'BEGIN{FS=":"}{print $4}' | head -1`
name_groupe=`cat /etc/group | grep ":$group_prim:" | awk 'BEGIN{FS=":"}{print $1}'`
echo "$user $group_prim $name_groupe" | tee -a >> fic_results_tmp.log


done

so here is the result as example :

user_name numeric_group_value group_name

thanks

# x=( $(cat /etc/group | grep ONE | sed 's/.*:\(.*\)/\1/;s/,/ /g;/^$/d') )
# for i in ${x[@]}; do echo $i; done > xgroups && sort -u xgroups | while read -r user ; do echo "$user's primary group -> `id -gn $user`" ; done
1 Like

Just for interest, here is a belt-and-braces approach which does not need workfiles.
Throughout it does an exact match on every lookup. It also deals with the problem of duplicates which can happen if another user appears more than once in the same secondary group list as the user "ONE".

username="ONE"
(
# Find all secondary groups for $username
groups -g "${username}" | tr " " "\n" | while read group_name
do
        # Find primary group of all members of each $group_name
        listusers -g "${group_name}" | awk '{print $1}'|while read member
        do
                # Look up $member in /etc/passwd and output username and group
                awk -F: '{if ($1 == member) {print $1,$4; exit}}' member=$member /etc/passwd
        done
done
# Eliminate duplicates
) | sort | uniq | while read username2 group_no2
do
        # Convert primary group number into group name
        group_name2="`awk -F: '{if ($3 == group) {print $1; exit}}' group=$group_no2 /etc/group`"
        echo "${username2} ${group_no2} ${group_name2}" > fic_results_tmp.log
done
1 Like

Dear methyl,

thanks a lot for your script. Thanks a lot for your help. Juste for info

The -g option doesn't work on my Solaris but groups command gave me the result anyway

groups

and

I used >> to get complete result.

>> fic_results_tmp.log

methyl, your great thank you very much for your help.