如何从Lua脚本中确定系统的操作系统? [英] How can I determine the OS of the system from within a Lua script?

查看:205
本文介绍了如何从Lua脚本中确定系统的操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要从Lua脚本确定系统的操作系统,但是Lua本身对此没有API,因此我使用os.getenv()并查询环境变量.在Windows上,检查环境变量"OS"可以得到系统OS的名称,但是Windows和大多数Unix版本上是否都存在可以检查的变量?

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?

推荐答案

在Unix系统上,尝试os.capture'uname',其中os.capture定义如下:

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

这将对所有版本的unix和Mac OSX有所帮助. 如果失败,您可能在Windows系统上?或检查os.getenv'HOME'.

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'.

这篇关于如何从Lua脚本中确定系统的操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆