0
votes

File: test.sh

Command: sh test.sh

Content of test.sh

   x=1
    while [ $x -le 5 ]
    do
      echo "Welcome $x times"
      x=$(( $x + 1 ))
    done

Error:

test.sh: line 6: syntax error near unexpected token `done'

test.sh: line 6: `done'

GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)

4
This works perfectly fine for me. - Rafe Kettler
RESOLVED: I tried to use "pico" instead of trasfering file via FTP and all works! thanks! - Leonardo

4 Answers

1
votes

It works when I run that code.

Make sure you're running with bash not sh. The $(( )) construct is relatively new.

1
votes

Run your script with:

bash test.sh

It will work. There are two main possibilities:

  • When it is run as sh, bash does not necessarily recognize all the syntax that it recognizes when it is run as bash. On the Linux and MacOS X machines where I tested this, though, both sh and bash worked fine.
  • Alternatively, you might be on a machine running AIX, HP-UX, Solaris or similar, where /bin/sh is not the same shell as bash at all. It is more rigid Bourne shell, where the notation you used is invalid - a syntax error.
0
votes

That's strange on my system I get.

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

Must have something to do with whitespace.

0
votes

Say hello to semicolons. :)

x=1
while [ $x -le 5 ]
do
  echo "Welcome $x times";
  x=$(( $x + 1 ));
done

You don't need a semicolon if it's one line, however, you might want to do a loop this way:

for i in `seq 1 5`
do
  echo "Welcome $i times"
done