[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Obtain the lua script's (absolute/real) dirname and file name from within the script itself.
- From: Sean Conner <sean@...>
- Date: Tue, 21 Jan 2020 21:54:24 -0500
It was thus said that the Great Hongyi Zhao once stated:
>
> Run the following script under terminal:
>
> obtain-script-path.lua
> -------
> cmd = os.execute("pwd") and "pwd" or "cd"
^^^^^^^^^^^^^^^^
This is excuting the command "pwd", and the output will go to stdout.
> f = io.popen(cmd,"r")
^^^^^^^^^^^^^^^^
This is also executing the command "pwd", only the output will be piped to
Lua.
> 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?
You can either just set cwd to "pwd", or
os.execute("pwd >/dev/null") -- adjust according to OS
But that sill executes the command twice.
-spc