Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
#!/bin/bash
initial="/"
usrname=`cut -d ":" -f1 users.txt`
password=`cut -d '"' -f3 users.txt|cut -d ":" -f2`
comment=`cut -d '"' -f2,3 users.txt|cut -d ":" -f1`
path=`cut -d "/" -f2,3 users.txt`
totalpath="$initial$path"
while read line
  do
  useradd -p "$password" -c "$comment" -m -d "$totalpath" "$usrname"
done < "users.txt"

I am trying to add multiple users using text file. I am getting "syntax error near unexpected token `done'". All my stored variables are coming out perfectly. I tested it using echo command. However, when I run the loop, the error message pops up and users are not getting created.

For your reference this is how my text file looks like:

acdeng:"ADLER CHARLES DAVID",00-9388,x0753,Engineering:acc944:/home/Engineering
ardsal:"ANSELL ROBERT D",14-2675,x1624,Sales:acc944:/home/Sales
share|improve this question
    
Apart from any syntax error(s), the values of $password, $comment et al aren't going to be updated each time through the while loop; only $line is updated, and you don't use its value. –  Keith Thompson Dec 4 '13 at 23:44
    
Did you copy-and-paste your exact code? I don't believe there's a syntax error in the code you posted (though there are some serious logical errors). –  Keith Thompson Dec 4 '13 at 23:48
    
i am new to scripting. yes i copied the exact code. –  user3068028 Dec 5 '13 at 0:04

1 Answer 1

As pasted above, the script seems to run for me without any syntax error.

However, I'll note that you don't seem to be parsing each line of the file. The initial assignments to usrname et al are extracting multi-line slices, which you then use as-is on each iteration of the loop. You never refer to $line in the loop, so on each iteration you'll get the same call to useradd.

I recommend adding echo in front of useradd so you can see what commands you are actually emitting.

share|improve this answer
    
Hi dan,echo has no effect. Still the same error message. its not creating any users. –  user3068028 Dec 4 '13 at 23:57
    
For you reference this is how my users.txt file looks like acdeng:"ADLER CHARLES DAVID",00-9388,x0753,Engineering:acc944:/home/Engineering ardsal:"ANSELL ROBERT D",14-2675,x1624,Sales:acc944:/home/Sales bcmar:"BIANCHI CHRISTOPHER",12-2275,x3280,Marketing:acc944:/home/Marketing cmbman:"CAPOZZI MICHAEL B",08-3191,x8035,Manufacturing:acc944:/home/Manufacturing –  user3068028 Dec 5 '13 at 0:00
1  
Danfuzz' middle paragraph is very important. Your "cut" invocations are outside your loop. You are reading the file once for each "cut" and then once in the loop. –  Jeremy Dec 6 '13 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.