Posted on 04-02-2024 10:35 PM
Hi Everyone,
I am new to Jamf Pro and i need a bash script to upgrade brew in our environment. Command i want to run - "brew upgrade xz". Could someone please help me with the shell script on priority? Thanks
Posted on 04-03-2024 06:26 AM
Hi @rahz0092
Try this one:
#!/bin/bash
# Check, where brew is installed
brewbin=$(which brew); echo "$brewbin"
# Variable for storing the current users name
currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print $3 }')
# Run brew update and patch xz in user context
/usr/bin/su "$currentUser" -c "$brewbin update" > /dev/null 2>&1
/usr/bin/su "$currentUser" -c "$brewbin upgrade xz"
exit 0
However, this script does not check if homebrew is actually installed. So if it isn't, it will throw an error.
Hope this helps :-)
Regards, Adi
Posted on 04-03-2024 06:36 AM
Forget the first one. Just noticed I did a mistake there with the brew checking.
This should do the trick (including detecting whether or not brew is installed):
#!/bin/bash
# Variable for storing the current users name
currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print $3 }')
# Check, where brew is installed
brewbin="$(/usr/bin/sudo -i -u "$currentUser" /usr/bin/which brew)"
if [ -z "$brewbin" ]
then
echo "brew not found. Cancel..."
exit 0
else
# Run brew update and patch xz in user context
/usr/bin/su "$currentUser" -c "$brewbin update" > /dev/null 2>&1
/usr/bin/su "$currentUser" -c "$brewbin upgrade xz"
fi
exit 0
Posted on 04-03-2024 09:38 PM
Thanks @abuehler .. I will test it now and let you.
Posted on 04-03-2024 10:03 PM
I have checked and script is working for me. Thanks man. @abuehler
Posted on 06-25-2024 01:47 PM
Happened to come across this while working the same issue. I did notice one thing to make it slightly more portable.
#!/bin/sh
# Homebrew Updater.sh
#
# Created by Ed C on 6/24/24.
#
# Grabbed from https://community.jamf.com/t5/jamf-pro/jamf-pro-brew-upgade-command-using-scripts-section/td-p/313170
# https://docs.brew.sh/FAQ#is-there-a-glossary-of-terms-around
#
# Variable for storing the current users name
currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ { print $3 }')
# Identify which Brew Formula to upgrade. If left blank, will attempt to update ALL formulae
formula=$4
# Check, where brew is installed
brewbin="$(/usr/bin/sudo -i -u "$currentUser" /usr/bin/which brew)"
if [ -z "$brewbin" ]
then
echo "brew not found. Cancel..."
exit 0
else
# Run brew update and patch xz in user context
/usr/bin/su "$currentUser" -c "$brewbin update" > /dev/null 2>&1
/usr/bin/su "$currentUser" -c "$brewbin upgrade $4"
fi
exit 0
Dropping in a variable=$4 allows you to just drop the formula into your policy's script field, or leave blank to apply to all.