2015-05-29 51 views
2

我正在創建一個bash腳本來設置開發環境。作爲我的腳本的一部分,我需要安裝Xcode命令行工具,並且我不希望腳本在安裝完成之前繼續執行。驗證Xcode命令行工具安裝在Bash腳本中

當我運行:

xcode-select --install 

它打印,如果要安裝已請求或者它已經安裝。我希望能夠等到消息更改爲已安裝。

我的劇本的相關部分如下:

check="$(xcode-\select --install)" 
echo "$check" 
str="xcode-select: note: install requested for command line developer tools\n" 
while [[ "$check" == "$str" ]]; 
do 
    check="$(xcode-\select --install)" 
    sleep 1 
done 

不幸$check永遠是空的,因爲xcode-select --install不返回任何東西,而是相呼應的消息發送到終端。

+0

難道輸出該消息到標準錯誤? 'xcode-select --install 2>&1'有幫助嗎? –

+0

在_「...我的腳本的相關部分...」中,您有'check =「$(xcode- \ select --install)」',不應該是'check =「$(xcode-選擇--install)「'?換句話說,'xcode-select'中的'-'和's'之間沒有_backslash_。 – user3439894

+0

沒有斜槓我在我的腳本的其餘部分有錯誤,因爲'select'是bash中的保留字 –

回答

1

我知道你可能已經解決了這個問題,我今天在這個問題上有一個破解,並且發現它打印到stderr而不是stdout。這是我用來確定是否安裝了Xcode中的代碼或不:

check=$((xcode-\select --install) 2>&1) 
echo $check 
str="xcode-select: note: install requested for command line developer tools" 
while [[ "$check" == "$str" ]]; 
do 
    osascript -e 'tell app "System Events" to display dialog "xcode command-line tools missing." buttons "OK" default button 1 with title "xcode command-line tools"' 
    exit; 
done 

希望這可以幫助別人的未來:)