#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0

    for statement problem


    I face problem with a very simple for statement function. This is the script below

    for (( i=1; i<=5; i++ ))
    do
    for (( j=1; j<=i; j++ ))
    do
    echo -n "$i"
    done
    echo ""
    done

    and the error msg is
    syntax error at line 1 : `((' unexpected

    y this happend? can anyone tell me y?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    12
    It would help us to know which language you are using. My coin toss says ksh, so I'm gonna roll with that.

    It would also help the computer to know what language you're using, so your script should start with a #! /path/to/interpreter statement. Here is a sample script that shows what "for" actually does. And it shows how to use a "while" to get what I think you want....
    Code:
    #! /usr/bin/ksh
    LIST="one two three four"
    for item in $LIST ; do
           echo $item
    done
    
    i=0
    while ((i<5)) ; do
         echo $i
         ((i=i+1))
    done
    
    exit 0
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0
    I did include the !# /usr/bin/ksh in my script.
    I test on the while statement, it work.. but my problem is y for statement cannot be used.

    for my information, for statement is shell script canbe hv this kind of syntax
    for variable in list
    do
    ...
    done

    but my question is can this kind of for statement working?
    for (( i=1; i<=5; i++ ))
    do
    ...
    done

    Thanks~
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    12
    Well, you tried it and got an error message. That's a pretty strong indication that it won't work.

    The confusion comes from the fact that there are several versions of the Korn shell. ksh-88 got picked up by all of the major unix vendors. The syntax for the posix shell is very close to ksh-88. So everyone was forced into ksh-88 for posix compliance.

    I see from The New Kornshell Command and Programming Language by Bolsky and Korn that the syntax you are trying was added to ksh-93. But the only way to get ksh-93 is to download the source code and install it yourself. And it's not like perl, compiling ksh is a tough job. And you can't redistribute ksh-93. That shuts the door on a guru porting it to each version of unix and making the port available on a free download site. As a result, ksh-93 is almost never available, and even if you have it, you should ignore the extentions if you want your script to be portable.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    22
    Rep Power
    0
    Thanks for the information~~ Then, i think i hv to choice other alternative instead of using for loop.. By the way, thanks for ur advice. I appreciate it very much~ Thank you very much!
    Have a nice day!

IMN logo majestic logo threadwatch logo seochat tools logo