lua-users home
lua-l archive

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


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"
> f = io.popen(cmd,"r")
> path = f:read("*a")
> f:close()

Thanks a lot, it will do the trick.  But I've another question on the
above code, see me following testings:

Run the following script under terminal:

obtain-script-path.lua
-------
cmd = os.execute("pwd") and "pwd" or "cd"
f = io.popen(cmd,"r")
path = f:read("*a")
f:close()

print(path)
------

Will give me the following output:

$ lua obtain-script-path.lua
/home/werner/Public/Lmod
/home/werner/Public/Lmod

As you can see, the script print the path twice, but I only used one
print command.  How to disable the other one output line?

Regards



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