Skip to main content
Solved

Help with a script that checks macOS version


howie_isaacks
Forum|alt.badge.img+23

Can anyone tell me what I'm doing wrong? I'm trying to write a script that checks the version of macOS so I can have it trigger a Jamf policy based on the version of macOS that the computer is running. I wanted to account for all versions of Sonoma and all versions of Ventura.  I keep getting "integer expression expected" errors. I used shellcheck.net and it didn't find any problems. I'm testing this in CodeRunner. I also tested it in Terminal. I thought I knew how to do this! I have even looked at some other scripts that I have that use similar syntax. They work.

#!/bin/zsh macOSVersion=$(/usr/bin/sw_vers | grep "ProductVersion" | /usr/bin/awk '{ print $2 }') echo "$macOSVersion" if [ "$macOSVersion" -ge 14 ] && [ "$macOSVersion" -lt 15 ]; then echo "This Mac is running macOS Sonoma" elif [ "$macOSVersion" -ge 13 ] && [ "$macOSVersion" -lt 14 ]; then echo "This Mac is running macOS Ventura" else echo "This Mac is running a macOS earlier than Ventura" fi

Best answer by mm2270

When checking if a value is greater than or less than in zsh, you need to use double brackets in your test. So something like this.

if [[ "$macOSVersion" -ge 14 ]] && [[ "$macOSVersion" -lt 15 ]]; then

Do the same for the elif line. Try it then. It shouldn't generate that error. The single brackets can only be used for string checks, not integer ones.

View original
Did this topic help you find an answer to your question?

6 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • Answer
  • February 6, 2024

When checking if a value is greater than or less than in zsh, you need to use double brackets in your test. So something like this.

if [[ "$macOSVersion" -ge 14 ]] && [[ "$macOSVersion" -lt 15 ]]; then

Do the same for the elif line. Try it then. It shouldn't generate that error. The single brackets can only be used for string checks, not integer ones.


howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 773 replies
  • February 6, 2024

Thanks! I later changed my variable to just the major version of macOS "14", "13", etc. that worked. I will also try your suggestion. This was driving me nucking futs!


Forum|alt.badge.img+3
  • New Contributor
  • 7 replies
  • February 7, 2024

Just curious about why you would use the script in order to run a policy based on macOS version instead of scoping the policy to a smart group based on macOS version?


Forum|alt.badge.img+15
  • Esteemed Contributor
  • 719 replies
  • February 7, 2024

Instead of grepping for ProductVersion, you can just run:

sw_vers -productVersion

 


Corey_Nechkash
Forum|alt.badge.img+12
Fwatson wrote:

Just curious about why you would use the script in order to run a policy based on macOS version instead of scoping the policy to a smart group based on macOS version?


I've used a function like this inside of a script that I use to apply Dock settings. It has to determine the OS version so that it knows which version of System Settings (or System Preferences) is needed. It's easier to use a function like this rather than create 3 different versions of the same script.


howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 773 replies
  • February 8, 2024
Fwatson wrote:

Just curious about why you would use the script in order to run a policy based on macOS version instead of scoping the policy to a smart group based on macOS version?


The reason is because this is for a policy that deploys our screen savers. I don't want to maintain two different policies. The policy works by first checking what macOS version is running, then it runs the appropriate script for that OS. We still have many Macs running Ventura. Most of our Macs are running Sonoma. I found a good solution for screen savers for Sonoma. Obviously there are more ways than one to do things.


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