我经常发现Bash语法非常有用,例如diff <(sort file1) <(sort file2)
中的进程替换。
可以在Makefile中使用这样的Bash命令吗?我在想这样的事情:
file-differences:
diff <(sort file1) <(sort file2) > $@
在我的GNU Make 3.80中,这将给出一个错误,因为它使用shell
而不是bash
来执行命令。
发布于 2009-02-26 06:04:22
从GNU Make文档中,
5.3.1 Choosing the Shell
------------------------
The program used as the shell is taken from the variable `SHELL'. If
this variable is not set in your makefile, the program `/bin/sh' is
used as the shell.
因此,将SHELL := /bin/bash
放在makefile的顶部,就可以运行了。
顺便说一句,你也可以为一个目标这样做,至少对GNU Make是这样。每个目标都可以有自己的变量赋值,如下所示:
all: a b
a:
@echo "a is $$0"
b: SHELL:=/bin/bash # HERE: this is setting the shell for b only
b:
@echo "b is $$0"
这将打印出来:
a is /bin/sh
b is /bin/bash
有关详细信息,请参阅文档中的“特定于目标的变量值”。这一行可以放在Makefile中的任何位置,它不一定要紧跟在目标之前。
发布于 2009-02-26 06:03:32
您可以使用-c
标志直接调用bash
:
bash -c "diff <(sort file1) <(sort file2) > $@"
当然,您可能无法重定向到变量$@,但是当我尝试这样做时,我得到了一条错误消息-bash: $@: ambiguous redirect
,因此您可能希望在深入了解之前查看一下(尽管我使用的是bash3.2,所以您的可能工作方式有所不同)。
发布于 2009-02-26 06:11:43
您可以直接在Makefile中调用bash,而不是使用默认的shell:
bash -c "ls -al"
而不是:
ls -al
https://stackoverflow.com/questions/589276
复制相似问题