LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2006, 08:21 PM   #1
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
adduser shell script having some problems.


I am a little rusty on my shell scripting but I am making a script to automatically create users using the variables provided. Here is the script itself followed by the error I recieve when I run it.

Code:
#!/bin/sh

# adduser
adduser $1 -p $2

# copy files
cp /usrfiles/config /home/$1
cp -Rf /usrfiles/steam /home/$1

# change user and group
cd /home/$1
chown $1 config
chown -Rf $1 steam
chgrp $1 config
chgrp -Rf $1 steam

# display info
if [ -d /home/$1 ]; then
        echo "User $1 created with home directory of /home/$1"
else
        echo "No user created"
Now the error:

Code:
[root@localhost ~]# newuser greg asdfasdf
adduser: user greg exists
cp: cannot overwrite non-directory `/home/greg' with directory `/usrfiles/steam'
/bin/newuser: line 11: cd: /home/greg: No such file or directory
chown: cannot access `config': No such file or directory
chown: cannot access `steam': No such file or directory
chgrp: cannot access `config': No such file or directory
chgrp: cannot access `steam': No such file or directory
/bin/newuser: line 23: syntax error: unexpected end of file
The weird thing here is this:
Code:
[root@localhost /]# ls /home
config  greg  jkkdc  kimball
greg is not a directory though, it is a file that is blank and owned by root. Any ideas?
 
Old 06-10-2006, 08:55 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, a couple of things:

First, this error
Quote:
adduser: user greg exists
implies hat you've run the script before using "greg" as a test case and never removed him, or that greg is a legitimate user.

Second, the next sequence of errors can be explained if a basic assumption in your scipt fails. That is, you call adduser, and your script assumes adduser will create a home directory (/home/greg). What if it doesn't? If it doesn't, then your next command:
Quote:
cp /usrfiles/config /home/$1
would copy /usrfiles/config to a file named /home/greg--not into a directory named /home/greg because the directory wasn't created.

That means the cd fails, all the other ownership/permissions changing stuff also fail.

Third, your if-else statement needs a "fi" at the end:
Code:
# display info
if [ -d /home/$1 ]; then
        echo "User $1 created with home directory of /home/$1"
else
        echo "No user created"
fi
I would suggest using the standard tools for all this (that's what they are there for )

Read the man page for adduser (sometimes named useradd) and read about the options, specifically -m and -p. The reason I suggest -p is because the password argument is expected to have already been passed through crypt. That is, if your script had worked, "asdfasdf" would be the encrypted form of the password and not the text a user would type to login.

Last edited by Dark_Helmet; 06-10-2006 at 08:59 PM.
 
Old 06-10-2006, 09:23 PM   #3
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Original Poster
Rep: Reputation: 15
Thanks for the quick and thorough reply. Yea I did run the script before as greg for a test. My bad there. I had a simliar script like this before that worked well, but that got deleted so I am writing this one. been a while, thanks again, I'll take a look at this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
adduser but no shell and no home dir ryedunn Linux - Newbie 7 09-05-2011 09:52 AM
Adduser script wincrk Linux - General 1 05-14-2006 11:19 PM
adduser and smbpasswd script homey Linux - Networking 7 09-16-2004 06:01 PM
adduser script problem svs Programming 1 07-31-2001 02:22 PM
Adduser script Copenhagen Cowboy Programming 0 03-06-2001 09:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration