相关文章推荐
欢乐的消防车  ·  java.lang.NoClassDefFo ...·  3 周前    · 
忐忑的口罩  ·  jupyterhub搭建及配置 - ...·  2 年前    · 
酒量小的牛排  ·  OpenSSL ...·  2 年前    · 
坚韧的馒头  ·  ASP.NET Core 中的 ...·  2 年前    · 

sh脚本中的错误,if语句给出了意外的文件结尾

0 人关注

我对Linux和shell脚本不是很熟悉,总之我尝试了一些我想添加到我的启动脚本中的命令,但遇到了一些问题。

我曾尝试用不同的方法来摆脱

: unexpected end of file" 
 command not found

in this

#!/bin/bash
DEBUG="$1"
if [ "$DEBUG" = "debug" ]; then
echo "Lets start in debug mode"

And I trie to run it from comm和line ike this

# sh myscript.sh debug

我也尝试过像这样修改if

if [ "$DEBUG" = "debug" ]
  echo ......

但错误仍然存在 我知道我可以像这样运行它,但它似乎没有满足。

 ./myscript.sh debug

请给我们一个提示或提示,我们将不胜感激! :)

2 个评论
你使用的是哪个文本编辑器?你能显示 cat -A script.sh 的输出吗?你更有可能使用Windows文本编辑器的DOS风格的结束语
你是对的 @Inian , 我在widows中使用了Winscp,然后把它移到我的Wirtual Box中。在移动它之后,我在Linux中使用VI来编辑文件。我的解决办法是使用你说的 "dos2unix"。
linux
if-statement
syntax-error
user2279869
user2279869
发布于 2018-01-23
2 个回答
Inian
Inian
发布于 2018-01-23
已采纳
0 人赞同

You need to convert your script using dos2unix . The script in question does not 脚本中有任何违规行为,可能导致报告的错误。

这个错误可能是因为从Windows机器/文本编辑器复制的CRLF结尾,在Linux/Unix机器下运行。通过运行 \r\n 将行尾(EOL)序列 \n 转换为仅仅【替换代码】。

dos2unix script.sh
    
Léo R.
Léo R.
发布于 2018-01-23
0 人赞同

要注意留白,例如,这对我来说是可行的(用双等号)。

#!/bin/bash
DEBUG="$1"
if [ "$DEBUG" == "debug" ]; then 
   echo "Lets start in debug mode"