LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-29-2009, 02:07 PM   #1
mjhermanso21
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Rep: Reputation: 0
user group shell script


I need a shell script that will print all users that belong to over 10 groups.

Currently I am getting the usernames from getent passwd, using awk get get the usernames, then running groups on each of the usernames. I have not figured out how I will count the groups yet but here is what I have so far.

Code:
#!/bin/sh

getent passwd > passwd.out

awk -F":" '{ print $1 }' ./passwd.out > users.out

cat users.out |while read line; do groups "${line}"; done

exit
This prints all the users and their groups, just very slowly. Also if I could do it without sending it to a file it should speed up.

Thanks
 
Old 07-29-2009, 02:29 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
no file and a trick with IFS and read
any better?

Code:
 
(
IFS=:
getent passwd  | 
while read u rest
do 
   echo -n $u:
   id -Gn $u
done
)
 
Old 07-29-2009, 02:35 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello mjhermanso21

Code:
for user in $( awk -F":" '{ print $1 }' < /etc/passwd )
do
	[[ $( groups $user | wc -w ) -gt 9 ]] && echo $user
done
Best

Charles
 
Old 07-29-2009, 02:43 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Inspired by bigearsbilly's techniques ...
Code:
IFS=':'
while read user rest
do
	[[ $(id -Gn $user | wc -w) -gt 10 ]] && echo $user
done < /etc/passwd
Edit: that leaves IFS set to : which doesn't matter if it's the end of the script. If it does matter you can get around it either by bigearsbilly's technique of running it in a subshell with (<stuff>) or by unsetting IFS afterwards:
Code:
IFS=':'
while read user rest
do
	[[ $(id -Gn $user | wc -w) -gt 10 ]] && echo $user
done < /etc/passwd
unset IFS

Last edited by catkin; 07-29-2009 at 02:47 PM. Reason: Forgot to reset IFS
 
Old 07-29-2009, 03:38 PM   #5
mjhermanso21
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks both of you. My only edit, which I didn't mention in my original post, was i wanted it to print like "user: groups...." so my final script was

#!/bin/sh

for user in $( awk -F":" '{ print $1 }' < ./passwd.out )
do
[[ $( groups $user | wc -w ) -gt 9 ]] && groups $user
done

*also we used LDAP not /etc/passwd so I am using a tmp file that has passwd info in it.
 
Old 07-30-2009, 03:09 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by mjhermanso21 View Post
Thanks both of you. My only edit, which I didn't mention in my original post, was i wanted it to print like "user: groups...." so my final script was

#!/bin/sh

for user in $( awk -F":" '{ print $1 }' < ./passwd.out )
do
[[ $( groups $user | wc -w ) -gt 9 ]] && groups $user
done

*also we used LDAP not /etc/passwd so I am using a tmp file that has passwd info in it.
That's mostly my version 0.1, before I smartened it up using bigearsbilly's ideas. More elegant would be
Code:
<command to write LDAP stuff to stdout> | while IFS=':' read user rest
do
        array=($(id -Gn $user)) 
	[[ ${#array[*]} -gt 10 ]] && echo ${array[*]}
done
As I learned in this LQ post a few days ago (thanks ntubski ), that idiom leaves the original IFS untouched saving the need for a subshell or for saving+restoring/unsetting IFS.
 
Old 07-30-2009, 10:34 AM   #7
mjhermanso21
LQ Newbie
 
Registered: Jun 2007
Posts: 7

Original Poster
Rep: Reputation: 0
very nice. and more efficient too!
 
Old 08-02-2009, 07:22 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by mjhermanso21 View Post
very nice. and more efficient too!
If the problem is solved, please use Thread Tools to mark it so.
 
  


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 to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
bash script user group folder zerocool22 Programming 6 05-27-2008 06:48 AM
Perl Script to add user to a group AgentRn007 Programming 4 06-21-2006 09:20 PM
Shell script that changes user davholla Linux - General 2 03-23-2004 09:18 PM
shell script user not ok slam Linux - General 4 07-24-2003 06:40 AM

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

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