0
votes
#!/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
1
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
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
i am new to scripting. yes i copied the exact code. - user3068028

1 Answers

2
votes

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.