0
#!/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
3
  • 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. Commented Dec 4, 2013 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). Commented Dec 4, 2013 at 23:48
  • i am new to scripting. yes i copied the exact code. Commented Dec 5, 2013 at 0:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi dan,echo has no effect. Still the same error message. its not creating any users.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.