Last active
May 24, 2024 06:42
-
-
Save rtud/b77867cd1095d88f23759d7803eb9a6a to your computer and use it in GitHub Desktop.
Bash script that automates upgrading brew packages in a non-admin/admin user setup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
########################################################################### | |
# The following is a workaround for upgrading brew packages when you have | |
# separate admin and non-admin users on a macOS device | |
########################################################################### | |
# Copyright (c) 2021 Radu Tudorie (rtud) | |
# | |
# MIT License | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
########################################################################### | |
# Edit the following variables with your standard & admin macOS usernames | |
STANDARD_USER="rtud" | |
ADMIN_USER="rtud-admin" | |
BREW_DIR="/usr/local" | |
TERM=xterm-256color | |
# Color Defs | |
F_RESET=$(tput sgr0) | |
F_BOLD=$(tput bold) | |
F_RED=$(tput setaf 1) | |
F_GREEN=$(tput setaf 2) | |
F_BLUE=$(tput setaf 4) | |
F_WHITE=$(tput setaf 7) | |
# Icon Definitions | |
I_GOOD="${F_BOLD}${F_GREEN}✔${F_RESET}" | |
I_BAD="${F_BOLD}${F_RED}✖${F_RESET}" | |
I_INFO="${F_BOLD}${F_BLUE}✻${F_RESET}" | |
I_TOOLS="${F_BOLD}${F_WHITE}⚒${F_RESET}" | |
########################################################################### | |
# To replicate the ASCII art: | |
# - visit: http://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=vim | |
# - select the "ANSI Shadow" font | |
echo -e " " | |
echo -e "${F_WHITE}${F_BOLD} ██████╗ ██████╗ ███████╗██╗ ██╗ ${F_RESET}" | |
echo -e "${F_WHITE}${F_BOLD} ██╔══██╗██╔══██╗██╔════╝██║ ██║ ${F_RESET}" | |
echo -e "${F_WHITE}${F_BOLD} ██████╔╝██████╔╝█████╗ ██║ █╗ ██║ ${F_RESET}" | |
echo -e "${F_WHITE}${F_BOLD} ██╔══██╗██╔══██╗██╔══╝ ██║███╗██║ ${F_RESET}" | |
echo -e "${F_WHITE}${F_BOLD} ██████╔╝██║ ██║███████╗╚███╔███╔╝ ${F_RESET}" | |
echo -e "${F_WHITE}${F_BOLD} ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚══╝╚══╝ ${F_RESET}\n" | |
if [ "$(whoami)" != "$ADMIN_USER" ]; then | |
echo -e "\n${I_BAD} ${F_WHITE}${F_BOLD}You must run this script as an admin.${F_RESET}" | |
echo -e "${I_INFO} Make sure you've edited the script and configured the ${F_BOLD}STANDARD_USER${F_RESET} and ${F_BOLD}ADMIN_USER${F_RESET} variables as per your macOS setup." | |
echo -e "${I_INFO} To gain access to an admin shell, type ${F_BOLD}su - $ADMIN_USER${F_RESET} in the terminal, then type in the admin password when prompted.\n" | |
else | |
echo -e "${I_TOOLS} ${F_WHITE}${F_BOLD}Updating the system's brew packages. Please wait...${F_RESET}" | |
echo -e "${I_INFO} The admin user's password might be required to perform the update.\n" | |
if [ "$(stat -f "%A" "$BREW_DIR/bin")" != '755' ]; then | |
echo -e "${F_WHITE}${F_BOLD}->${F_RESET} Removing group and others permissions on the ${F_BOLD}$BREW_DIR/*${F_RESET}...\n" | |
sudo chmod -R g-w,o-w $BREW_DIR/* | |
else | |
echo -e "${F_WHITE}${F_BOLD}->${F_RESET} Permissions for ${F_BOLD}$BREW_DIR/*${F_RESET} are already setup accordingly.\n" | |
fi | |
if [ "$(stat -f "%Su" "$BREW_DIR/bin")" != "$ADMIN_USER" ]; then | |
echo -e "${F_WHITE}${F_BOLD}->${F_RESET} Temporarily switching ownership for ${F_BOLD}$BREW_DIR/*${F_RESET} to the admin user..." | |
sudo chown -R $ADMIN_USER:admin $BREW_DIR/* | |
else | |
echo -e "${F_WHITE}${F_BOLD}->${F_RESET} Ownweship of ${F_BOLD}$BREW_DIR/*${F_RESET} was already set to the admin user.\n" | |
fi | |
echo -e "\n${F_WHITE}${F_BOLD}->${F_RESET} Checking in with brew for any updated packages...\n" | |
brew update | |
echo -e "\n${F_WHITE}${F_BOLD}->${F_RESET} Upgrading the outdated brew pacakages...\n" | |
brew upgrade | |
echo -e "\n${F_WHITE}${F_BOLD}->${F_RESET} Upgrading the outdated brew cask pacakages...\n" | |
brew upgrade --cask $(brew list --cask) | |
echo -e "\n${F_WHITE}${F_BOLD}->${F_RESET} Switching back ownership for ${F_BOLD}$BREW_DIR/*${F_RESET} to the standard user...\n" | |
sudo chown -R $STANDARD_USER:admin $BREW_DIR/* | |
echo -e "\n${I_GOOD} ${F_WHITE}${F_BOLD}Brew packages have been updated.${F_RESET}" | |
echo -e "${I_INFO} Please check the output of brew upgrade and see if there are any manual steps that need running after the update.\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment