1

ASSUMPTIONS:

I have a file called users.txt

username:password:groups
user1:password:group1,group2,group3
user2:password:group3
user3:password:group1,group3
user4:password:group2,group3

CODE

#!/bin/bash
FILENAME="users.txt"

while IFS=':' read USERNAME PASSWORD GROUPS
do

    echo "USERNAME" $USERNAME "PASSWORD" $PASSWORD "GROUPS" $GROUPS

done < "$FILENAME"

I want to be able to add a bunch of users to be able to do a samba share. I have a full list of users to input. I am having difficulty mostly with the groups.

Two biggest questions are, how to I get the groups as one string and how do I encrypt the password.

I have been through all tutorials on this site and none of them are working.

Simple to understand code would really help. Thanks.

UPDATED FINISHED WORKING EXAMPLE

#!/bin/bash
# set filename
FILENAME="users.txt"

# loop through file 
while IFS=':' read USERNAME PASSWORD GROUPNAMES
do

    # add user and assign groups
    echo " "
    echo "ADDING USER: " $USERNAME
    useradd $USERNAME -G $GROUPNAMES

    # add password
    echo -e "$PASSWORD\n$PASSWORD\n" | passwd $USERNAME

    # add user to samba
    echo -e "$PASSWORD\n$PASSWORD\n" | smbpasswd -a $USERNAME

done < "$FILENAME"
2
  • Avoid using all uppercase variable, you might end up conflicting with built-in variables. Commented Jul 9, 2015 at 15:21
  • Thanks, I got it working, but I'll know that for future. Commented Jul 9, 2015 at 19:14

3 Answers 3

3

Don't use GROUPS as a variable name : it's a read-only variable already used by the system. Just change the name of this variable.

Sign up to request clarification or add additional context in comments.

7 Comments

Good point and a useful piece of advice but it's not an answer.
@Tom Fenech Could you explain me more please? I tested it, it makes the script working. Is it about the encryption question?
OH MY GOSH! I am so frustrated that it was that easy! This was the number one problem I had and couldn't get past it. THANK YOU!
Yeah, I still have to figure out the encryption part, but the rest of my code will be easy to figure out. I was mostly stuck on why GROUPS wouldn't work.
My apologies, I misinterpreted the question. It wasn't clear to me that the while read wasn't setting the variables correctly, I thought that they were asking how to use them. +1 from me.
|
0

I'm not sure about the password encryption part but I can give a suggestion about the groups. Inside of your loop, you could do this:

read -ra groups_array -d, <<<"$groups"

Now you have an array containing each group as an element. You can pass them as separate arguments to a command using the expansion "${groups_array[@]}" or access them individually using "${groups_array[0]}", "${groups_array[1]}", etc.

Note that I am using lowercase variable names - this is intentional. Uppercase names should be reserved for use by the shell, as otherwise you run the risk of overwriting important ones.

Comments

-1

Excellent script above: I used the code co create this working version.

#!/bin/bash

This method includes the bash code and data in just one file

Install this file, run it and remove it immediately for future use with a new server etc.

function setupUser()
{

first, remove old instance of user in both linux and samba

echo "remove user: " $1 smbpasswd -d $1 userdel $1

Now add new user in both linux and samba

echo "adding user: "$1 useradd $1 -G nogroup echo -e "$2\n$2\n" | passwd $1 echo -e "$2\n$2\n" | smbpasswd -a $1 }

#---------------------------------------------------------------------------

setupUser tom secret setupUser dick secret setupUser harry secret

1 Comment

please respect the SO style guide and change your formatting accordingly, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.