Something went wrong in the following loop

The script is

And the error is
+ count=2
./FindEID.ksh: line 227: syntax error near unexpected token `else'
./FindEID.ksh: line 227: ` else'

I just can't find out what the problem is, can anyone tell me about this error is?
Thank you

Hi.

The keyword elif is the same as else if, yes? What does one need after an if? ... cheers, drl

I tried to indent the scripts but failed. It looks ugly right now.

ELIF is same as ELSE IF, I think I did match the if-elif-else-fi pair

Superficially, it looks okay. Usually this happens when you have an "else" without a prior "if" but as far as I could quickly see, these are balanced. We could speculate that there is something outside the part you posted (or perhaps invisible control characers we can't see) which are masking out one of the early "if"s. A typical problem is if you have a missing closing quote somewhere; it's okay, in shell syntax, to have multi-line strings, so the string runs on and on and on and a large part of your script ends up missing because it's inside the string when finally another balancing close quote is encountered.

Maybe an example is better to show this.

if 'true magic is false; then
  echo I snot
  echo foo!'
else
  implode --violent --fireworks
fi

See? So the "then" never happens as far as the shell is concerned, because it's inside a string, and then it acts surprised when it sees an "else" where it expected a "then". The reason is that the opening quote on the first line starts a long string which only ends just before the else.

This should quickly become evident if you run with set -vx but if it's a large script, of course, it all scrolls by very fast. 2>&1 | less is your friend.

Thank you very much, era.

In my script, I have found out what caused the problem. There are a "then" missed after the "elif"

I think I will be more careful when some long script came to me...

Argh, right, I missed that even though I was specifically looking for it! Good catch.