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

4 REPLIES 4

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