Get OS Version From Scripts
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.
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).
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.
August 13th, 2009 at 13:04
Been using:
sw_vers | grep 'ProductVersion' | awk '{ print $2 }'
in scripts for a while now :) Works great!
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.
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…
September 7th, 2009 at 5:07
uname -sr will get OS and version.