【问题标题】:Makefile executes $(shell command) unordered [duplicate]Makefile执行$(shell命令)无序[重复]
【发布时间】:2020-04-13 16:58:58
【问题描述】:

我的 Makefile 有问题:

  • 在 Makefile 的开头,会创建一个文件。
  • 我想使用“cat 命令”检索创建的文件的内容。
  • 我需要使用 $(shell cat) 执行,因为我必须在另一个命令中检索它。

这就像子外壳没有意识到该文件的创建并且没有找到它。 如果你看一下输出消息的顺序......首先,它正在执行$(shell cat) 命令,因为第一行是:"cat: test.txt: The file or directory does not exist"。

为什么它似乎在执行无序的命令...首先执行 $(shell cat)...

可以用下面这个愚蠢的例子来展示:

通过测试例程查看以下 Makefile:

test:
    rm -rf test.txt
    echo "Hello World" > test.txt
    echo "$(shell cat test.txt)"

执行make test输出如下:

cat: test.txt: The file or directory does not exist.
rm -rf test.txt
echo "Hello World" > test.txt
echo ""

如果你执行两次“make test”,你可以意识到Makefile的第二次执行正在回显文本“Hello World”......因为它在开始时执行$(shell cat test.txt)并且文件从一开始就存在“make test”执行……

关于正在发生的事情以及我必须如何继续实现我的目标有什么建议吗?

非常感谢!

【问题讨论】:

  • ...是的,当您告诉make 运行子进程而不是让它调用的 shell 执行它时,就会发生这种情况。您是否有理由想要在此处使用shell cat,而不是将子shell 推迟到由子进程而不是make 运行?
  • 我之所以使用shell cat是因为我有一个命令执行需要取文件的值。例如:curl -X POST -u ${userPass} -H ${headers} ${url} -d '{ "key": "'"$(shell cat pubkey.txt) ...}
  • 我的意思是。我不知道如何在另一个命令中执行cat。我在互联网上找到的唯一实现方法是使用 shell cat 执行来执行它。
  • 啊。如果您正在生成 JSON,最好不要使用 cat;最好使用jq 之类的东西,它知道如何修改文件内容以正确编码,即使它包含需要转义才能有效的字符。 curl ... -d "$(jq --rawfile pubkey pubkey.txt '{"key": $pubkey}')",f/e,但要在 makefile 中使用,$s 需要加倍。

标签: shell unix makefile cat subshell


【解决方案1】:

将您的$s 加倍以转义它们,因此shell 仍能以文字形式看到它们(因此由shell 执行,而不是在shell 调用之前由make 执行):

test:
    rm -rf test.txt
    echo "Hello World" > test.txt
    echo "$$(cat test.txt)"

【讨论】:

  • 非常感谢查尔斯,它就像一个魅力!
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多