当我在Lua中执行"os.execute"时,控制台会快速弹出,执行命令,然后关闭.但是有没有办法只使用标准的Lua库来取回控制台输出?
Nor*_*sey 57
如果您有io.popen,那么这就是我使用的:
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
如果您没有io.popen,那么您的系统上可能没有popen(3),而且您正在使用深酸奶.但是所有unix/mac/windows Lua端口都有io.popen.