SIGPIPE 13

Get OS Version From Scripts

August 1st, 2009

It is sometimes useful to have a script check the OS version, for example the way to get the user’s full name was previously done using niutil but Apple removed that command in Leopard (it can now be done using dscl).

One BSD command to read OS properties is sysctl but prior to Leopard it didn’t offer kern.osrelease making it a bit awkward, as you had to keep track of how kern.osversion maps to a more humane number.

Lo and behold! Today I stumbled over sw_vers (x-man-page links fail when there is an underscore in the URL, so type man sw_vers in Terminal, rdar://7111174).

The manual page has no history, but the date is 2003 and an example in the manual page has 10.2.4 as output, so it goes way back.

[by Allan Odgaard]


6 Responses to “Get OS Version From Scripts”

  1. Brent Gulanowski Says:
    August 7th, 2009 at 21:27

    Little gems. If you only support 10.4 and later, dscl already existing in Tiger (alongside niutil, which was deprecated).

  2. Nigel Kersten Says:
    August 13th, 2009 at 12:52

    You actually should be using dscl, even in Tiger.

    nicl/niutil/etc can only look at the local NetInfo database. You however may have users whose records are not in the local database, but are being included via Active Directory, LDAP, Novell, NIS, whatever.

    Developers often forget to do any testing at all with network or mobile accounts on OS X.

  3. Nick Peelman Says:
    August 13th, 2009 at 13:04

    Been using:

    sw_vers | grep 'ProductVersion' | awk '{ print $2 }'

    in scripts for a while now :) Works great!

  4. Nigel Kersten Says:
    August 13th, 2009 at 14:29

    Nick, ever tried:

    sw_vers -productVersion

    ? :)

    My comment before also sounded a bit harsh. It's difficult for small developers to test directory service integrated clients. I would assume Apple's testing labs have these environments set up, but I've never been to one.

  5. tyches Says:
    August 18th, 2009 at 12:55

    $ uname -a Darwin Onyx.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386

    With a little mapping, it should give you everything you need…

  6. Yvo Says:
    September 7th, 2009 at 05:07

    uname -sr will get OS and version.


Leave a Reply