title: Shell syntax error near unexpected token `done'
body: <p>This shell script is supposed to add users to the system.  The new users details are in a file.  The shell is rejecting this script with the message:</p>
  
  <pre><code>syntax error near unexpected token 'done'.
  </code></pre>
  
  <p>What's wrong?</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/bash
  #Purpose: Automatically add new users in a linux system based upon the data found within a text file
  #         Assign encryped passwords to each user
  #         Add users to groups or create new groups for these users
  #         Report errors and successful operations where necessary in log files
  #         post help options (echo)
  
  #Root validation
  if [[ $(id -u) -eq 0 ]]; then
    #Argument validation
    if [[ -z "$1" ]]; then
      echo "No arguments found!"
      echo "Please include a user detail text file as first argument"
      echo "Please include a report text file as second argument"
      echo "Please include an error report text file as the third argument"
      echo "Use the -h argument (i.e. ./script -h) for help"
    exit 1
  fi
  
  #Help validation and Help file
  if [[ "$1" = "-h" ]]; then
    echo "This is the help information file"
    echo "This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)"
    echo "The format of this user detail text file is "username password groupname fullname" seperated using TAB spacing"
    echo "This script will read the first argument as the user detail file, the second and third arguments will be read as a success report file and error report file respectively"
    exit
  fi
  
  #Reads first argument as user detail file for data
  cat userlist.txt | while read uname password gname fullname
  #Reads /etc/passwd for Username
  egrep -w "^$uname" /etc/passwd
  #If Username is found then error reports
  if [ $? == 0 ]; then
    echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" &gt;&gt; Successes1.log
    exit 1
  else
    #Reads /etc/group for Groupname
    egrep -w "^$gname" /etc/group
    #If Groupname is found then nothing
    if [ $? == 0 ]; then
      echo ""
    else
      #If Groupname not found then creates new group and reports
      groupadd "$gname"
      echo "Group Not Found: New Group $gname was created" &gt;&gt; Successes1.log
    fi
    #Retrieves Date
    createddate=$(date)
    #Perl password script takes input from Userlist
    pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")
    #Adds Users with variables from userlist
    useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"
    #Reports information to successlist and errorlist report files
    if [ $? == 0 ]; then
      groupid=$(id -g $uname)
      userid=$(id -u $uname)
      echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" &gt;&gt; Successes1.log
    else
      groupid=$(id -g $uname)
      userid=$(id -u $uname)
      echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" &gt;&gt; Errors1.log
      echo "Error: Must be root user to execute script"
      exit
    fi
  done
  </code></pre>
  
  <hr>
  
  <h3>Second attempt</h3>
  
  <p>Using some of the ideas from the answers, I came up with a second attempt:</p>
  
  <pre><code>#!/bin/bash
  #Purpose: Automatically add new users in a linux system based upon the data found within a text file
  #         Assign encryped passwords to each user
  #         Add users to groups or create new groups for these users
  #         Report errors and successful operations where necessary in log files
  #         post help options (echo)
  
  #Root validation
  if [[ $(id -u) -eq 0 ]]; then
    #Argument validation
    if [[ -z "$1" ]]; then
      echo "Usage: $0 usernames report errors" 1&gt;&amp;2
      echo "Please include a user detail text file as first argument"
      echo "Please include a report text file as second argument"
      echo "Please include an error report text file as the third argument"
      echo "Use the -h argument (i.e. ./script -h) for help"
      exit 1
    fi
  fi
  
  #Help validation and Help file
  if [[ "$1" = "-h" ]]; then
    echo "This is the help information file"
    echo "This script is designed to add users to a linux system by reading information from a user detail file (such as userlist.txt)"
    echo "The format of this user detail text file is "username password groupname fullname" seperated using TAB spacing"
    echo "This script will read the first argument as the user detail file, the second and third arguments will be read as a success report file and error report file respectively"
    exit
  fi
  
  #Reads first argument as user detail file for data
  cat jan.txt | while read uname password gname fullname; do
  #Reads /etc/passwd for Username
   egrep -w "^$uname:" /etc/passwd &gt;/dev/null 2&gt;&amp;1
  #If Username is found then error reports
  if [ $? == 0 ] 
  then
    echo "User Already Exists : Error adding user with username $uname;$gname;$fullname" &gt;&gt; Errors1.log
  
  else
    #Reads /etc/group for Groupname
    egrep -w "^$gname" /etc/group
    #If Groupname is found then nothing
  if [ $? == 0 ]; then
      echo ""
  else
    #If Groupname not found then creates new group and reports
    groupadd "$gname"
      echo "Group Not Found: New Group $gname was created" &gt;&gt; Successes1.log
  done &lt; $1
  #Retrieves Date
  createddate=$(date)
  #Perl password script takes input from Userlist
  pass=$(perl -e 'print crypt($ARGV[0], "Password")' "$password")
  #Adds Users with variables from userlist
  useradd "$uname" -g "$gname"  -c "$fullname" -p "$pass"
  #Reports information to successlist and errorlist report files
  if [ $? == 0 ]
  then
    groupid=$(id -g $uname)
    userid=$(id -u $uname)
    echo "User Successfully Added: $uname;$userid;$gname;$groupid;$createddate;$fullname" &gt;&gt; Successes1.log
  else
    groupid=$(id -g $uname)
    userid=$(id -u $uname)
    echo "Useradd Error Occurred: $uname;$userid;$gname;$groupid;$createddate;$fullname" &gt;&gt; Errors1.log
    echo "Error: Must be root user to execute script"
    exit 1
  fi
  fi
  done
  </code></pre>
  
  <p>This does not seem to work properly either.  What's wrong now?
  Seems to show arguments and runs however no users nor group have been added therefore no logs have been created</p>