What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Bash Loop Error

Col8eral

Regular Contributor
Try to run a simple loop script

Code:
#!/bin/sh
max=10
for (( i=2; i <= $max; ++i ))do
echo"$i"
done

but im getting "syntax error: bad for loop variable" when I run it. Wht am I doing wrong?
Thanks.
 
Try to run a simple loop script

Code:
#!/bin/sh
max=10
for (( i=2; i <= $max; ++i ))do
echo"$i"
done

but im getting "syntax error: bad for loop variable" when I run it. Wht am I doing wrong?
Thanks.
Just a quick clarification. Our routers use POSIX, not Bash. (You can enable it if you so choose, but it gets more messy). POSIX shell has issues with advanced loops like this... you probably want to use something like this:

Code:
i=0
while [ $i -le 10 ]; do
    echo "$i"
    i=$(( i + 1 ))
done
 
Last edited:
Thank you. I tried that and got this error:
loop: line 7: syntax error: unexpected end of file (expecting "do")
Make sure you set the environment at the top:

Code:
#!/bin/sh

i=0
while [ $i -le 10 ]; do
    echo "$i"
    i=$(( i + 1 ))
done

exit 0

This works for me:

1742133794017.png
 
Last edited:
Not sure what i'm doing wrong
Are you copying and pasting from a Windows notepad window by chance? Or some other intermediary source? I have heard of instances where additional hidden characters will mess with the script. Try copying it from here, and pasting it directly within nano (right-click on your mouse while in nano)... then save it. See if that helps?
 
Just a quick clarification. Our routers use POSIX, not Bash.
...
Just to be clear, ASUS routers use BusyBox's implementation of the ash shell. You can see this by checking the "history" filename in the $HOME directory:

BusyBox_Ash_Shell_History.jpg


While BusyBox ash shell is considered POSIX-compliant, technically it's not a true POSIX shell because it also allows some syntax forms that do not strictly adhere to the current POSIX standards for the shell command language specifications.

For example, the following syntax for string equality comparison works fine in ASUS routers but is not POSIX compliant:
Bash:
{
   varStr1="Var1"  varStr2="Var2"
   if [[ "$varStr1" == "$varStr2" ]]
   then echo TRUE
   else echo FALSE
   fi
}

A POSIX-compliant syntax would be the following:
Bash:
{
   varStr1="Var1"  varStr2="Var2"
   if [ "$varStr1" = "$varStr2" ]
   then echo TRUE
   else echo FALSE
   fi
}

So this means that you could write a shell script that works fine in ASUS routers but doesn't adhere strictly to POSIX standards; and that is not necessarily a "bad" thing, especially if your shell script is designed for and meant to work on ASUS routers only.

My 2 cents.
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top