views:

334

answers:

3

Ok I need to determine the system's OS from a Lua script, but Lua as such has no API for this, so I use os.getenv() and query enviromental variables. On Windows checking the enviromental variable "OS" gives me the name of the system's OS, but is there some variable that exists on both Windows and most flavors of Unix that can be checked?

+1  A: 

Unixes should have the $HOME variable (while Windows doesn't have that), so you can check it (after checking the OS variable is empty).

friol
+1  A: 

I guess that if you just need Windows/Unix detection, you could check the filesystem for the existence of /etc or /bin or /boot directories. Aditionally, if you need to know which distro is it, most Linux distros have a little file in /etc showing the distro and version, sadly they all name it differently.

Vinko Vrsalovic
+1  A: 

On a Unix system, try os.capture 'uname' where os.capture is defined below:

function os.capture(cmd, raw)
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  if raw then return s end
  s = string.gsub(s, '^%s+', '')
  s = string.gsub(s, '%s+$', '')
  s = string.gsub(s, '[\n\r]+', ' ')
  return s
end

This will help on all flavors of unix and on Mac OSX. If it fails, you might be on a Windows system? Or check os.getenv 'HOME'.

Norman Ramsey