Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shurkin18
Last active May 23, 2023 11:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurkin18/62ec34967794a32f9d63615db881ab5c to your computer and use it in GitHub Desktop.
Save shurkin18/62ec34967794a32f9d63615db881ab5c to your computer and use it in GitHub Desktop.
Installs brew via script, then installs/loads jq JSON parser
###########################################################################################
# There is also an alternative way of running jq JSON parser, without installing the whole brew suite
# You can download the jq binary here: https://techstoreon.com/files/jq-osx-amd64
# Pre-load it to each mac via the policy and store it somewhere (in /var for example) and just point your script to it
# every time jq needs to be used
###########################################################################################
#!/bin/bash
# Checks if jq is not already present, and if not - installs brew via script, then installs/loads jq JSON parser
#
# Modified by Aleksandr Kamenev / shurkin18@gmail.com | used some of the data from this script: https://github.com/kennyb-222/AutoBrew/
#
# If you need to fully uninstall brew, run this script: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
# Check if js JSON parser is already present on the mac and if so - exit out of the script
if [ ! -z $(which jq) ]; then
echo "jq JSON parser is already installed on the machine, exiting the script."
exit 1
fi
# Set environment variables
HOME="$(mktemp -d)"
export HOME
export USER=root
export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
BREW_INSTALL_LOG=$(mktemp)
# Get current logged in user
currentuser=`stat -f '%u %Su' /dev/console | awk '{ print $2 }'`
echo "Current logged in user is: $currentuser"
# Ensure currentuser isn't empty
if [ -z "${currentuser}" ]; then
echo "'currentuser' is empty. You must specify a user!"
exit 1
fi
# Install Homebrew | strip out all interactive prompts
/bin/bash -c "$(curl -fsSL \
https://raw.githubusercontent.com/Homebrew/install/master/install.sh | \
sed "s/abort \"Don't run this as root\!\"/\
echo \"WARNING: Running as root...\"/" | \
sed 's/ wait_for_user/ :/')" 2>&1 | tee "${BREW_INSTALL_LOG}"
# Reset Homebrew permissions for current user
brew_file_paths=$(sed '1,/==> This script will install:/d;/==> /,$d' \
"${BREW_INSTALL_LOG}")
brew_dir_paths=$(sed '1,/==> The following new directories/d;/==> /,$d' \
"${BREW_INSTALL_LOG}")
# Get the paths for the installed brew binary
brew_bin=$(echo "${brew_file_paths}" | grep "/bin/brew")
brew_bin_path=${brew_bin%/brew}
# shellcheck disable=SC2086
chown -R "${currentuser}":admin ${brew_file_paths} ${brew_dir_paths}
chgrp admin ${brew_bin_path}/
chmod g+w ${brew_bin_path}
# Unset home/user environment variables
unset HOME
unset USER
# Finish up Homebrew install as current user
su - "${currentuser}" -c "${brew_bin} update --force"
# Run cleanup before checking in with the doctor
su - "${currentuser}" -c "${brew_bin} cleanup"
sleep 1
# Give the necessary folder permissions for current user, then install jq using brew as current user
currentuser=`stat -f '%u %Su' /dev/console | awk '{ print $2 }'`
chown -R $currentuser /usr/local/lib
chown -R $currentuser /usr/local/sbin
sleep 2
su "$currentuser" -c "brew install jq"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment