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 03-05-2015, 08:21 AM   #1
UNIX Zyklon
LQ Newbie
 
Registered: Jan 2010
Posts: 17

Rep: Reputation: 1
simple script... take users from a group and add in another group


hello all

im very bad at making scripts, all i need to do is take all users from a ldap group, and add in a local group, but im not sure how to make it


getent group |awk -F':' '/groupA/{print $4}'
user1,user2,user3,user4,user5

now i need to add it all to groupB
 
Old 03-05-2015, 08:31 AM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Code:
getent group |awk -F':' '/groupA/{print $4}'
user1,user2,user3,user4,user5
Is the above section working? I mean are you able to get the desired output? If yes, then I would suggest redirecting the output to a file.

Code:
getent group |awk -F':' '/groupA/{print $4}' > /tmp/userlist-groupA
for i in `cat /tmp/userlist-groupA`
do
{
usermod -a -G <group_name> $i
}
done
If you want to add the new group as primary group then just replace -G with -g. Also, -a is for appending to the existing group list.
 
1 members found this post helpful.
Old 03-05-2015, 08:36 AM   #3
UNIX Zyklon
LQ Newbie
 
Registered: Jan 2010
Posts: 17

Original Poster
Rep: Reputation: 1
hello

thx for the reply, this almost work, just one thing need to be fixed

getent group |awk -F':' '/groupA/{print $4}' > /tmp/userlist-groupA

the users are listed in the file as user1,user2.user3,etc

how can i list them like this

user1
user2
user3
etc?
 
Old 03-05-2015, 10:29 AM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Ah that is really irritating stuff. I have come up with a work around but don't know that is the most efficient way to do it or not. Here you go:

Code:
for i in {1..100}
do
{
getent group |awk -F':' '/groupA/{print $4}' | cut -d "," -f $i > /tmp/userlist-groupA
if [ $? -eq 0 ]
then
{
echo "Moving to next lot"
}
else
{
i=100
}
done
The logic behind this is I am running a loop from 1 to 100. Since it the output is in paragraph I am assuming you wont cross 100 but if you are worried you can put it to run for 500 times. The next thing I am doing is to cut the output and the -f 1 will contain number of users but not paragraph but one user per line . Next thing I am checking if the exit code is 0 if exit code is non-zero then we know we have reached end and the loop should exit and that is the reason I am changing the value to 100.

Hope it works for you.
 
1 members found this post helpful.
Old 03-05-2015, 12:00 PM   #5
UNIX Zyklon
LQ Newbie
 
Registered: Jan 2010
Posts: 17

Original Poster
Rep: Reputation: 1
thanks a lot for the help!

i figured out another solution for it as well, i tested and it worked

Code:
#/bin/bash
getent group |awk -F':' '/GroupA/{print $4}' > /tmp/users_list
 sed 's/,/\n/g'  /tmp//tmp/users_list > /tmp/users_list2

for i in `cat /tmp/users_list2`
do
{
usermod -a -G group $i
}
done

rm -f /tmp/users_list2
rm -f /tmp/users_list
 
1 members found this post helpful.
Old 03-05-2015, 12:08 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,612

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Could pipe to while read to avoid temp files:
Code:
#!/bin/bash

# UNTESTED

getent group | awk -F':' '/GroupA/{print $4}' | sed 's/,/\n/g' | while read i ; do
    usermod -a -G group "$i"
done
 
1 members found this post helpful.
Old 03-05-2015, 12:55 PM   #7
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Nice!! You're welcome. Please mark the thread as solved.
 
1 members found this post helpful.
  


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
Can I add a group of users to the wheel group? Teleute Linux - Security 7 05-21-2010 09:55 PM
Script to Auto-add Domain Users to Workstation Power Users Group doesn't work avinash.rao Ubuntu 0 08-22-2009 04:57 AM
Script to add a secondary group to all users except system default users Tekken Linux - Server 5 06-29-2009 04:02 PM
group: add complete group into other group max_mad SUSE / openSUSE 1 04-12-2006 01:43 AM

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

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