2016-07-01 114 views

回答

3

自卸stdout,測試退出狀態,以及錯誤出在失敗:

ifneq (0,$(shell >/dev/null command doesnotexist ; echo $$?)) 
    $(error "not good") 
endif 

這裏是失敗的樣子:

[[email protected]]$ make 
/bin/sh: doesnotexist: command not found 
Makefile:6: *** not good. Stop. 
[[email protected]]$ 

如果你想看到stdout,那麼你可以將它保存到一個變量和只測試lastword

FOOBAR_OUTPUT := $(shell echo "I hope this works" ; command foobar ; echo $$?) 
$(info $$(FOOBAR_OUTPUT) == $(FOOBAR_OUTPUT)) 
$(info $$(lastword $$(FOOBAR_OUTPUT)) == $(lastword $(FOOBAR_OUTPUT))) 
ifneq (0,$(lastword $(FOOBAR_OUTPUT))) 
    $(error not good) 
endif 

wh ich給出

$ make 
/bin/sh: foobar: command not found 
$(FOOBAR_OUTPUT) == I hope this works 127 
$(lastword $(FOOBAR_OUTPUT)) == 127 
Makefile:12: *** not good. Stop. 
+1

謝謝,這工作得很好,但我怎麼讓它也顯示標準輸出控制檯以及stderr? –

+0

@CameronMartin,優點。我用一個技巧修改了我的答案,以捕獲'stdout'並通過'lastword'測試。 – rubicks