简体   繁体   English

在Bash中循环:语法错误:意外的文件结束

[英]Looping in Bash: syntax error: unexpected end of file

Im new to to this Bash/Shell stuff and got to do some network analysis for a uni assignment. 我是这个Bash / Shell的新手,并为单一任务做了一些网络分析。

Just trying to do a simple loop but getting a weird error that have been unable to fix despite a 2 hour google crawl: 只是尝试做一个简单的循环,但得到一个奇怪的错误,尽管谷歌爬行2小时仍然无法解决:

#!/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: 我尝试使用以下语法使用for循环:

#!/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 正如旁注,我发现以下有关循环的stackoverflow问题,复制了“固定代码”并得到了同样的错误: 循环Bash Shell脚本

Im running version 4+ and using G-VIM as the text editor on Windows 7. Anyone got any ideas? 我正在运行版本4+并使用G-VIM作为Windows 7上的文本编辑器。任何人都有任何想法?

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. 我的问题是我使用FileZilla连接到我的服务器。 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. 似乎即使我使用WinVi或G-Vim来创建文件,FileZilla也会在我运行Windows时将其转移到服务器时在文件末尾添加某种形式的额外字符。

I switched to WinSCP as my connection agent and tried it and hey presto worked fine. 我切换到WinSCP作为我的连接代理并尝试了它,嘿presto工作正常。

Thanks for the other answers. 谢谢你的其他答案。

Lewis 刘易斯

before running the bash script use the dos2unix command on it 在运行bash脚本之前,请使用dos2unix命令

dos2unix <bashScript>
./<bashScript>

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. 我的问题是我使用FileZilla连接到我的服务器。 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. 似乎即使我使用WinVi或G-Vim来创建文件,FileZilla也会在我运行Windows时将其转移到服务器时在文件末尾添加某种形式的额外字符。

I switched to WinSCP as my connection agent and tried it and hey presto worked fine. 我切换到WinSCP作为我的连接代理并尝试了它,嘿presto工作正常。

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. 实际上这个问题的解决方案是在编辑菜单中编写脚本检查EOL转换是否为Unix格式。 It should be Unix format for shell script. 它应该是shell脚本的Unix格式。

Do you have a newline after the done ? done后你有新行吗? That might account for the trouble. 这可能是麻烦的原因。

Does it work with the bash in Cygwin on your machine? 它可以在你的机器上使用Cygwin中的bash吗? (It should; it works fine when copied to my Mac, for example, with any of sh , bash , or ksh running it.) (它应该;它复制到我的Mac时工作正常,例如,运行shbashksh任何一个。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM