title: Syntax error near unexpected token `fi' in command
body: <p>
  I am trying to create a command which lists the first or last n lines (number specified by the user) of every file in a directory which is also specified by the user. They also have the option of using head or tail.</p>
  
     
  
  <pre><code>HEADORTAIL=$1
  
  NUMLINES=$2
  
  DIRECTORY=$3
  
  if [ $# -lt 3 ]
  then
        echo "The command needs three arguments to work"
        echo "The usage of this command is as follows: lshead [-head or -tail] [n$
        exit
  elif [ -d "$DIRECTORY" ]
  then
      echo "This directory exists"
      while [ $2 -lt 1 ] ; do
              echo "The number of lines you wish to see must be greater than 0"
              read $2
      done
  if [ $1 == "-head" ]
  then
      head -$2 $HOME/$3/*
  elif [ $1 == "-tail" ]
  then
      tail -$2 $HOME/$3/*
      exit
  fi
  fi
  fi
  fi
  exit
  </code></pre>
  
  
  
  <p>I don't really know what to expect in terms of an answer from the terminal. Apologies if I have made any mistakes or done anything stupid. I am relatively knew to bash shell scripting and to this site.</p>
  
  
  
  <p>I get this error when I run the command.</p>
  
  <pre><code>lshead3 -head 10 bin
  ./lshead3: line 30: syntax error near unexpected token `fi'
  ./lshead3: line 30: `fi'
  </code></pre>
  
  <p>Just got this when I put it into ShellCheck</p>
  
  <pre><code>$ shellcheck myscript
  
      Line 9:
          echo "The usage of this command is as follows: lshead [-head or -tail] [n$
          ^-- SC1009: The mentioned parser error was in this simple command.
               ^-- SC1078: Did you forget to close this double quoted string?
  
      Line 11:
      elif [ -d "$DIRECTORY" ]
            ^-- SC1079: This is actually an end quote, but due to next char it looks suspect.
                       ^-- SC1078: Did you forget to close this double quoted string?
  
      Line 13:
          echo "This directory exists"
               ^-- SC1079: This is actually an end quote, but due to next char it looks suspect.
  
      Line 21:
      elif [ $1 == "-tail" ]
                     ^-- SC1073: Couldn't parse this double quoted string.
  
      Line 28:
  
  ^-- SC1072: Expected end of double quoted string. Fix any mentioned problems and try again.
  
  $ 
  </code></pre>