4

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

3
  • 2
    The above code is fine. If you're editing with Windows, are you sure you're using the correct line-endings? Commented Mar 4, 2012 at 15:04
  • Hi Oli - I just fixed it please see my alteration to above description can't post an answer myself as too low rep. Thanks. Commented Mar 4, 2012 at 15:21
  • You have now enough reputation to move your solution to a proper answer and accept it to close this question. Please do. Commented Jun 13, 2014 at 8:26

4 Answers 4

2

before running the bash script use the dos2unix command on it

dos2unix <bashScript>
./<bashScript>
1
  • Thanks for the answer Aditya - I have tried that but dos2unix or fromdos arent installed on the online server and I have no admin rights. Also im using a text editor which dosen't add Windows CR which is what I thought was the initial problem. Commented Mar 4, 2012 at 15:16
1

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.

0

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.)

1
  • Hi Jonathan, thanks for your answer. No new line. However I just managed to run the script contents directly into bash and it worked fine so its not a syntax error with the code itself. I have just figured out what the problem is and am posting my own answer in a minute if your interested. Lewis Commented Mar 4, 2012 at 15:17
0

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.

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.