Lun*_*oms 27 linux shell makefile gnu-make
我在执行linux工具时有一个makefile规则.我需要检查工具命令的退出状态,如果该命令失败,则必须中止make.
我试过用$ ?, $$来检查?\ $?makefile中的etc等.但是当makefile运行时,它们会给我语法错误.
这样做的正确方法是什么?
这是Makefile中的相关规则
Run Code Online (Sandbox Code Playgroud)mycommand \ if [ $$? -ne 0 ]; \ then \ echo "mycommand failed"; \ false; \ fi
sus*_*tus 44
在makefile中:
mycommand || (echo "mycommand failed $$?"; exit 1)
Run Code Online (Sandbox Code Playgroud)
makefile操作中的每一行都调用一个新shell - 必须在命令失败的操作行中检查错误.
如果mycommand失败,则逻辑分支到echo语句,然后退出.
c24*_*24w 15
以下是其他几种方法:
shell & .SHELLSTATUSsome_recipe:
@echo $(shell echo 'doing stuff'; exit 123)
@echo 'command exited with $(.SHELLSTATUS)'
@exit $(.SHELLSTATUS)
Run Code Online (Sandbox Code Playgroud)
输出:
$ make some_recipe
doing stuff
command exited with 123
make: *** [Makefile:4: some_recipe] Error 123
Run Code Online (Sandbox Code Playgroud)
它确实有一个警告,即shell命令输出没有流式传输,因此当它完成时你最终会转储到stdout.
$?some_recipe:
@echo 'doing stuff'; exit 123;\
EXIT_CODE=$$?;\
echo "command exited with $$EXIT_CODE";\
exit $$EXIT_CODE
Run Code Online (Sandbox Code Playgroud)
输出:
$ make some_recipe
doing stuff
command exited with 123
make: *** [Makefile:2: some_recipe] Error 123
Run Code Online (Sandbox Code Playgroud)
它基本上是一串shell命令,用分号分隔.逃避任何你想要的新线路是令人讨厌的,并且很容易忘记分号,但我采用这种方法纯粹是因为上面提到的警告.