0

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)

1
  • RESOLVED: I tried to use "pico" instead of trasfering file via FTP and all works! thanks! Commented Feb 20, 2011 at 21:38

4 Answers 4

1

It works when I run that code.

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

Sign up to request clarification or add additional context in comments.

5 Comments

Works for me under sh as well. My version of sh is 4.1.
@Rafe On some architectures, sh is a separate, less capable executable than bash.
i have GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
RESOLVED: I tried to use "pico" instead of trasfering file via FTP and all works! thanks!
@Leonardo: Check your FTP line ending conversions. Line endings are significant to bash since a newline marks a new statement, and if the line endings are being translated incorrectly, the statements will be mixed up. You CAN explicitly terminate statements with semicolons, but you're better off just making your file transfers work properly.
1

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.

1 Comment

Look hard at the code, then. Possibly, you have CRLF (DOS) line endings? Even that's a bit of a stretch, though. The Linux machine I tested on has bash 3.2.25(1) on x86/64. Look at your code with od -c; there is ample evidence that what is copied and pasted is OK. Maybe try typing it in again into test2.sh, and see if that works. It sounds silly, but at this point, your problem is most peculiar.
0

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.

Comments

0

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

2 Comments

The semi-colons are not relevant.
seq isn't needed in Bash: for i in {1..5} or for ((i=1;i<=5;i++))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.