互动popen()Lua调用

时间:2012-01-03 17:53:35

标签: lua

我正在尝试创建一个在后台运行shell的程序,并将用户命令发送给它以便执行并返回结果。这是代码:

--note: this runs on windows but I assume replacing "cmd" with "sh" it can run on linux as well
exe,err=io.popen("cmd > stdout.txt 2> stderr.txt");
if not exe then
    print("Could not run command. Error: "..err)
    return
else
    print("Command run successfully... ready!")
end

stdout,err=io.open("stdout.txt","r")
if not stdout then print("Could not open stdout: "..err) return end
stderr,err=io.open("stderr.txt","r")
if not stdout then print("Could not open stderr: "..err) return end

function execute(str)
    exe:write(str)
    return stdout:read("*all") or stderr:read("*all") or "nil"
end

repeat
    print("COMMAND: ")
    userinput=io.read("*line")
    print("You entered: '"..userinput.."'")
    if userinput=="" then print "Empty line! Exiting program..." break end
    print("Result: "..execute(userinput))
until true

print "Closing..."
execute("exit")
print "1"
exe:close()
print "2"
stdout:close()
print "3"
stderr:close()
print "Finished!"

问题:退出程序时,它会挂起exe:close()来电。执行循环也表现得很奇怪(有时候我必须按几次输入userinput=io.read("*line")才能工作。

我用google搜索文件:close()是否也适用于io.popen()结果但未找到任何内容的文件句柄。但那个电话不会失败。它只是挂断了。换句话说,程序的输出是这样的:

Command run successfully... ready!
COMMAND: 
dir

dir
You entered: 'dirdir'
Result: Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\lua>
C:\lua>
C:\lua>
Closing...
1

1 个答案:

答案 0 :(得分:7)

Lua仅依赖于ANSI C功能。因此io.popen使用popen(3)函数。引自它的手册:

  

由于管道根据定义是单向的,因此type参数可以仅指定读取或写入,而不是两者;结果流相应地只读或只写。

您尝试通过将输出重定向到文件并同时打开该文件并在执行命令后从中读取来尝试解决此限制。但是,在这种情况下,您可能会遇到输出缓冲问题 - 我认为这就是您所遇到的问题。

您可以试用Lua Ex APIwiki page here),而不是尝试解决io.popen,它提供了另一种生成API的过程,并允许您执行以下操作:< / p>

-- popen2(), from http://lua-users.org/wiki/ExtensionProposal
function popen2(...)
  local in_rd, in_wr = io.pipe()
  local out_rd, out_wr = io.pipe()
  local proc, err = os.spawn{stdin = in_rd, stdout = out_wr, ...}
  in_rd:close(); out_wr:close()
  if not proc then
    in_wr:close(); out_rd:close()
    return proc, err
  end
  return proc, out_rd, in_wr
end
-- usage:
local p, i, o = assert(popen2("wc", "-w"))
o:write("Hello world"); o:close()
print(i:read"*l"); i:close()
p:wait()