Lua os.execute返回值 [英] Lua os.execute return value

查看:1224
本文介绍了Lua os.execute返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Lua的局部变量中读取以下内容?

Is it possible to read the following from the local variable in Lua?

local t = os.execute("echo 'test'")
print(t)

我只想实现这一点:通过ox.execute执行的任何操作都将返回任何值,我想在Lua中使用它-例如echo 'test'将在bash命令行中输出test-是能否将返回值(在这种情况下为test)获取到Lua局部变量?

I just want to achieve this: whatever is executed via the ox.execute and will return 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆