【问题标题】:/bin/sh: -c: line 1: syntax error: unexpected end of file (using Makefile)/bin/sh: -c: 第 1 行:语法错误:文件意外结束(使用 Makefile)
【发布时间】:2023-03-25 17:16:01
【问题描述】:

我的团队在 Makefile 的末尾有一个目标,如下所示:

# Fix permissions and ownership problems (only run this in development!)
fixperms:
@if [ $(OS_NAME) == "Linux" ]; then \
    @sudo chown --recursive php:php .
    @sudo chmod --recursive 0777 var/
fi

@if [ $(OS_NAME) == "Darwin" ]; then \
    @sudo chown --recursive php:php .
    @sudo chmod -R 0777 var/
fi

... 它在末尾包含一个换行符。当我运行 make fixperms(在我的 macOS 机器上——我没有在 Linux 上尝试过)时,我收到以下消息:

/bin/sh: -c: line 1: 语法错误:文件意外结束

make: *** [fixperms] 错误 2

我该如何解决这个问题?

===

编辑1:我也试过不换行——结果是一样的。

===

编辑 2:我还尝试重新排列 Makefile,以便在末尾放置一个不同的目标。运行 make fixperms 仍然会导致相同的错误。很奇怪。

【问题讨论】:

  • 无论您想要完成什么,chmod 777 几乎肯定是错误的,并且是一个严重的安全问题。 恢复到正常的权限,可能还有所有权给需要能够写入此位置的特定系统用户。在最坏的情况下,如果您运行的生产系统对随机访问者具有写入权限,请执行安全审核并从已知良好的来源重新安装。

标签: macos makefile newline darwin


【解决方案1】:

我假设您在此处有缩进您的食谱的 TAB,而您没有显示。

问题是您在第一行的末尾只有一个反斜杠,所以这是唯一“继续”的行。 Make 在单独的 shell 中调用配方中的每个逻辑行,因此:

fixperms:
        @if [ $(OS_NAME) == "Linux" ]; then \
            @sudo chown --recursive php:php .
            @sudo chmod --recursive 0777 var/
        fi

运行以下 shell 命令:

/bin/sh -c 'if [ $(OS_NAME) == "Linux" ]; then @sudo chown --recursive php:php .'
/bin/sh -c 'sudo chmod --recursive 0777 var/'
/bin/sh -c 'fi'

你可以看到这些是非法的shell脚本;您在第一个中收到错误,因为您缺少 fi

你应该把它写成一个 shell 命令脚本,用反斜杠确保下一行包含在逻辑行中:

fixperms:
        @if [ $(OS_NAME) == "Linux" ]; then \
            sudo chown --recursive php:php .; \
            sudo chmod --recursive 0777 var/; \
        fi
        @if [ $(OS_NAME) == "Darwin" ]; then \
            sudo chown --recursive php:php .; \
            sudo chmod -R 0777 var/; \
        fi

(注意去掉脚本中间的 @ 以及那些是 makefile 配方元字符,并且只在配方的开头被识别)。

我实际上认为您应该删除所有@。至少在你的 makefile 100% 工作之前添加它们总是一个致命的错误,因为你丢弃了本来可以帮助你调试的关键信息。例如,如果您没有将它们放在此处,您会看到 make 打印出它要调用的行,它可能会帮助您了解问题所在。

你可以考虑这个:http://make.mad-scientist.net/managing-recipe-echoing/

【讨论】:

  • 我还应该指出,== 不是测试程序的 POSIX 标准比较运算符。你应该改用=,除非你在你的makefile中设置了SHELL = /bin/bash
  • 美元符号也需要加倍。
  • 哦,我以为这是一个命令,但我猜你是对的。
【解决方案2】:

我一直无法让 shell 条件起作用。我切换到使用 Makefile 条件,这似乎已经完成了这项工作。所以我现在有了这个:

# Fix permissions and ownership problems (only run this in development!)
fixperms:
ifeq ("$(OS_NAME)", "Linux")
    sudo chown --recursive php:php . ; sudo chmod --recursive 0777 var/
endif
ifeq ("$(OS_NAME)", "Darwin")
    sudo chown -R php:php . ; sudo chmod -R 0777 var/
endif

...这似乎运作良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 2016-05-30
    • 1970-01-01
    • 2013-12-24
    • 2014-07-31
    • 2013-02-03
    • 2015-03-29
    相关资源
    最近更新 更多