Lua os.execute 返回值 [英] Lua os.execute return value
问题描述
是否可以从Lua中的局部变量中读取以下内容?
Is it possible to read the following from the local variable in Lua?
local t = os.execute("echo 'test'")
print(t)
我只想实现这一点:每当 os.execute
返回任何值时,我想在 Lua 中使用它 - 例如 echo 'test'
将输出 test
在 bash 命令行中 - 是否可以将返回值(在这种情况下为 test
)获取到 Lua 局部变量?
I just want to achieve this: whenever os.execute
returns any value, I would like to use it in Lua - for example echo 'test'
will output test
in the bash command line - is that possible to get the returned value (test
in this case) to the Lua local variable?
推荐答案
您可以使用 io.popen()
代替.这将返回一个文件句柄,您可以使用它来读取命令的输出.类似以下内容可能有效:
You can use io.popen()
instead. This returns a file handle you can use to read the output of the command. Something like the following may work:
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
请注意,这将包括命令发出的尾随换行符(如果有).
Note that this will include the trailing newline (if any) that the command emits.
这篇关于Lua os.execute 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!