0

I want to write a script in CentOs 7 that creates a new user for every CSV file row.

My csv file looks like this:

username;firstname;lastname;password
username2;firstname2;lastname2;
...
...

First column is the username, second is the first name, third is the last name and last column stands for password. If there isn't a password, it creates a user with some default value.

My code:

#!/bin/bash 

export IFS=";" 

cat users.csv | while read username name surname password; do 
    useradd "$username" -c "${name} ${surname}" -p "$password"
done

How do I create users from each column?

9
  • Try something first, show some effort. For starters, read about read and while commands and how to read files with them. Commented Jan 13, 2018 at 17:28
  • I've already wrote this code Commented Jan 13, 2018 at 17:32
  • #!/bin/bash export IFS=";" cat users.csv | while read username name surname password; do useradd "$username" -c "${name} ${surname}" -p "$password" ; done Commented Jan 13, 2018 at 17:32
  • 1
    Don't post code in the comments, post it in the question. Commented Jan 13, 2018 at 17:32
  • it just creates users but I don't know how to set null columns into something else Commented Jan 13, 2018 at 17:33

1 Answer 1

1

Try this:

$ su - newuser
No passwd entry for user 'newuser' 

$ IFS=";" && echo "newuser;New;User;testpass" | while read username name surname password; do sudo useradd "$username" -c "${name} ${surname}" -p `openssl passwd -1 $password` -m; done

$ su - newuser
Password: 
newuser@my-desktop:~$ logout

$ cat /etc/passwd | grep newuser
newuser:x:1002:1002:New User:/home/newuser:
Sign up to request clarification or add additional context in comments.

Comments

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.