gpt4 book ai didi

linux - 为什么 bash 在通过 make 执行时不支持 !(...) 模式(w/SHELL :=/bin/bash)?

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:42 24 4
gpt4 key购买 nike

我在 Makefile 中运行一行在 /bin/bash 中运行良好的代码时遇到问题。我知道 Make 在 /bin/sh 中运行,所以我有 solved that problem with this guide通过向 Make 声明我希望它在 Bash 中运行。然而,即使我在 Bash 中运行 Make,我仍然无法让这条线工作,即使它在 Bash 中工作得很好。

为什么 Bash 在 Makefile 中运行 rm -f !(2).txt 时会抛出错误,而在其 shell 中执行时却不会?

EDIT: I seek to delete all files with a certain file extension except one of those files. The command rm -f !(2).txt does just that in my bash terminal but not in Make.

EDIT: When running rm -f !(2).txt "normally" in the terminal with Bash (terminal returned bash when issuing command echo "$0") it works just fine but whey I run /bin/bash -c 'rm -f !(2).txt' I get the same error as in the Makefile.

生成文件

all: clean

clean: SHELL:=/bin/bash
clean:
rm -f !(2).txt

在 Bash 中运行命令

 $ ls
1.txt 2.txt 3.txt 4.txt Makefile # Directory has all text files
$ rm -f !(2).txt # Remove all text files but 2.txt
$ ls
2.txt Makefile # Only 2.txt is not removed

通过 Makefile 在 Bash 中运行命令

 $ ls                                   # Directory has all text files
1.txt 2.txt 3.txt 4.txt Makefile
$ make clean # Running make
rm -f !(2).txt
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `rm -f !(2).txt'
make: *** [clean] Error 1

最佳答案

您需要启用 extglob shell 选项才能使其工作。在GNU make中,可以通过

SHELL := /bin/bash
.SHELLFLAGS := -O extglob -c

由问题作者编辑

上面两行代码的示例对我不起作用,但作为 this fellow found out ,似乎有不同的解决方案,具体取决于版本。

Hmm. For some reason the .SHELLFLAGS didn't work for me, but putting the flags directly in SHELL=/bin/bash -O extglob -c did. [...] – Timothy Jones

例子@choroba使用在 Make 4.0 中有效但对我不​​起作用,因为我有 Make 3.81。但是,这是我的解决方案:

all: clean

clean: SHELL=/bin/bash -O extglob -c
clean:
rm -f !(2).txt

关于linux - 为什么 bash 在通过 make 执行时不支持 !(...) 模式(w/SHELL :=/bin/bash)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42189388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com