Something went wrong in the following loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Something went wrong in the following loop
# 1  
Old 03-29-2008
Something went wrong in the following loop

The script is
Quote:
210 count=2 #the iteration time
211 while [ ${NUM2} -lt ${NEWESTEID} -o ${NUM1} -gt ${OLDESTEID} ]
212 do
213 eval count=`expr $count \* 2`
214
215 subscribe -n ${CHANNELNAME} -s ${NUM1} -e ${NUM2} > ${RAWDATAFILE}
216 if grep "SOS" ${RAWDATAFILE}
217 then
218 tempDate=`FindDate ${RAWDATAFILE}`
219 if [ ${tempDate} -eq ${COB} ]
220 then
221 EID=`FindSOS ${RAWDATAFILE}`
222 echo ${EID}
223 exit 0
224 elif [ ${tempDate} -gt ${COB} ]
225 NUM1=`expr ${OLDESTEID} + ${EIDCOUNT} / ${count}`
226 NUM2=`expr ${NUM1} + ${LOOP}`
227 else
228 NUM1=`expr ${NUM1} + ${EIDCOUNT} / ${count}`
229 NUM2=`expr ${NUM1} + ${LOOP}`
230 fi
231 else
232 tempDate=`FindDate ${RAWDATAFILE}`
233 if [ ${tempDate} -eq ${COB} ]
234 then
235 NUM1=`expr ${NUM1} - ${EIDCOUNT} / ${count}`
236 NUM2=`expr ${NUM1} + ${LOOP}`
237 elif [ ${tempDate} -gt ${COB} ]
238 NUM1=`expr ${OLDESTEID} + ${EIDCOUNT} / ${count}`
239 NUM2=`expr ${NUM1} + ${LOOP}`
240 else
241 NUM1=`expr ${NUM1} + ${EIDCOUNT} / ${count}`
242 NUM2=`expr ${NUM1} + ${LOOP}`
243 fi
244 fi
245 done
246
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

Last edited by tpltp; 03-29-2008 at 08:08 AM..
# 2  
Old 03-29-2008
Hi.

The keyword elif is the same as else if, yes? What does one need after an if? ... cheers, drl
# 3  
Old 03-29-2008
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
# 4  
Old 03-29-2008
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.

Code:
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.

Last edited by era; 03-29-2008 at 08:46 AM.. Reason: Note about set -vx
# 5  
Old 03-29-2008
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...
# 6  
Old 03-29-2008
Argh, right, I missed that even though I was specifically looking for it! Good catch.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What's wrong with this while loop?

function get_tablespace() { ## Get the current size of the tablespace size=`su -l oracle -c 'db-control report' |egrep "DATA_TBS" | awk '{print $5}'|tr -d '%'` ## Loop through until the size is 82 or less count=0 while && do ... (2 Replies)
Discussion started by: bille
2 Replies

2. Shell Programming and Scripting

What is wrong with my if loop?

Hello everyone, I need a little help. I wrote a cshell script to change the format of a file but it is not working. input file is like that: 2014 3 20 15 0 5.270 40.7739 27.6471 20.232 0.6 0 0 0 1 Site6 4.081 1.00 P Site6 7.585 1.00 S Site1 4.441 1.00 P... (9 Replies)
Discussion started by: miriammiriam
9 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

5. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

what's wrong with that loop ?

hello everybody, here's my code mkf () { INDEX=0; while ; do touch "file$1.f"; INDEX=$INDEX+1; done } when I type mkf 10 the loop seems to act infinite and only the last file of the loop is created, in the example below, there just is file10.f in... (11 Replies)
Discussion started by: Oddant
11 Replies

7. Shell Programming and Scripting

Whats wrong with my Loop

Hi all, I have been given a task to search for strings in a file that is encoded. I need to display the file name only when all the 3 strings which i provide are present in the file name. i first try to get a list of files according from the directory according to the value passed in argument... (0 Replies)
Discussion started by: amit1_x
0 Replies

8. Shell Programming and Scripting

Something wrong with while loop

Hi there, i've written a script to extract a portion of a MySQL database table and convert it to CSV and then to import it back as CSV to MySQL. Initially it worked without the while loop but after adding the while loop statement, i am getting the following error: ./export-csv-coordinates.sh:... (4 Replies)
Discussion started by: edge80
4 Replies

9. Shell Programming and Scripting

What is wrong in my IF loop ??

What is wrong in my IF loop if then echo " The request is authenticated " fi The error im getting is $ ./routing.sh server enables ********************************************************************** Preparing to service the request for Device server in Question... (9 Replies)
Discussion started by: raghunsi
9 Replies

10. Shell Programming and Scripting

new to shell scripting: whats wrong with my if loop

#!/bin/bash for file in $HOME/*; do if ; then rm -i $file > /dev/null echo "$?" echo "$file has been deleted" fi done Been trying to learn shell scripting for a week or so now, when i run the script it doesnt display an error message, seems like it runs fine, however it doesnt delete... (10 Replies)
Discussion started by: stride6
10 Replies
Login or Register to Ask a Question