Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Lua中等待进程完成?_Lua_Popen - Fatal编程技术网

如何在Lua中等待进程完成?

如何在Lua中等待进程完成?,lua,popen,Lua,Popen,我必须在Lua中使用io.popen,以运行一个带有命令行参数的可执行文件。 如何在Lua中等待进程完成,以便捕获预期的输出 local command = "C:\Program Files\XYZ.exe /all" hOutput = io.popen(command) print(string.format(""%s", hOutput)) 假设可执行文件是XYZ.exe,需要使用命令行参数/all调用它 一旦执行了io.popen(command),进程将返回一些需要打

我必须在Lua中使用
io.popen
,以运行一个带有命令行参数的可执行文件。 如何在Lua中等待进程完成,以便捕获预期的输出

  local command = "C:\Program Files\XYZ.exe /all"

  hOutput = io.popen(command)
  print(string.format(""%s", hOutput))
假设可执行文件是XYZ.exe,需要使用命令行参数
/all
调用它

一旦执行了
io.popen(command)
,进程将返回一些需要打印的字符串

我的代码片段:

function capture(cmd, raw)
  local f = assert(io.popen(cmd, 'r'))
  -- wait(10000); 
  local s = assert(f:read('*a')) 
  Print(string.format("String: %s",s )) 
  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 
local command = capture("C:\Tester.exe /all")

非常感谢您的帮助。

如果您使用的是标准Lua,那么您的代码看起来有点奇怪。我不能完全确定关于超时或平台依赖性的
io.popen
语义,但以下内容至少在我的机器上有效

local file = assert(io.popen('/bin/ls -la', 'r'))
local output = file:read('*all')
file:close()
print(output) -- > Prints the output of the command.

我有一个代码,不知怎的,它不能正常工作函数捕获(cmd,raw)本地f=assert(io.popen(cmd,'r'))--等待(10000);local s=assert(f:read('*a'))Print(string.format(“string:%s”,s))f:close()如果原始,则返回s end s=string.gsub(s,“^%s+”,”)s=string.gsub(s,“[\n\r]+”,”)返回s end local command=capture(“C:\Tester.exe/all”)您能解释一下,为什么需要
assert
around?@rubo77,我已经有一段时间没有使用Lua了,但是基于一个快速测试,我认为在这种情况下,
assert
根本没有用,因为
io.popen
似乎会在出现错误时返回一个对象。