Announcement

Collapse
No announcement yet.

strange problem dealing with for loop

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    strange problem dealing with for loop

    when I am doing the below operation on the shell prompt, then it works fine ::
    for (( i=0 ; i<=5 ; i++ )); do echo $i; done

    but when I store this loop in a file.sh and run
    $sh file.sh
    then it gives error as::
    Syntax error: Bad for loop variable

    I don't know why??, and even when I m doing ssh to my friend's shell then also it works fine.
    weired isn't


    #2
    Re: strange problem dealing with for loop

    An avoidable nuisance:

    Code:
    USER@C3PO # for (( i=0 ; i<=5 ; i++ )); do echo $i; done
    0
    1
    2
    3
    4
    5
    USER@C3PO # sh TrialAndError.sh
    TrialAndError.sh: 3: Syntax error: Bad for loop variable
    USER@C3PO # /bin/bash TrialAndError.sh
    0
    1
    2
    3
    4
    5
    USER@C3PO # /bin/dash TrialAndError.sh
    TrialAndError.sh: 3: Syntax error: Bad for loop variable
    ... and the explanation ...

    Code:
    USER@C3PO # ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 2006-10-26 18:08 /bin/sh -> dash
    ... further ranting, er, reading >

    Birdy aka littleDrHouse

    --

    Postscript: reference updated

    Comment


      #3
      Re: strange problem dealing with for loop

      Originally posted by penguin.ch
      Calling bash scripts through /bin/sh puts bash in sh compliance mode, resulting in subtle differences as well (as opposed to calling bash scripts with /bin/bash).

      Comment


        #4
        Re: strange problem dealing with for loop

        Originally posted by Earthwings
        Calling bash scripts through /bin/sh puts bash in sh compliance mode, resulting in subtle differences as well (as opposed to calling bash scripts with /bin/bash).
        Agreed; but, as a matter of fact, quite a lot of people keep believing in "sh = bash" and code accordingly, as one can learn from scans like [sudo] grep '#!/bin/sh' /etc/* ... and I strongly suspect that we are going to realize in due course if or when one of these scripts is going down the wrong drain, so to speak.

        Most of the users don't even know that the default shell has been changed underhand, and therefore will not even think of factoring the shell(s) when attempting to narrow down a problem.

        This is going to be great fun, I dare say :P

        Comment


          #5
          Re: strange problem dealing with for loop

          thanx to all of you

          I figured out same yst'day only.....but anyway thanks a lot

          Comment

          Working...
          X