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 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 :
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:
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 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.
# 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
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