How to add users from text file into the unix system?

I have a homework that require creating text file of users in unix and then reading each line of the file and actually adding them to the system?

How can this be done? what i have done:

however it's not working.

7

1 Answer

Create a file with name: text witch contains all users name.

for example my text file contentsusertemp

this is only one test user name.

Then create bash script file like this

run.sh

#!/bin/bash
while read line; do
useradd $line
done < text

Make the script run.sh runnable by type in terminal

chmod +x run.sh

At last run script with

./run.sh

Running above script may need root privileges.

sudo ./run.sh

Now these users cant log on because we have not assign any password to them.

[ToDO]

  1. Assign password for created list of users.
  2. Difference between using adduser and useradd, in this situation. see
  3. Read username, password from only one file, text.

[Related posts]

[Edit]

  • Remove redundant chmod 777 run.sh as wjandrea said on comment, its only make the script readable writable and executable for everyone see more

  • Use read command in script instead of more, to read line by line. I think its better in speed.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like