Jamf Pro - brew upgade command using scripts section

rahz0092
New Contributor II

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

5 REPLIES 5

abuehler
New Contributor III

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

abuehler
New Contributor III

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

rahz0092
New Contributor II

Thanks @abuehler .. I will test it now and let you.

rahz0092
New Contributor II

I have checked and script is working for me. Thanks man. @abuehler 

easyedc
Valued Contributor II

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.