Creating User Accounts from a list in file

I have a file that contains a list of names.

I need a loop that creates user accounts to all the names in the list
where username = names in file
password = username

Another question: how can i validate that a particular var is of 6 characters length
I need an if statement that will validate a particular string
if it's less than 6 it will ask the user to reEnter, if it's fine,, it'll procceed..

Please i have to submit it the project today,,
would anyone help me out :slight_smile:

Best Regards,
Laila Saif

do you want to create a unix user account ??

for the second question :

#!/usr/bin/ksh

if (( ${#var} < 6 ))
then
echo "String length is less than 6"
fi

Hi there,,

Thanx alot for your quick reply..

I'm using Fedora Core 1
using the useradd and passwd commands in shell scripting..

regarding the second question,,,
it works perfectly,, thanx alot,, would i have to change the syntax
if i'm using in a while loop

i need to say

while [ ${#var} < 6 ]
do
echo it must be more than 6 charc.
echo please reEnter
read var
done

but it seems it doesn't work that way,, there's something wrong i don't know what it is,,

printf "Please enter var : "
read var

while (( ${#var} < 6 ))
do
print "$var value is less than 6"
printf "Please reenter var : "
read var
done

Regarding the first question :

if you have many users to add, take any one line of a user from /etc/paswd and format your user file with the same format.. after formating it, copy all the lines from the file and append it to /etc/passwd file in the end through vi and save.

You need to login as root first.

I'm not sure whether they is any other easy way, i generally add user ids in /etc/passwd. I also don't think we can assign the password, we need to run passwd command for each and every user.

Once you complete adding to /etc/passwd, run pwconv command.

Someone can help, if there is a simple way of doing it.

Thanks a million,,

The var testing works fine..

for the first question:

I'm actually which the following menu is part of it:

  1. Enter names
  2. Creates user accounts.

the first option takes all the names and inputs it in a file called usernames.txt
when option 2 is chosen,, it must create useraccounts for all the names listed in the file
making the useraccount & password = name inserted
it should create a user account for each name that is included in the file automatically from the script..

I'm not good at linux, may be you can try below stuff

cat users.txt | \
while read userid
do
useradd $userid
done

i hope useradd is not an interactive program. But passwd program needs to be invoked for each and every user to assign password or the users can change their password on first longin.. but not sure whether we have a better solution.

The command you gave me for the create user is fine, but i need to log on as root
to be able to create those accounts. Do u know whether there's a command that i can use in my script that will allow me to enter as root and create them automatically?

Not sure, there is something called setuid but i have never done it before.

after creating the script, try this

chmod 04755 executablename

I guess this needs to be done from root... after this you can run this program from other users as well... try luck, i have not done this before.