Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify Xcode Command Line Tools Install in Bash Script

Tags:

bash

xcode

macos

I am creating a bash script to setup a development environment. As part of my script I need to install Xcode Command Line Tools and I don't want the script to continue execution until the installation has been completed.

When I run:

xcode-select --install

It prints that an install has been requested or that it has already been installed. I want to be able to wait until the message changes to already being installed.

The relevant part of my script is as follows:

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

Unfortunately $check is always empty because xcode-select --install does not return anything and instead echoes the message to the terminal.

like image 937
Teddy Sterne Avatar asked Oct 19 '25 00:10

Teddy Sterne


1 Answers

I know you have probably already solved this issue, I had a crack at this problem today, and discovered that it prints to stderr and not stdout. This is the code I used to determine whether Xcode is installed or not:

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

Hope this helps someone in the future :)

like image 141
skandebaba Avatar answered Oct 20 '25 14:10

skandebaba