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?
好吧我需要从Lua脚本确定系统的操作系统,但Lua本身没有API,所以我使用os.getenv()和查询环境变量。在Windows上检查环境变量“OS”给了我系统操作系统的名称,但是在Windows和大多数Unix上都有一些可以检查的变量吗?
5 个解决方案
#1
10
On a Unix system, try os.capture 'uname' where os.capture is defined below:
在Unix系统上,尝试os.capture'uname',其中os.capture定义如下:
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'.
这将有助于所有版本的unix和Mac OSX。如果失败,您可能在Windows系统上?或者查看os.getenv'HOME'。
#2
17
You can try package.config:sub(1,1)
. It returns the path separator, which is '\\'
on Windows and '/'
on Unixes...
您可以尝试package.config:sub(1,1)。它返回路径分隔符,在Windows上为'\\',在Unix上为'/'...
#3
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.
我想如果你只需要Windows / Unix检测,你可以检查文件系统是否存在/ etc或/ bin或/ boot目录。另外,如果你需要知道它是哪个发行版,大多数Linux发行版在/ etc中有一个小文件显示发行版和版本,遗憾的是它们都以不同的名称命名。
#4
4
When lua is compiled, it is configured slightly differently depending on what operating system it is compiled for.
编译lua时,根据编译的操作系统,它的配置略有不同。
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.
例如,当lua加载作为动态库分发的基于C的模块时,它必须知道用于这些库的扩展,这在每个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
#5
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).
Unix应该有$ HOME变量(而Windows没有),所以你可以检查它(在检查OS变量为空之后)。
#1
10
On a Unix system, try os.capture 'uname' where os.capture is defined below:
在Unix系统上,尝试os.capture'uname',其中os.capture定义如下:
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'.
这将有助于所有版本的unix和Mac OSX。如果失败,您可能在Windows系统上?或者查看os.getenv'HOME'。
#2
17
You can try package.config:sub(1,1)
. It returns the path separator, which is '\\'
on Windows and '/'
on Unixes...
您可以尝试package.config:sub(1,1)。它返回路径分隔符,在Windows上为'\\',在Unix上为'/'...
#3
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.
我想如果你只需要Windows / Unix检测,你可以检查文件系统是否存在/ etc或/ bin或/ boot目录。另外,如果你需要知道它是哪个发行版,大多数Linux发行版在/ etc中有一个小文件显示发行版和版本,遗憾的是它们都以不同的名称命名。
#4
4
When lua is compiled, it is configured slightly differently depending on what operating system it is compiled for.
编译lua时,根据编译的操作系统,它的配置略有不同。
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.
例如,当lua加载作为动态库分发的基于C的模块时,它必须知道用于这些库的扩展,这在每个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
#5
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).
Unix应该有$ HOME变量(而Windows没有),所以你可以检查它(在检查OS变量为空之后)。