lua-users home
lua-l archive

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


Sean Conner <sean@conman.org> 于2020年1月22日周三 上午10:54写道:
>
> 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
                                         ^^^^^
   Do you mean `cmd' here?

>
>         os.execute("pwd >/dev/null") -- adjust according to OS
>
> But that sill executes the command twice.

If so, why you still suggest this method?

Regards
>
>   -spc
>
>


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