Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 899
Robert Gould Avatar asked Nov 17 '08 07:11

Robert Gould


People also ask

What is a Lua state?

The lua_State is basically a way to access what's going on in the Lua "box" during execution of your program and allows you to glue the two languages together.

What is G in Lua?

In Lua, the global variable _G is initialized with this same value. ( _G is never used internally.) When Lua loads a chunk, the default value for its _ENV upvalue is the global environment. Therefore, by default, free names in Lua code refer to entries in the global environment.

What is an environment in Lua?

In Lua, objects of type thread, function and userata can be associated with a table called environment. The environment is also a regular table. It can operate like a normal table and store various variables related to the object. The environment on the associated thread s can only be accessed through C code.


2 Answers

You can try package.config:sub(1,1). It returns the path separator, which is '\\' on Windows and '/' on Unixes...

like image 101
mnicky Avatar answered Oct 07 '22 19:10

mnicky


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

like image 32
Norman Ramsey Avatar answered Oct 07 '22 20:10

Norman Ramsey