Script to list primary group of users


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to list primary group of users
# 1  
Old 10-12-2010
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 :


Code:
#!/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.
# 2  
Old 10-12-2010
I am not sure what you are trying to achieve. Why can't you use:
Code:
id -gn ONE

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-12-2010
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 :

Code:
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
# 4  
Old 10-12-2010
First off, there are several syntactical problems in your script:

Code:
\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:

Code:
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
This User Gave Thanks to bakunin For This Post:
# 5  
Old 10-12-2010
@bakunin \rm is a way to bypass alias definitions, but the ${} is incorrect indeed.
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-12-2010
Dear bakunin,

thanks for your quick reply.

First :

Code:
\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.

Code:
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 ; )

Code:
#!/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
# 7  
Old 10-12-2010
Code:
# 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

This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Creating a group of users with script

Hi, I have a file with usernames, and the comment section, e.g : Data removed by request of sanchitadutta91, 20 May 2020 I need to add these users into a server. Is it possible to use a script to create the users, together with the comment ? From the commandline to add one user, the... (2 Replies)
Discussion started by: anaigini45
2 Replies

2. UNIX for Beginners Questions & Answers

Primary group

Is it possible user without a primary group (3 Replies)
Discussion started by: lobsang
3 Replies

3. Shell Programming and Scripting

Script to add new users to a group on multiple servers using SSH

Hi Experts, I am new to scripting. We have around 400 Linux servers in our environment. I want to add a new user to a perticular group on all the servers using SSH. Requirements: 1) Need to take the server names from a text file. 2) Login into each server and check whether perticular... (1 Reply)
Discussion started by: Satya1983
1 Replies

4. Ubuntu

primary group for user

HI I need to know what is the primary group name of a particular user. How to do this ? Maybe with groups cmd ? (first group name in line, is the primary group) thx for help. (2 Replies)
Discussion started by: presul
2 Replies

5. AIX

script for finding all the users with GID 0 ( admin group )

Hi Friends, I am trying to write a script for finding all the users with the GID 0 i.e. Admin users. can you please help me on this. (1 Reply)
Discussion started by: anoopraok
1 Replies

6. Shell Programming and Scripting

List ALL users in a Unix Group (Primary and Secondary)

Is there a command or better combination of cmds that will give me the list of Unix users in a particular Unix group whether their primary group is that group in question (information stored in /etc/passwd) or they are in a secondary group (information stored in /etc/group). So far all I got... (5 Replies)
Discussion started by: ckmehta
5 Replies

7. UNIX for Advanced & Expert Users

Change a users primary group after login

When users login, they are directed to menu (aix script). The menu enables the user to choose an environment to work in. Each environment has a different group id. When a user chooses a menu option, I want to change his primary group to that specific environment's group id. Is this at all possible... (3 Replies)
Discussion started by: terrym
3 Replies

8. Solaris

How do you list users in a solaris group

I need to list all users in a group. This is a large unix site running nis+. (6 Replies)
Discussion started by: gillbates
6 Replies

9. UNIX for Dummies Questions & Answers

command help, how do i list the users of a group?

What command allows you to display a list of the userids of all the other users in a group, regardless if they are logged in or not? (3 Replies)
Discussion started by: crabtruck
3 Replies

10. UNIX for Dummies Questions & Answers

How to find All Primary and Secondary Group ID's for a user

Is there any command which can list me all the Group ID's (Primary, Secondary ) assocaited with a single user. Thanks Sanjay (2 Replies)
Discussion started by: sanjay92
2 Replies
Login or Register to Ask a Question