验证Xcode命令行工具以Bash脚本

时间:2015-05-29 15:08:10

标签: xcode macos bash

我正在创建一个bash脚本来设置开发环境。作为我的脚本的一部分,我需要安装Xcode命令行工具,我不希望脚本继续执行,直到安装完成。

当我跑步时:

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没有返回任何内容,而是将消息回传给终端。

1 个答案:

答案 0 :(得分:2)

我知道你可能已经解决了这个问题,今天我遇到了这个问题,发现它打印到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

希望这可以帮助将来的某个人:)

相关问题