Skip to main content
Question

Jamf Pro - brew upgade command using scripts section


Forum|alt.badge.img+3

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

adibue
Forum|alt.badge.img+4
  • New Contributor
  • 17 replies
  • April 3, 2024

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


adibue
Forum|alt.badge.img+4
  • New Contributor
  • 17 replies
  • April 3, 2024

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

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 2 replies
  • April 4, 2024
adibue wrote:

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

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 2 replies
  • April 4, 2024
adibue wrote:

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

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


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 623 replies
  • June 25, 2024

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings