bash - 在 GNU Make 4 中使用 "bash -e"作为 shell 时,rm -f 因丢失文件而失败

标签 bash makefile gnu-make

我有一个如下所示的 Makefile:

.ONESHELL:
SHELL = /bin/bash
.SHELLFLAGS = -e

clean:
    rm -f foo

.PHONY: clean
foo不存在。当我在 Debian、GNU Make 4.1 上运行它时,我收到此错误:
rm -f foo
/bin/bash: rm -f foo: No such file or directory
Makefile:6: recipe for target 'clean' failed
make: *** [clean] Error 1

当我在 macOS 上运行它时,GNU Make 3.81 它按预期工作。如果我写 -rm -f 没有区别而不是 rm -f .

为什么我会收到错误消息?运行 bash -e -c "rm -f foo" ; echo $?在 shell 打印 0在两个系统上。

我错过了什么吗?

最佳答案

必须非常仔细地阅读错误消息:

/bin/bash: rm -f foo: No such file or directory

这不是来自 rm 的消息说找不到foo ,这是来自 Bash 的消息,告诉它找不到文件 rm -f foo (文件名中有空格)。

.SHELLFLAGS = -e ,shell 获得的内容与命令行相同:
bash -e "rm -f foo"

所以它寻找一个文件 rm -f foo从中读取命令。您需要明确添加 -c标志告诉它将参数本身作为命令,即 .SHELLFLAGS = -ec , 导致
bash -ec "rm -f foo"

不出所料,default value.SHELLFLAGS包含 -c :

The argument(s) passed to the shell are taken from the variable .SHELLFLAGS. The default value of .SHELLFLAGS is -c normally, or -ec in POSIX-conforming mode.

关于bash - 在 GNU Make 4 中使用 "bash -e"作为 shell 时,rm -f 因丢失文件而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47286460/

相关文章:

Bash: "printf %q $str"在脚本中删除空格。 (备择方案?)

linux - 转换针对Linux和Bash的Makefile

makefile - GNU Make 的多通配符模式规则

linux - 使用 Bash 将两个字符之间的所有字符串提取到数组

python - Cmd 和 Git bash 在运行 Python 代码时有不同的结果

c++ - ld : library not found for -lX11

linux - makefile 中的虚假目标

c++ - 在 Ubuntu 上使用 Eclipse C++ 时的 makefile 问题

gnu-make - 能否从其配方中发出信号表明目标尚未重建?

linux - 如何使用 awk 交换和删除列而不获取换行符和空格?