[英]Unable to capture command exit code in makefile
我正在嘗試設置我的第一個makefile,並在步驟1中遇到一個塊。在我的shell腳本中,我這樣做了:
which brew | grep 'brew not found' >/dev/null 2>&1
if [ $? == 0 ]; then
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
這作為一個bash腳本工作得很好。 經過一些谷歌搜索,對於一個Makefile,我到目前為止已經提出了這個命令:
BREW_INSTALLED = $(shell which brew | grep 'brew not found' >/dev/null 2>&1; echo $$?)
然而,運行它得到了我
make: BREW_INSTALLED: No such file or directory
我同樣不確定何時應該將@
添加到命令中(看起來像我不想輸出的任何內容?)。
我目前正在使用GNU Make 3.81
。
典型的makefile會檢查是否存在所需的工具,如下所示:
BREW := $(shell which brew)
# Check if variable brew is empty
ifeq ($(BREW),)
$(error brew not found)
else
$(info brew found: $(BREW))
endif
all:
@echo "Do something with brew"
$(BREW) --version
注意:前兩個縮進行中必須沒有制表空間。 如果所有 規則必須使用制表符縮進,則兩個配方行。
食譜開頭的@
抑制回應: https : //www.gnu.org/software/make/manual/html_node/Echoing.html
這一行有幾種可能性:
BREW_INSTALLED = $(shell which brew | grep 'brew not found' >/dev/null 2>&1; echo $$?)
如果成功, 這在標准錯誤將其輸出到標准輸出,在發生故障時的情況。 您正在嘗試捕獲stdout上的錯誤消息。
為了養活它的標准錯誤到grep,你需要寫
which brew 2>&1 >/dev/null | grep 'brew not found'
( 2>&1
和>
的順序也很重要)。
但是,你不應該依賴於它的特定錯誤消息。
但是你已經讓你從它想要的返回碼,所以你不需要的grep的。
返回失敗參數的數量,或者在沒有給出`programname'時返回-1。 https://linux.die.net/man/1/which
考慮使用grep -q 'expression'
來壓縮輸出而不是重定向stdout和stderr。
-q, - quiet, - silent
安靜; 不要寫任何標准輸出。 如果發現任何匹配,則立即退出零狀態,即使檢測到錯誤也是如此。 https://linux.die.net/man/1/grep
你得到的錯誤信息與我上面寫的內容無關。 這意味着shell正在嘗試將BREW_INSTALLED作為命令運行,這可能意味着make將它放在新shell的開頭。 也許你是在tabspace之后寫的? 請參閱https://www.gnu.org/software/make/manual/html_node/Recipe-Syntax.html
要捕獲返回碼(作為字符串!):
BREW_INSTALLED := $(shell which brew >/dev/null 2>&1; echo $$?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.