题
当我执行“os.execute”时在Lua中,控制台快速弹出,执行命令,然后关闭。但是有没有办法只使用标准的Lua库来获取控制台输出?
解决方案
我想你想要这个 http://pgl.yoyo.org/luai/i /io.popen io.popen。但它并不总是在编译中。
其他提示
如果您有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+如果您没有io.popen,那么您的系统可能无法使用popen(3),并且您正在使用深酸奶。但是所有unix / mac / windows Lua端口都有io.popen。
, '') s = string.gsub(s, '[\n\r]+', ' ') return s end
如果您没有io.popen,那么您的系统可能无法使用popen(3),并且您正在使用深酸奶。但是所有unix / mac / windows Lua端口都有io.popen。
我不知道Lua具体但你通常可以运行命令:
comd >comd.txt 2>&1
将输出和错误捕获到文件comd.txt,然后使用语言文件I / O函数读取它。
如果语言本身没有提供捕获标准输出和错误的话,我就是这样做的。
不隶属于 StackOverflow