Lua os.execute()不起作用 [英] Lua os.execute() does not work

查看:1173
本文介绍了Lua os.execute()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lua os.execute()命令的问题。



我只想回声一个字,写入一个文件,如 echo'aword'> C:\folder\tempworkspace\foo 尝试os.execute()命令。目录C:\folder\tempworkspace存在,foo是我想要命令创建并填充aword的文件的名称。
以后,当这个工作,我想使用 R -q -erbinom(1000,1,0.7)>调用R。 C:\folder\tempworkspace\foo



我已经尝试了
Lua编程 - os.execute()在Windows中不起作用和为什么不会将应用程序程序文件在lua中使用os.execute运行?$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

当我在命令提示符中直接键入这些命令时,它们工作。我使用Windows 7 Professional作为管理员,Lua 5.1.4使用Eclipse。



这是我迄今为止所尝试的:

  os.execute(echo'hehe'> C:\folder\tempworkspace\foo)
os.execute [[echo 'hehe'> C:\folder\tempworkspace\foo]]
os.execute [[echohehe'>'C:\folder\tempworkspace\foo'] ]
os.execute [[echo'hehe'> C:\folder\tempworkspace\foo]]
os.execute [[echo'hehe'> C:\\folder\\tempworkspace\\foo]]
os.execute [[cmd.exe / c echo'hehe'> C:\folder\tempworkspace\foo]]
os.execute(cmd.exe / c echo'hehe'> C:\\folder\\tempworkspace\\foo )
os.execute(cmd.exe / c echo'hehe'>'C:\\\folder\\tempworkspace\\foo')

我将非常感谢任何有关改进我的代码的建议。 (注意:我正在写一个文件,因为我以后要在lua中使用输出。另一种使用io.popen()的方法已经被建议在某个地方,但据说它依赖于平台,反正我当我尝试使用 x = io.popen(R-q -e'rnorm(10)'))时,Lua崩溃。



在第一个答案之后修改:



您的sendMsg功能在我的电脑上也不起作用,我没有收到任何错误,我真的想知道有什么问题。对于pcall来说,我这样做是对的吗?由于行 print(err)不打印任何东西,我想知道我是否正常进行。

  function sendMsg(cmd,msg,fpath)
local output = cmd .... msg ..>.. fpath
print(output)
os.execute(输出)
end
函数sendMsgArgs()
sendMsg(echo,huhu,C:\\\\\tempworkspace\\\ \\foo);
end
err = pcall(sendMsgArgs)
if err == true then
print(THIS WORKED)
else
print(THIS DID不工作)
print(err)
end

编辑:这是实际上是一个Eclipse /编辑器问题。所以我想添加一些标签,例如lua-eclipse,但是我不能。

解决方案

这段代码片段在我的Win7框上:

  function sendMsg(cmd,msg,fpath)
local output = cmd .. $ ...


sendMsg(echo,呵呵,C:\\\\\

您需要确保该文件存在,然后再尝试写入。你是从lua口译运行的吗?当您无法写入时,您是否收到任何错误消息?



另外 - 您可以使用io.open()并以这种方式写入。包装这个功能并使用pcall()调用它可能会给你更多的信息,如果你有某种窗口问题打开/阅读到该位置。

  local fout = io.open(C:\\\\\\\foo,w +)
fout:write(hehe)
fout:close()

使用pcall的示例:

 本地结果,错误= pcall(sendMsg,echo,huhu,C:\\merret\\tempworkspace\\ foo)
if result == false then
print(error)
else
print(success!)
end


I'm having a problem with the Lua os.execute() command.

I just want to echo a word and write it into a file, like echo 'aword' > C:\folder\tempworkspace\foo to try the os.execute() command. The direcory C:\folder\tempworkspace exists, "foo" is the name of the file I want the command to create and fill with "aword". Later, when this works, I'd like to call R, using R -q -e "rbinom(1000,1,0.7)" > C:\folder\tempworkspace\foo.

I've already tried all the advice provided in Lua programming - os.execute() is not working in Windows and Why won't applications in Program Files run using os.execute in lua? but my problem seems to be a different one, maybe not even in the syntax, but somewhere else?

When I type those command directly in the Command Prompt, they work. I use Windows 7 Professional as an administrator, and Lua 5.1.4 with Eclipse.

Here is what I have tried so far:

os.execute("echo 'hehe' > C:\folder\tempworkspace\foo")
os.execute [["echo 'hehe' > C:\folder\tempworkspace\foo"]]
os.execute [["echo 'hehe' > 'C:\folder\tempworkspace\foo'"]]
os.execute [[echo 'hehe' > C:\folder\tempworkspace\foo]]
os.execute [[echo 'hehe' > C:\\folder\\tempworkspace\\foo]]
os.execute[[cmd.exe /c echo 'hehe' > C:\folder\tempworkspace\foo]]
os.execute("cmd.exe /c echo 'hehe' > C:\\folder\\tempworkspace\\foo")
os.execute("cmd.exe /c echo 'hehe' > 'C:\\folder\\tempworkspace\\foo'")

I'd be very grateful for any suggestions to improve my code. (Note: I'm writing to a file, because I want to use the output later in lua. Another way of doing this, using io.popen() has been suggested somewhere, but it is said to be platform dependent, anyway my Lua crashes when I try to use x = io.popen("R -q -e 'rnorm(10)'")).

Edit, after first answer:

Your sendMsg function somehow also does not work on my computer, and I don't get any error, I really wonder what's the problem. For pcall, am I doing this right? As the line print(err) does not print anything I wonder if I am doing correctly.

function sendMsg(cmd, msg, fpath)
  local output = cmd.. " ".. msg.. " > ".. fpath
  print(output)
  os.execute(output)
end
function sendMsgArgs()
  sendMsg("echo", "huhu", "C:\\merret\\tempworkspace\\foo");
end
err = pcall(sendMsgArgs) 
if err == true then
  print("THIS WORKED")
else
  print("THIS DID NOT WORK")
  print(err)
end

Edit: This was actually a Eclipse/editor issue. So I wanted to add some tags, such as lua-eclipse, but I can't.

解决方案

this snippet of code works on my Win7 box:

function sendMsg(cmd, msg, fpath)
    local output = cmd.. " ".. msg.. " > ".. fpath
    print(output)
    os.execute(output)
end

sendMsg("echo", "hehe", "C:\\path\\to\\foo");

you need to make sure the file exists before you attempt to write to it. are you running this from the lua interpreter? do you get any error messages back when it fails to do the write?

alternatively - you can just use io.open() and write that way. wrapping this in a function and calling it using pcall() may give you more information if you have some sort of windows issue opening/reading to that location.

local fout = io.open("C:\\path\\to\\foo", "w+")
fout:write("hehe")
fout:close()

an example using pcall:

local result, error = pcall(sendMsg, "echo", "huhu", "C:\\merret\\tempworkspace\\foo")
if result == false then
    print(error)
else
    print("success!")
end 

这篇关于Lua os.execute()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆