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
# 8  
Old 10-12-2010
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".

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

This User Gave Thanks to methyl For This Post:
# 9  
Old 10-13-2010
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

Code:
groups

and

I used >> to get complete result.

Code:
>> fic_results_tmp.log

methyl, your great thank you very much for your help.
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