我想根据当前目录中的默认 makefile 是否包含某个目标在 shell 中运行某些操作。
#!/bin/sh
make -q some_target
if test $? -le 1 ; then
true # do something
else
false # do something else
fi
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为如果目标不存在,GNU make 会返回错误代码 2,否则返回 0 或 1。问题是没有以这种方式记录。这是男人的一部分:
-q, --question
``Question mode''. Do not run any commands, or print anything;
just return an exit status that is zero if the specified targets
are already up to date, nonzero otherwise.
Run Code Online (Sandbox Code Playgroud)
仅区分零/非零。这样做的正确方法是什么?
您应该阅读GNU make 手册而不是手册页:手册页只是一个摘要,而不是完整的定义。手册说:
\n\nThe exit status of make is always one of three values:\n\n0 The exit status is zero if make is successful\n\n2 The exit status is two if make encounters any errors. It will print messages\n describing the particular errors.\n\n1 The exit status is one if you use the \xe2\x80\x98-q\xe2\x80\x99 flag and make determines that\n some target is not already up to date.\nRun Code Online (Sandbox Code Playgroud)\n\n由于尝试创建不存在的目标是一个错误,因此在这种情况下您将始终获得退出代码 2。
\n