LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2005, 11:52 PM   #1
quack322
LQ Newbie
 
Registered: Jun 2004
Distribution: Redhat 9
Posts: 11

Rep: Reputation: 0
Script - Find primary group


I need to create a shell script that can take a user name put in
eg.
Code:
READ THEUSERNAME
and have it automatically find that users primary group. I have tried to use the groups command in my shell scripts but it returns all the users groups, when I must use only the primary group for the rest of my shell script. Also this cannot be their GID but it must be the name of the primary group their in.
 
Old 01-02-2005, 12:52 AM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
You can still use the 'groups' command and just use awk with sed to grab only the first (primary group) that it displays. Anyhow, you can play around with that. Instead I used the cut command. The script below will:

1. Display the GID (which I know you are not interested but hey, in case you want to see it)
2. Take the GID and compare that with the /etc/group file to grab the primary group
3. Then displays the Primary Group how you wanted.

As you can see, it's pretty straight forward. If you have any questions, let me know. Have fun!

-twantrd



#!/bin/bash

PASSFILE="/etc/passwd"
GRPFILE="/etc/group"

echo "Enter the username that you want to look up the Primary Group for: "
read input
gid=`grep $input $PASSFILE | cut -d : -f 4`
echo "GID is: $gid"
pri_grp=`grep $gid $GRPFILE | cut -d : -f 1`
echo "Primary Group is: $pri_grp"

exit 0
 
Old 01-02-2005, 12:54 AM   #3
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
You need id command only:
Code:
id -g -n USER-NAME
 
Old 01-02-2005, 04:13 AM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Or that will work too! . Didn't know about that one hehe...

-twantrd
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change a user's primary group? Akhran Linux - Newbie 4 03-19-2010 08:54 PM
Samba and Windows Primary Group saylestock Linux - Networking 3 05-04-2004 12:44 PM
Primary and secondary group trophy Linux - Newbie 2 04-03-2004 12:30 AM
Block 0 in primary superblock/group descriptor area bad ZilverZtream Linux - Hardware 0 08-10-2003 05:34 AM
What is a Primary Group? ejtbrown Linux - General 2 03-21-2001 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration