Question

Im new to to this Bash/Shell stuff and got to do some network analysis for a uni assignment.

Just trying to do a simple loop but getting a weird error that have been unable to fix despite a 2 hour google crawl:

#!/bin/bash
x=1
while [ $x -le 5 ]
do
  echo "Welcome $x times"
  x=$(( $x + 1 ))
done

I have tried using a for loop using the following syntax:

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

Whenever I place the first script on my server I get the following message:

./temp.sh: line 8: syntax error: unexpected end of file

Before running this file I have performed the following commands to add permissions and make the file executable:

ls -l temp.sh
chmod +x temp.sh

Just as a side note I found the following stackoverflow question about loops, copied the 'fixed code' and got the same error: Looping a Bash Shell Script

Im running version 4+ and using G-VIM as the text editor on Windows 7. Anyone got any ideas?

MANAGED TO SOLVE THIS MYSELF:

Seeing as my reputation is still too low I can't answer this myself at present so to stop people wasting there time here is how I fixed it:

Ok i've managed to fix this so will leave this up for anyone else who looks around.

My problem was that I was using FileZilla to connect to my server. It seems that even though I was using WinVi or G-Vim to create the files FileZilla was adding some form of extra characters to the end of my file when I transferred it to the server as im running Windows.

I switched to WinSCP as my connection agent and tried it and hey presto worked fine.

Thanks for the other answers.

Lewis

Was it helpful?

Solution 2

Ok i've managed to fix this so will leave this up for anyone else who looks around.

My problem was that I was using FileZilla to connect to my server. It seems that even though I was using WinVi or G-Vim to create the files FileZilla was adding some form of extra characters to the end of my file when I transferred it to the server as im running Windows.

I switched to WinSCP as my connection agent and tried it and hey presto worked fine.

OTHER TIPS

before running the bash script use the dos2unix command on it

dos2unix <bashScript>
./<bashScript>

Do you have a newline after the done? That might account for the trouble.

Does it work with the bash in Cygwin on your machine? (It should; it works fine when copied to my Mac, for example, with any of sh, bash, or ksh running it.)

I also got this problem. Actually the solution for this problem is while writing script check in edit menu EOL Conversion is Unix format or not. It should be Unix format for shell script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top