3

I tried almost all solutions suggested here but this very simple code of mine keeps showing this error

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

:

-sh-4.1$ sh test1.sh
1
test1.sh: line 10: syntax error: unexpected end of file

line 10 is one line after the "done" command. I know it has something to do with the spaces, but there aren't any spaces in my program.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
RAJKUMAR PADILAM
  • 131
  • 1
  • 10

3 Answers3

3

You must have spaces around [ ] to do the whole script working, so :

x=1
echo $x
while [ $x -le 5 ]; do
#      ^        ^
#    space    space
    echo $x
    x=$(($x+1))
done

or with arithmetic :

x=1
echo $x
while ((x < 5)); do
    echo $((x++))
done
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

try put a header into the script

#!/bin/bash

this way linux will automatically interpret the script as a bash script.

repzero
  • 8,254
  • 2
  • 18
  • 40
0

I found the problem - carriage-return characters in the file.

I was typing the script in Windows (text pad) and executing it on Linux. The editor on Windows ends lines with \r\n; when running the script on Linux, the presence of \r was creating a problem.

Using vi as editor helped me resolve this issue.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
RAJKUMAR PADILAM
  • 131
  • 1
  • 10