Skip to content

Instantly share code, notes, and snippets.

@liamnewmarch
Created February 3, 2020 11:46
Show Gist options
  • Save liamnewmarch/228bbc015a479483c4bf2870d9ef2051 to your computer and use it in GitHub Desktop.
Save liamnewmarch/228bbc015a479483c4bf2870d9ef2051 to your computer and use it in GitHub Desktop.
Interactive script to run brew doctor, brew upgrade and brew cleanup on macOS
#!/bin/bash
BLUE='\033[1;34m'
GREEN='\033[1;32m'
PURPLE='\033[1;35m'
RED='\033[1;31m'
RESET='\033[0m'
WHITE='\033[1;37m'
YELLOW='\033[1;33m'
# Homebrew paths (from `brew doctor`)
HOMEBREW_PATHS=$(echo /usr/local/{bin,etc,sbin,share,share/doc})
# First check with brew doctor
echo -e "${WHITE}Running ${PURPLE}brew doctor${RESET}"
brew doctor
# If there was a non-zero exit status offer to fix paths
if [ $? -ne 0 ]; then
while true; do
echo -e "\n${YELLOW}Warning:${WHITE} non-zero exit status from ${PURPLE}brew doctor${RESET}"
read -p "Attempt to fix homebrew paths? [Y/n] " yn
case $yn in
[Yy]*|'' )
# Warn the user we need to use sudo
echo -e "${WHITE}Claiming ownership of paths in ${BLUE}/usr/local/${RESET} (requires sudo)"
sudo chown -R $(whoami) $HOMEBREW_PATHS
chmod u+w $HOMEBREW_PATHS
break
;;
[Nn]*|* )
echo -e "${RED}Aborted${RESET}"
exit 0
;;
esac
done
fi
# Run the main brew update command
echo -e "\n${WHITE}Running ${PURPLE}brew upgrade${RESET}"
brew upgrade
# Clean up after with brew cleanup
echo -e "${WHITE}Running ${PURPLE}brew cleanup${RESET}"
brew cleanup
# We done!
echo -e "${GREEN}Success${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment