23

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?

Robert Gould
  • 68,773
  • 61
  • 187
  • 272

6 Answers6

37

You can try package.config:sub(1,1). It returns the path separator, which is '\\' on Windows and '/' on Unixes...

mnicky
  • 1,308
  • 11
  • 24
10

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
  • 198,648
  • 61
  • 360
  • 533
7

When lua is compiled, it is configured slightly differently depending on what operating system it is compiled for.

Many of the strings which are set in the 'package' module can thus be used to distinguish which system it was compiled for.

For instance, when lua loads C-based modules which are distributed as dynamic libraries, it has to know the extension used for those libraries, which is different on each OS.

Thus, you can use a function like the following to determine the OS.

local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
if BinaryFormat == "dll" then
    function os.name()
        return "Windows"
    end
elseif BinaryFormat == "so" then
    function os.name()
        return "Linux"
    end
elseif BinaryFormat == "dylib" then
    function os.name()
        return "MacOS"
    end
end
BinaryFormat = nil
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
4

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
  • 330,807
  • 53
  • 334
  • 373
  • Detecting folders such as `/Applications` for MAC OS, `/home` (after trying MAC OS) for GNU/Linux and finaly `C:/Windows` and `C:/WINNT` is a good solution for me. – MARTIN Damien Jan 20 '13 at 09:06
2

FWIW, the simplest function I can think of:

function MyScript:OS()
    return package.config:sub(1,1) == "\\" and "win" or "unix"
end
Rai
  • 314
  • 1
  • 2
  • 9
1

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
  • 6,996
  • 4
  • 44
  • 81
  • 1
    On each windows version I've checked, there is no HOME environment variable. Try to do "echo %HOME%" from the command prompt. – friol Oct 21 '12 at 08:09
  • Windows 7(v 6.1, build 7601) has HOME, what OS version did you check? – Baiyan Huang Oct 21 '12 at 08:15
  • On windows 7 too. What does the "echo %HOME%" command print out? – friol Oct 21 '12 at 09:24
  • maybe that is because I have the cygwin installed – Baiyan Huang Nov 06 '12 at 06:40
  • 1
    My win7 pro 64-bit has `HOMEPATH` and `HOMEDRIVE` but no `HOME`. I don't have Cygwin. IMHO, checking the environment is going to be fragile since any user might have something like Cygwin present or have decided that `HOME` is convenient. Also, when running under Cygwin, is the system really Windows any more? This quickly becomes a thorny question. – RBerteig Apr 24 '13 at 17:37
  • I just spent an hour trying to debug a Lua script that uses the `$HOME` method of detecting the OS. For some reason my Windows environment got that set and chaos ensued. Find another way (the `package.config:sub(1,1)` mentioned above works well). – Perry May 16 '14 at 23:33
  • windows 10 and onwards uses powershell as the default shell, and thus has always had `$HOME` mean the same thing as it does on linux and mac. – airtonix Dec 26 '22 at 10:01