0

I have an interactive bash script which first asks for user name and THEN ask for password and then confirm password again. (It creates users in our ERP system NOT in Ubuntu)

I have an CSV of 1000+ users and randomly generated passwords so I have to create all these users. I want to write a script which picks users from the CSV and then when asked for password it passes passwords from the CSV to create users. Following is what I have done but it didn't work as intended:

while IFS=, 
read -a csv_line; do 
createusr ${csv_line[0]}:${csv_line[1]}:${csv_line[1]};done < Desktop/Passwoerter.csv

It gives error that password do not match! The actual script for individual user work like:

~$ createuser xyz <press Enter>
password for xyz: whatever <press enter>
confirm password for xyz whatever <enter again>
~$

Its NOT:

~$ createuser xyz whatever whatever <press enter>
~$ 

It works fine if I add one by one but there are 1000+ so I was trying using a small script over CSV.

0

2 Answers 2

1

First Option: Use newusers: Its a part of passwd package.

DESCRIPTION
   The newusers command reads a file (or the standard input by default)
   and uses this information to update a set of existing users or to
   create new users. Each line is in the same format as the standard
   password file.

Input file formatting:

username:passwd:UID:GID:full name,room number,work phone,home phone,other:directory:shell

As you can see, the password is provided in the file.

Downside, You need to convert your csv to txt with provided format.

newusers < file.txt

Second Option: you can use useradd with -p switch.

useradd -m -p $(mkpasswd "password") username

-m Create the user's home directory if it does not exist.

-p The encrypted password, as returned by crypt(3).

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

3 Comments

No, I have an script to create these users as these are part of an ERP and hit various tables in DB and already existing script create user and save the required info in various tables in DB so I have to use this script only to create users.
This creates the same users, You just need to convert your csv into provided format or use second option, and supply the values in variables.
I have to create users for our ERP, not for ubuntu, so first script do that but one at a time
0

I find out what I needed, my script looks like:

while IFS=# read -a csv_line
do
    printf "%s\n%s\n" ${csv_line[1]} ${csv_line[1]} | createuser ${csv_line[0]}  
done

and executed like:

./insertalluser < Desktop/Passwoerter.csv

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.