从 makefile 管道两个命令不起作用

Piping two command from makefile is not working

我在 makefile

中使用以下命令
apps := $(shell fzr provide apps )
apps := $(subst ],,$(subst [,,$(apps)))

在命令中我正在获取值数组 并从中删除数组 []

我想在我的终端中运行这个命令,我使用下面的命令

fzr provide apps | (subst ],,$(subst [,,$(apps))) | $(apps)

我遇到了错误

bash: apps: command not found
bash: apps: command not found
bash: subst: command not found
bash: subst: command not found

我在这里错过了什么?

如果我 运行 只有

fzr provide apps

我明白了,有效

[app1 app2 app3]

想法是检查命令

apps := $(subst ],,$(subst [,,$(apps)))

它适用于 mac 但在 windows 它是 不是 ...

在 Makefile 中有效的命令在终端提示符下无效。

如果要删除 Bash 脚本中的前导和尾随方括号,请尝试

fzr provide apps |
sed 's/^\[//;s/\]$//'

如果你想把它放在 Makefile 中,请注意你需要将美元符号加倍(单个美元符号由 make 本身评估;加倍它通过文字shell).

的美元符号
apps := $(shell fzr provide apps | sed 's/^\[//;s/\]$$//')

您的 Makefile 使用 GNU Make 特有的语法;也许您在 Windows 上的 make 版本不是 GNU 兼容版本。

这是@tripleee 发布的脚本的较短版本:

fzr provide apps | tr -d '[]'

这会删除输入字符串中出现的 [] 个字符。

在您的 makefile 中,$(apps) 扩展为 makefile 变量的值 apps。这是在读取 makefile 时完成的,并且在执行 shell 命令之前替换值(因此 shell 永远不会看到 $(apps),而是看到 appval1,或其他应用恰好是。

在 shell 中(我假设 bash shell),$(apps) 表示 运行 命令 apps。如果你想要 shell 变量的值,你必须使用 ${apps}。此外,$(subst) 是一个 make 构造,而 shell 也不理解,因此您必须将其替换为某些东西(可能 sed)。

因此,在这种情况下,您需要执行以下操作:

fzr provide apps | sed "s/[][]//g"

注意:这会替换所有方括号,而不仅仅是前导和尾随。如果你只想做前导和尾随,请参阅 tripleee 的回答。