ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.