Skip to content

Instantly share code, notes, and snippets.

@federicocappelli
Last active January 22, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save federicocappelli/c0778d20598f83b000fb525f431d5e30 to your computer and use it in GitHub Desktop.
Save federicocappelli/c0778d20598f83b000fb525f431d5e30 to your computer and use it in GitHub Desktop.
#!/bin/bash
#author Federico Cappelli - github.com/federicocappelli - 2021
if [ "$1" = "-h" ] || [ "$1" = "" ]; then
echo
echo "Select the command line tools for a specific Xcode version"
echo "Please place your Xcodes in /Applications or ~/Applications and name them like Xcode_10.3.app"
echo "examples:"
echo "./xcodeselect.sh 11"
echo "./xcodeselect.sh 10.3"
echo "./xcodeselect.sh latest"
echo
exit
fi
case $1 in
"latest")
appSuffix=""
;;
*)
appSuffix="_$1*"
;;
esac
developerFolderPath="/Applications/Xcode$appSuffix.app/Contents/Developer"
developerFolderPathUser="~/Applications/Xcode$appSuffix.app/Contents/Developer"
function doesAnyFileExist {
local arg="$*"
local files=($arg)
[ ${#files[@]} -gt 1 ] || [ ${#files[@]} -eq 1 ] && [ -e "${files[0]}" ]
}
if doesAnyFileExist $developerFolderPath; then
echo "Selecting xcode (at $developerFolderPath)"
sudo xcode-select --switch $developerFolderPath
else
if doesAnyFileExist $developerFolderPathUser; then
echo "Selecting xcode at $developerFolderPath"
sudo xcode-select --switch $developerFolderPathUser
else
echo "Error: Xcode $1 doesn't exist at $developerFolderPath"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment