User2403
User2403

Reputation: 185

Interactive Bash Script

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.

Upvotes: 0

Views: 158

Answers (2)

User2403
User2403

Reputation: 185

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

Upvotes: 0

Farhad Farahi
Farhad Farahi

Reputation: 39237

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).

Upvotes: 1

Related Questions