最近写的lua脚本需要运行在多个平台,因而一些平台相关的属性必须区别设置。如路径分隔符。
在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

posted on 2013-03-18 18:28  #hanhui  阅读(369)  评论(0)    收藏  举报