'(' unexpected - Error using for Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting '(' unexpected - Error using for Loop
# 1  
Old 09-08-2014
'(' unexpected - Error using for Loop

HI,

I am facing an error while executing a script in for loop.
Code:
 the syntax is : for (( i=0;i<=${v_colcnt};i++ )); do
 Error : '(' unexpected

I am new to unix and have tried multiple options such as changing the script from !# /bin/ksh to !# /bin/sh (not sure if this works to execute in sh). I have tried searching for the syntax of for loop in ksh but didn't find the above syntax.Can someone please suggest if the above works in ksh.

I am using SunOS server and script is in ksh. Please help

Thanks in Advance


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by rbatte1; 09-08-2014 at 12:24 PM.. Reason: Added ICODE tags
# 2  
Old 09-08-2014
Well the syntax of a for is not like that...
Code:
for i in ...
do 
   blah
   blah
   .
   .
done

# 3  
Old 09-08-2014
Bug

First of all you have to fix the shebang/hashbang line (first line) in your script and try again.

!# /bin/ksh and !# /bin/sh <- This is wrong/invalid.

It has to be either #!/bin/ksh or #!/bin/sh
# 4  
Old 09-08-2014
What shebang? I see nothing, but perhaps they edited the script out from under you.
# 5  
Old 09-08-2014
for (( i=0;i<=${v_colcnt};i++ )); do looks like BASH syntax.
# 6  
Old 09-08-2014
Hi,
Thanks for the prompt response. The hashbang syntax is
Code:
#!/bin/ksh

or
Code:
[#!/bin/sh]

only but I typed it incorrectly in my earlier post. So what I incur from the above comments is that the syntax of for is not correct for a ksh script.
I will try modifying the script using the syntax mentioned by Vbe.
Code:
 
  
 for i in [ 0- ${v_colcnt} ]
 do 
   Processing logic.
i=`expr $i+1`   .
done

are there any other options.

Thanks all for your help![/QUOTE]

Moderator's Comments:
Mod Comment Please use code tags for code. Not color, not font, but code, [code]stuff[/code].

Last edited by Corona688; 09-08-2014 at 01:46 PM..
# 7  
Old 09-08-2014
Code:
[#!/bin/sh]

This is wrong, use
Code:
#!/bin/sh

That is not the syntax vbe suggested either.

There are several ways to do this, this perhaps the best for an extremely basic shell:

Code:
I=1

while [ "$I" -le "${v_colcnt}" ]
do
        ...
        let I=I+1
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop - unexpected token `do

My requirement is to search for current date-1 .log files in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs folder and write the file name to filenames.txt When I ran the script below, I got error as syntax error near unexpected token `do I'm not sure what is wrong in my code. I... (11 Replies)
Discussion started by: Ram Kumar_BE
11 Replies

2. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

3. Shell Programming and Scripting

Bash (Ubuntu server): Syntax error: "|" unexpected in While-loop

Hello forum, I hope my problem is easy to solve for someone in here! My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I'm only able to run it on one account at a time. After... (3 Replies)
Discussion started by: primaxx
3 Replies

4. Shell Programming and Scripting

unexpected output within a for loop using awk

Hi all, after hours of playing around with this and scouring the web I decided to ask my fellow UNIX operators as I can't wrap my head around this. First off, I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer... (2 Replies)
Discussion started by: Keepcase
2 Replies

5. Shell Programming and Scripting

While loop error: Unexpected token done

I have tried to implement a while loop into the code but upon running the following code i am getting the errors: ./Assigntest: line 42: syntax error near unexpected token `done' ./Assigntest: line 42: `done' The code is as follows: #!/bin/bash #Filename: Assignment Author: Luke... (9 Replies)
Discussion started by: warlock129
9 Replies

6. Shell Programming and Scripting

for loop not working - syntax error at line 6: `end of file' unexpected

I have a file called test.dat which contains a b I have written a shell script called test.sh for i in `cat test.dat` do echo $i done When i run this script using sh test.sh I get this message - test.sh: syntax error at line 6: `end of file' unexpected What is the... (3 Replies)
Discussion started by: debojyoty
3 Replies

7. Shell Programming and Scripting

sed error : Syntax error: redirection unexpected

My script is throwing the error 'Syntax error: redirection unexpected' My line of code.. cat nsstatustest.html | sed s/<tr><td align="left">/<tr><td align="left" bgcolor="#000000"><font color="white">/ > ztmp.Ps23zp2s.2-Fpps3-wmmm0dss3 HTML tags are getting in the way but they're needed to... (3 Replies)
Discussion started by: phpfreak
3 Replies

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question