Forum Home
Press F1
 
Thread ID: 78591 2007-04-21 09:22:00 Help with Linux -Part2 ronyville (10611) Press F1
Post ID Timestamp Content User
542942 2007-04-21 09:22:00 Hi guys,
I was writing a command to add a user by reading user details from a text file. It worked fine the first few times but for some reason it doesnt anymore. I have tried doing whatever I can but cant figure out where the problem is.
heres the code

while read inputline
do
login="$(echo $inputline | cut -d: -f1)"
password="$(echo $inputline | cut -d: -f2)"
group="$(echo $inputline | cut -d: -f3)"
fullname="$(echo $inputline | cut -d: -f4)"
done < /home/rony/userlist

#This sections adds user to the system

if [ $(id -u) -eq 0 ]; then

egrep "^$login" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$login exists!"

exit 1

else

password=$(perl -e "print crypt($ARGV[0], 'password')" $password)

/usr/sbin/groupadd $group

/usr/sbin/useradd -m -p $password $login

[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"


fi
exit 2

fi

Its reads data from a file "userlist"..
Now when i run this command, it tell me that user already 'exists' but when i go into users and groups.. its not there. It was working fine but ........ somethings wrong now. Any help would be appreciated

cheers
Ron
ronyville (10611)
542943 2007-04-21 09:45:00 this want i use to do the same thing



HOME_BASE="/home/"
cat new | while read USER PASSWORD GROUP
do
echo $USER $PASSWORD $GROUP
useradd ${USER} -p ${PASSWORD} -G ${GROUP} -m -d ${HOME_BASE}${USER}
done


"new" being the name of the text file

I have also found that sometimes you need to use the group gid as opposed to the group name
beama (111)
542944 2007-04-21 09:58:00 how are you deleteing the test user ?
how are you running the script ie # ./script.sh
try using another unused user name see if that works

have you maked the script executable

chmod u+rwx filename

x = executable
beama (111)
542945 2007-04-21 10:15:00 how are you deleteing the test user ?
how are you running the script ie # ./script.sh
try using another unused user name see if that works

have you maked the script executable

chmod u+rwx filename

x = executable

Yup. did mark the script using chmod 777 filename
I delete the test user through adminstator>groups and users. GUI.. not through command mode. It was working fine that way. I will try yur 1st suggestion and see if it makes any difference
ronyville (10611)
542946 2007-04-21 10:16:00 just read through your scrip agian and noticed that when you are using your varibles

ie

/usr/sbin/useradd -m -p $password $login

you are not enclosing them in {}

try

/usr/sbin/useradd -m -p ${password} ${login}

also scripts such as this should be run as root or su (su is better) because I dont think a normal user has sufficent system rights to add users.
beama (111)
542947 2007-04-21 10:19:00 edit

wrong information
beama (111)
542948 2007-04-21 11:16:00 /usr/sbin/useradd -m -p ${password} ${login}

also scripts such as this should be run as root or su (su is better) because I dont think a normal user has sufficent system rights to add users.

I did that {} but not luck, and I m logged in as a root user. thanks for your help
ronyville (10611)
542949 2007-04-21 11:42:00 This is what i get after i run the scripts.

[root@RC6 rony]# ./testing
Welcome rony
Today is Sat Apr 21 22:41:00 NZST 2007

exists!
[root@RC6 rony]#

I m guessing its the first part of the code that determines if the user is already in the system...

#This sections adds user to the system
if [ $(id -u) -eq 0 ]; then

egrep "^${login}" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "${login} exists!"

exit 1

mmmmmmmmmmmmmmmmmmmmmmmm .....
ronyville (10611)
542950 2007-04-21 11:47:00 I also tried adding a user with different details but get the same result..:annoyed: ronyville (10611)
542951 2007-04-21 22:03:00 if im reading your script right

while read inputline

/* then loading the login variable name */

login="$(echo $inputline | cut -d: -f1)

Is this getting the username from the input ie bash loggin. that is maybe why you are getting the exsit output
you are logged in there for you exist
should this variable loading come from the txt file

should you not loading all variables from the txt file you seem to getting values from inputline not he text file

see my code example ie cat new | while read etc

add this line for error checking
while read inputline
do
login="$(echo $inputline | cut -d: -f1)"
echo $login
/*check the value this variable is holding*/
beama (111)
1 2