在Lua中获取os.execute的输出

Dre*_*mer 42 lua

当我在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.


小智 26

我想你想要这个http://pgl.yoyo.org/luai/i/io.popen io.popen.但它并不总是编入.