How can I determine the OS of the system from within a Lua script?

前端 未结 5 1329
离开以前
离开以前 2021-02-12 12:36

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 en

5条回答
  •  梦毁少年i
    2021-02-12 13:34

    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
    

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

提交回复
热议问题