在Makefile中回显退出代码

时间:2019-11-16 22:54:15

标签: bash shell makefile

我已经检查了几则关于SO的帖子,但无法获得我想要的答案。

我想将Stata脚本作为Makefile的一部分运行。因此,在我的目标之一中,我定义了

make all

但是,每当我查看live_data命令(包括Executing Stata code StataIC -e stata_code.do' Finished execution of Stata code. Code finished with exit code of )的输出时,都会看到

exit_code

基本上,变量StataIC -e stata_code.do始终为空。

但是,如果我弹出一个终端并简单地运行exit_status=$?,然后依次运行echo $exit_status10,则会得到正确的结果(0或1)。有人可以指出我在这里到底在想什么吗?

2 个答案:

答案 0 :(得分:1)

您有几个问题。首先,正如@AProgrammer所指出的那样,配方中的每一行都是在其自己的子外壳中执行的,因此,一行中设置的变量无法保存到下一行:

trial:
    @var=blue
    @echo this command has no access to what was stored in var

解决方案是将命令放在一行中

trial:
    @var=blue ; echo this command can use what was stored in var

如果太长,请用反斜杠换行:

trial:
    @var=blue ;\
  echo this command can use what was stored in var

(请注意,只有一个制表符,即@之前的那个;“ echo”左侧的空白只是使规则易于阅读的一些空格。)

第二,您必须注意语法。在Make中扩展变量的方法是$(var),而在Shell中扩展变量的方法是$var;在另一上下文中使用任何一种语法都不会给您想要的结果。

var=red
$(info $(var)) # this is a Make command

trial:
    @var=blue ; echo this is a shell command, so $(var) will not work

第三,尽管规则中的命令是shell命令,但Make会尝试扩展运行该规则之前 看到的任何变量,因此我们必须对“ $ var”中的'$'进行转义加上另一个“ $”:

trial:
    @var=blue ; echo $var will not work, but $$var will

将所有内容放在一起,我们得到:

live_data:
    @echo "Executing Stata code" ; \
  StataIC -e 'stata_code.do' ; \
  exit_status=$$? ; \
  echo "Finished execution of Stata code." ; \
  echo Code finished with exit code of $$exit_status

答案 1 :(得分:1)

请注意,您可以简化配方,从而无需保存$?的值。

live_data:
    @echo "Executing Stata code"; \
      StataIC -e 'stata_code.do'; \
      printf '%s\n' "Finished execution of Stata code." \
                    "Code finished with exit code of $$?"

这里,两个字符串都是在printf之后立即调用的单个StataIC的参数,因此$?正确地引用了StataIC的退出状态。