【问题标题】:bash script, syntax error: unexpected end of filebash 脚本,语法错误:文件意外结束
【发布时间】:2017-10-16 04:37:41
【问题描述】:

我想创建一个执行特定任务的 bash 脚本,同时检查是否具有特定名称的文件。我正在使用 UTF-8 编码并运行脚本“bash test.sh”。在 Ubuntu 机器上。

#!/bin/bash

echo "Starting..."
while true
do
  echo "In loop..."
  sleep 2

  if 
    -e "./droneControl.py"
  then
    break
  fi
done

echo "found..."

【问题讨论】:

  • 您的if 需要放在括号中:if [ -e "./droneControl.py" ]。另外你为什么要使用循环?
  • 您使用“-e”作为“if”的参数,它是“test”命令的参数。试试“if [[ -e ./drone control.py ]]”。
  • 仍然出现同样的错误
  • 我正在使用 while 循环,因为我希望它不断检查文件是否存在。
  • 请看:shellcheck.net

标签: bash syntax


【解决方案1】:

您需要使用dos2unix 或等效命令来转换您的脚本。

$ bash fromnotepad.sh
fromnotepad.sh: line 2: $'\r': command not found
Starting...
fromnotepad.sh: line 16: syntax error: unexpected end of file

$ sed 's/\r$//' fromnotepad.sh > fromunix.sh

$ bash fromunix.sh
Starting...
In loop...
found...

脚本:

$ cat fromunix.sh
#!/bin/bash

echo "Starting..."
while true
do
  echo "In loop..."
  sleep 2

  if [ -e "./ls.txt" ]
  then
    break
  fi
done

echo "found..."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2019-07-31
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多