LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-02-2011, 05:05 PM   #1
xodustrance
Member
 
Registered: Jun 2003
Distribution: Gentoo baby, gentoo
Posts: 148

Rep: Reputation: 15
grep groups, gid, UID, etc -


Maybe I am going about this the wrong way. I need to be able to grab a users group ID's to set a model after account.

So if I do:

id userid

I get the following line -
uid=1001(userid) gid=2000(groupname1) groups=2000(groupname2),1000(groupname3),1500(groupname4)

How could I get just the groups ID's returned?

Obviously I start out with
id userid|

but pipe to what? Grep returns the whole line, so it looks like I need to use AWK or SED -

Could someone provide an example on how I could get the following output:
2000
1000
1500

I need the group ID's only so I can place them in some variable to use on a user creation script. So basically get the model after's name, and then output each group as a seperate line...

Any idea on how, or even better, could someone provide an example? my brain hurts from the sed and awk tutorials and I just dont think I can get it with out a working example.

Thanks for any help you can provide.
 
Old 11-02-2011, 09:56 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Buster (Fluxbox WM)
Posts: 1,390
Blog Entries: 52

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
The id command can be set to only output the groups, ie:
Code:
id -G userid
If you need separate lines, you can just use tr:
Code:
id -G userid | tr ' ' '\n'
You can use grep for simple pattern outputs, using the '-o' flag to restrict the output to the matches, as the following example shows (this makes assumptions such as group names not having embedded numbers, so isn't foolproof):
Code:
id userid | grep -o 'groups.*' | grep -o '[0-9]*'
If you are going to put time into learning a text oriented scripting language, consider learning perl instead of awk; it isn't as sleek as sed, but has a wider range of applications.

Last edited by neonsignal; 11-02-2011 at 10:12 PM.
 
Old 11-03-2011, 06:56 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,846

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
bash also sets a GROUPS array variable, holding the current user's group membership, if that's all you need.

Code:
echo "${GROUPS[@]}"
An array can also be used to hold and manipulate the output values of id, as opposed to working with tr or whatnot to change the delimiter. You can use the IFS setting to decide how the list is delimited when printed if you want (when using the ${array[*]} form).

Code:
ids=( $( id -G username ) )

IFS=$'\n'	#to delimit the list with newlines
echo "${ids[*]}"
 
1 members found this post helpful.
Old 11-03-2011, 03:25 PM   #4
xodustrance
Member
 
Registered: Jun 2003
Distribution: Gentoo baby, gentoo
Posts: 148

Original Poster
Rep: Reputation: 15
Wow neon - thanks a mil! that is exactly what I needed to get my output right.

David - your solution seems what I want going down the road, the right way.

I got my script done thanks to neon, but I would really like to work on Dave's solution and post up what my example should look like.

Off to hit the tutorials on bash arrays
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Differences between UID 0 and GID 0? dwarf007 Linux - Newbie 6 03-16-2010 04:52 AM
what is uid and gid rmanocha Linux - Software 9 08-18-2008 11:03 PM
uid,gid from proc sanjaykhuntia Linux - Enterprise 1 10-28-2007 07:50 PM
cifs not using uid and gid mattd7591 Linux - General 10 07-19-2007 11:30 AM
What is my uid/gid? Jeebizz Slackware 2 11-22-2005 11:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:26 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