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

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

问题描述

好的,我需要从 Lua 脚本确定系统的操作系统,但 Lua 本身没有 API,所以我使用 os.getenv() 并查询环境变量.在 Windows 上,检查环境变量OS"给了我系统操作系统的名称,但是否有一些变量存在于 Windows 和大多数 Unix 版本中可以检查?

解决方案

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

<前>函数 os.capture(cmd, raw)本地 f = assert(io.popen(cmd, 'r'))本地 s = assert(f:read('*a'))f:关闭()如果原始然后返回 s 结束s = string.gsub(s, '^%s+', '')s = string.gsub(s, '%s+$', '')s = string.gsub(s, '[ ]+', ' ')返回结尾

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

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?

解决方案

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, '[

]+', ' ')
  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'.

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

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