Automatic User Creation

Hello everybody,

I'm new to sh scripting and relatively new to linux in general which is why I've come here for assistance.

I have been attempting to make a shell script which automatically creates user accounts from a file list and further creating directories and moving files into it etc. The issue I'm having is that it won't create any other user accounts after the first one, it gets stuck on the second username entry and gives this error when it attempts to make the account,

(shaun being the second username in the Username file)

Can you please take a look at my code and see if I've made any mistakes? I've looked for hours to try and figure it out but to no avail.

Thank you in advance

So, you're incrementing the linecount and then using that to as an argument for the 'head' command, which will then return that number of lines from the top of Usernames - is that really what you want to do? I think that you want the second line on line count 2 - not the first 2 lines!

Why don't you just read the input file line by line and work on each entry in turn...

while read line
do
     process....
done < Usernames

gahh I'm confused. I was under the impression that I was incrementing the value which then dictates which line to read from the file.

the code you've said I should use, could you possibly help me in terms of what I need to replace if I put that in and where etc?

Sorry I'm just so new to shell scripting, total noob.

---------- Post updated at 08:17 PM ---------- Previous update was at 07:09 PM ----------

I have managed to integrate your suggestion Jerry thanks a lot for the help but it's brought up another issue now, maybe simply fixed but I can't get my head around it.

This is my code now:

The script successfully creates user accounts for names of accounts that don't already exist but when the userexists () function is called it throws up an infinite loop error message that says,

If anybody can help me fix this issue it would be much appreciated!

thank's in advance again

A few things...

First,

set -x

in the code or

#! /bin/ksh -x

as the first line will provide verbose debug output and typically makes things much more obvious.

Secondly, you would normally use 'find'to find multiple files in a directory tree matching defined criteria. In this case, I think that you simply want to determine whether a home directory exists, so use

if [ -d /home/${eachline} ] ...

You may find the -x option shows you that the content of $existinguser is not what you expect!

Your code

if [ $input -eq '1' ]

is syntactically incorrect.
If you want to compare $input with integer 1, use -eq and don't quote the 1. If you want to compare it with string "1", use '=' and for full correctness double quote $input.