Looping in Bash: syntax error: unexpected end of file

I already entered into this Bash / Shell material and got some network analysis for uni assignment.

Just trying to make a simple loop, but getting a weird error that could not be fixed despite a 2 hour google scan:

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

I tried using a for loop using the following syntax:

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

Whenever I put 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 ran the following commands to add permissions and make an executable:

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

As a note, I found the following loop stack stack question, copied the โ€œfixed codeโ€ and got the same error: Looping a Bash Shell Script

Im running version 4+ and using G-VIM as a text editor in Windows 7. Has anyone got any ideas?

MANAGED BY THE DECISION OF THIS MYSELF:

Seeing that my reputation is still too low, I canโ€™t answer this question right now to stop people wasting time on how I fixed it:

Well, I managed to fix it, so he will leave it for everyone who is looking around.

My problem was that I used FileZilla to connect to my server. It seems that although I used WinVi or G-Vim to create the files, FileZilla added some extra characters to the end of my file when I transferred it to the server when I started Windows.

I switched to WinSCP as my communication agent and tried it, and it worked fine fine.

Thanks for the other answers.

Lewis

+4
source share
4 answers

Well, I managed to fix it, so he will leave it for everyone who is looking around.

My problem was that I used FileZilla to connect to my server. It seems that although I used WinVi or G-Vim to create the files, FileZilla added some extra characters to the end of my file when I transferred it to the server when I started Windows.

I switched to WinSCP as my communication agent and tried it, and it worked fine fine.

+1
source

before running bash script use dos2unix command on it

 dos2unix <bashScript> ./<bashScript> 
+2
source

Do you have a newline after done ? This can cause problems.

Does it work with bash in Cygwin on your computer? (It must work fine when copying to my Mac, for example, with any of sh , bash or ksh running it.)

0
source

I also had this problem. In fact, the solution to this problem when writing a script is to check in the EOL Conversion edit menu whether this is a Unix format or not. This should be the Unix format for the shell script.

0
source

Source: https://habr.com/ru/post/1399653/


All Articles