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