互动popen()Lua调用

互动popen()Lua调用,第1张

概述我正在尝试创建一个在后台运行 shell的程序,并将用户命令发送给它以便执行并返回结果.这是代码: --note: this runs on windows but I assume replacing "cmd" with "sh" it can run on linux as wellexe,err=io.popen("cmd > stdout.txt 2> stderr.txt");if 我正在尝试创建一个在后台运行 shell的程序,并将用户命令发送给它以便执行并返回结果.这是代码:

--note: this runs on windows but I assume replacing "cmd" with "sh" it can run on linux as wellexe,err=io.popen("cmd > stdout.txt 2> stderr.txt");if not exe then    print("Could not run command. Error: "..err)    returnelse    print("Command run successfully... ready!")endstdout,err=io.open("stdout.txt","r")if not stdout then print("Could not open stdout: "..err) return endstderr,err=io.open("stderr.txt","r")if not stdout then print("Could not open stderr: "..err) return endfunction execute(str)    exe:write(str)    return stdout:read("*all") or stderr:read("*all") or "nil"endrepeat    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 trueprint "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: dirdirYou 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
解决方法 Lua仅依赖于ANSI C功能.因此io.popen使用 popen(3)功能.引用它的手册:

Since a pipe is by deFinition unIDirectional,the type argument may specify only reading or writing,not both; the resulting stream is correspondingly read-only or write-only.

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

您可以尝试使用Lua Ex API(wiki page here),而不是尝试解决io.popen问题,它提供了另一种生成API的过程,并允许您执行以下 *** 作:

-- popen2(),from http://lua-users.org/wiki/ExtensionProposalfunction 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_wrend-- usage:local p,i,o = assert(popen2("wc","-w"))o:write("Hello world"); o:close()print(i:read"*l"); i:close()p:wait()
总结

以上是内存溢出为你收集整理的互动popen()Lua调用全部内容,希望文章能够帮你解决互动popen()Lua调用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1219871.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存