Automatic User Creation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatic User Creation
# 1  
Old 01-06-2013
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,

Quote:
find: paths must precede expression: shaun
(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.

Quote:
userchecks () {
linecount=$(wc -l < Usernames)
echo $line

while [ $line -le $linecount ]
do
line=`expr $line + 1`
echo $line
username=$(head -$line Usernames)
existinguser=$(find /home -name $username)
if
[ -z $existinguser ]
then
sudo useradd -m $username

unset username
unset existinguser

else
userexists
fi
done
}

userexists () {
echo "PRESS 1 TO DELETE THE OLD $existinguser ACCOUNT, OR 2 TO KEEP IT, BUT RENAME IT WITH _OLD PREFIX:"
read input
echo $input

if [ $input -eq 1 ]
then
user=$(basename $existinguser)
sudo userdel $user
sudo rm -rf $existinguser
echo "USER ACCOUNT DELETED"
sudo useradd -m $user
unset username
unset existinguser
unset user
elif [ $input -eq 2 ]
then
suffix="_old"
user=$(basename $existinguser)
sudo userdel $user
sudo mv /home/$user /home/$user$suffix
echo "USER ACCOUNT NAME SUFFIXED WITH _OLD"
sudo useradd -m $user
unset username
unset existinguser
unset user
fi

userchecks

}

line=0
userchecks
Thank you in advance
# 2  
Old 01-06-2013
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...

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

# 3  
Old 01-06-2013
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:
Quote:
useradd () {

while read eachline
do
existinguser=$(find /home -name $eachline)
if
[ -s $existinguser ]
then
sudo useradd -m $eachline
else
userexists

fi
done < Usernames


}

userexists () {
echo "PRESS 1 TO DELETE THE OLD $existinguser ACCOUNT, OR 2 TO KEEP IT, BUT RENAME IT WITH _OLD PREFIX:"
read input
echo $input

if [ $input -eq '1' ]
then
user=$(basename $existinguser)
sudo userdel $user
sudo rm -rf $existinguser
echo "USER ACCOUNT DELETED"
sudo useradd -m $user
unset username
unset existinguser
unset user
elif [ $input -eq '2' ]
then
suffix="_old"
user=$(basename $existinguser)
sudo userdel $user
sudo mv /home/$user /home/$user$suffix
echo "USER ACCOUNT NAME SUFFIXED WITH _OLD"
sudo useradd -m $user
unset username
unset existinguser
unset user
fi

useradd

}

useradd
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,
Quote:
./makeUser1: 28: [: /home/shaun: unexpected operator
PRESS 1 TO DELETE THE OLD /home/shaun
/home/charlie/.local/share/Trash/files/shaun ACCOUNT, OR 2 TO KEEP IT, BUT RENAME IT WITH _OLD PREFIX:
ellis
./makeUser1: 45: [: Illegal number: ellis
./makeUser1: 55: [: Illegal number: ellis
If anybody can help me fix this issue it would be much appreciated!

thank's in advance again
# 4  
Old 01-07-2013
A few things...

First,
Code:
set -x

in the code or
Code:
#! /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
Code:
if [ -d /home/${eachline} ] ...

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

Your code
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

NIS user creation

Hi can anyone help me how to create a NIS user by editing the files like Passwd,group, Netgroup,auto.home and Aliases using the "enis" utility? Please do the needful................................ (4 Replies)
Discussion started by: Revathi@1
4 Replies

2. Shell Programming and Scripting

User creation script

Hi Gems.. I am working out on project of creating a mass user on 100 server. Please help me with script where i can create an user id of new 80 user using shell script Thanks in advance. Indrajit Bhagat (1 Reply)
Discussion started by: indrajit_renu
1 Replies

3. AIX

User creation

hi seniors i want to create the users like when the user try to login it should not should not promt for passwd is it possible to create users like this please help me (6 Replies)
Discussion started by: senmak
6 Replies

4. Solaris

Need help with user creation

Hello guys, i have a problem where i cant create a user and here some problem that i encounter. 1. /etc/passwd seems locked. 2. getting this error UX: useradd: ERROR: Cannot update system files - login cannot be created. # useradd -u 47367 -g 1084 -c "user123" -d /local/home/user123 -s... (2 Replies)
Discussion started by: brew
2 Replies

5. Red Hat

User creation

Hi Thanks in advance. How to create a user without useradd command ?? (1 Reply)
Discussion started by: krish4linux
1 Replies

6. Solaris

User Creation

i am trying to create a user on solaris 10. it is not taking user name with more than ten characters. which file i need to edit to do this setting. (4 Replies)
Discussion started by: raynu.sharma
4 Replies

7. Solaris

User creation

Hi all, I want to create one user with full permissions to one directory called /opt/tivoli/tsm/client/ba/bin. Name for the user Tivoli Thanks & Regards Babu (7 Replies)
Discussion started by: lbreddy
7 Replies

8. UNIX for Advanced & Expert Users

Automatic time out of user login

I have asked by our security team to implement an automatic time out for user logins after a specified time interval. I have never heard of this feature in Unix before. Does anyone know of a way to accomplish this for HP-UX 11i? (2 Replies)
Discussion started by: keelba
2 Replies

9. UNIX for Dummies Questions & Answers

user creation problem

hello, Actually I want to create a user for our brower based custom application for the mail access from our mailserver(linux). I create user dummy and I granted all the privileages to dummy user and made dummy equivelent to root and if I tried to create a another user logging as dummy ... (1 Reply)
Discussion started by: jarkvarma
1 Replies
Login or Register to Ask a Question