LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   simple script... take users from a group and add in another group (https://www.linuxquestions.org/questions/linux-general-1/simple-script-take-users-from-a-group-and-add-in-another-group-4175535839/)

UNIX Zyklon 03-05-2015 08:21 AM

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

T3RM1NVT0R 03-05-2015 08:31 AM

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.

UNIX Zyklon 03-05-2015 08:36 AM

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?

T3RM1NVT0R 03-05-2015 10:29 AM

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.

UNIX Zyklon 03-05-2015 12:00 PM

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


ntubski 03-05-2015 12:08 PM

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


T3RM1NVT0R 03-05-2015 12:55 PM

Nice!! You're welcome. Please mark the thread as solved.


All times are GMT -5. The time now is 08:03 AM.