Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get containing path of lua file

Tags:

I am wondering if there is a way of getting the path to the currently executing lua script file?

This is specifically not the current working directory, which could be entirely different. I know luafilesystem will let me get the current working directory, but it doesn't seem to be able to tell the current executing script file.

Thanks

EDIT: I'm not running from the standard command line interpreter, I am executing the scripts from a C++ binary via luabind.

like image 326
radman Avatar asked Jun 17 '11 02:06

radman


People also ask

Where are Lua modules stored?

The standard ELT development environment provides some lua files to set default variables ( PATH , LD_LIBRARY_PATH …). There are located under: /elt/System/modulefiles.

How do I view a .LUA file?

Software developers typically open and modify LUA files with source code editors, such as Microsoft Visual Studio Code and MacroMates TextMate. Plain text editors, which include Microsoft Notepad and Apple TextEdit, may also be used to open and modify LUA files.

What is Dofile Lua?

The dofile function is actually an auxiliary function; loadfile does the hard work. Like dofile , loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.


2 Answers

This is a more elegant way:

function script_path()    local str = debug.getinfo(2, "S").source:sub(2)    return str:match("(.*/)") end  print(script_path()) 
like image 138
anthonygore Avatar answered Sep 22 '22 17:09

anthonygore


If the Lua script is being run by the standard command line interpreter, then try arg[0].

like image 35
lhf Avatar answered Sep 23 '22 17:09

lhf