lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If the Lua script resides in a different directory from the current directory where the lua interpreter started the script the path should be part of arg[0]. The path may be relative to current path or absolute. os.execute/io.popen method should give the current path. Combining this information should give the path of the script.

On Wed, Jan 22, 2020 at 7:40 AM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
Milind Gupta <milind.gupta@gmail.com> 于2020年1月22日周三 上午4:08写道:
>
> When the Lua script on most systems starts arg contains the command from which the script was started. I have this file:
>
> for k,v in pairs(arg) do
> print(k,v)
> end
>
> When I run it like:
> D:\>lua d:\commandArgs.lua
> 0       d:\commandArgs.lua
> -1      lua
>
> When I do:
> D:\>lua commandArgs.lua
> 0       commandArgs.lua
> -1      lua
>
> This works fine for me in Windows and Linux as well. In the second case we do not get the path. To get the path can be system dependent. If you have the os library and popen function in your lua version, 1 way to get the path is:
>
> cmd = os.execute("pwd") and "pwd" or "cd"

The issue with method is:

`pwd' will give the work dir which maybe or may not the dir the lua
script resides in.

But I want to obtain the dir which is the lua script resides in.

Regards

> f = io.popen(cmd,"r")
> path = f:read("*a")
> f:close()
>
> Milind
>
>
> On Mon, Jan 20, 2020 at 11:20 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>>
>> Hi,
>>
>> Is there any robust and convenient methods for me to obtain the lua
>> script's (absolute/real) dirname and file name from within the script
>> itself?
>>
>> Regards
>> --
>> Hongsheng Zhao <hongyi.zhao@gmail.com>
>> Institute of Semiconductors, Chinese Academy of Sciences
>> GnuPG DSA: 0xD108493
>>


--
Hongsheng Zhao <hongyi.zhao@gmail.com>
Institute of Semiconductors, Chinese Academy of Sciences
GnuPG DSA: 0xD108493