在lua中,没有找到相关判断操作系统的函数。因此相关设置一直手工设置,增加了环境配置的时间。
在luarocks模块中,有一个luarocks.site_config模块(一个lua文件),其安装时便设定了操作系统类型。
因此我们可以从这个模块获取操作系统:
示例:
require "luarocks.site_config"
local system = luarocks.site_config.LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l")
if system == "Darwin" then
-- do something darwin related
elseif system and system:match("^Windows") then
-- do something Windows related
end
print(system)
结果:
$ lua test_os.lua
Darwin