简体   繁体   English

如何从LUAJ执行Linux终端命令?

[英]How to execute a linux terminal command from LUAJ?

I want to simply execute a linux terminal command like ls from LuaJ and the result that it will return or anything that returns i want to receive it and will show the names in the Java Gui . 我只想简单地执行一个linux terminal command例如来自LuaJ ls ,结果将返回或返回任何返回的结果,我希望接收它并在Java Gui显示名称。 I searched but found this but not one with LuaJ . 我搜索了但找到了,LuaJ没有找到。

Is there any function to execute the terminal command from LuaJ ?? 有什么功能可以执行LuaJ的终端命令吗?

There are multiple ways to do this, for one, you can implement it yourself in Java then link it to LuaJ. 有多种方法可以执行此操作,其中一种方法是,您可以用Java自己实现它,然后将其链接到LuaJ。

LuaFunction command = new OneArgFunction()
{
    public LuaValue call(LuaValue cmd)
    {
        Process p = Runtime.getRuntime().exec("/bin/sh", "-c", cmd.checkstring());
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        int returnCode = p.waitFor();
        return LuaValue.valueOf(returnCode);
    }
}
globals.set("command", command);

Then in Lua: 然后在Lua中:

local code = command("ls");

The problem with actually getting the output of a command is that you can't just have a fixall solution. 实际获取命令输出的问题在于,您不能仅仅拥有一个固定解决方案。 For all the system knows you could be calling a program which runs for 2 hours generating constant output, which could be an issue, not to mention if the program requires input. 众所周知,您可能正在调用一个程序,该程序运行2个小时会产生恒定的输出,这可能是个问题,更不用说该程序是否需要输入了。 If you know you're only going to use certain functions you can make a dirty version of above function to capture the output from the stream and return it all instead of the exit code, just don't use it on other processes that don't return quickly. 如果您只打算使用某些功能,则可以制作上述功能的肮脏版本,以捕获流中的输出并返回所有输出而不是退出代码,只是不要在没有此功能的其他进程上使用它不能很快回来。 The other alternative is to create a class that wraps the input and output streams from the process and return a coerced version of that class, and manage the input and output from lua. 另一种选择是创建一个类,该类包装来自流程的输入和输出流,并返回该类的强制版本,并管理lua的输入和输出。

Lua does have a function that's part of the OsLib called execute(), if execute doesn't exist in your current environment then in Java call: Lua确实具有OsLib的一部分函数execute(),如果当前环境中不存在execute,则在Java调用中:

globals.load(new OsLib());

Before loading the lua code. 在加载lua代码之前。 the os.execute() function returns the status code, and doesn't return the streams, so no way to get the output there. os.execute()函数返回状态代码,并且不返回流,因此无法在那里获得输出。 To get around this you can modify the command to pipe the output to a temp file and open it with the io library (new IoLib() if doesn't exist in current environment). 为了解决这个问题,您可以修改命令以将输出通过管道传递到临时文件,并使用io库(如果当前环境中不存在新的IoLib())将其打开。

The other option is to use io.openProcess, which also executes the command and returns a file to read the output from. 另一个选项是使用io.openProcess,它还执行命令并返回一个文件以读取输出。

Resources: 资源:

http://luaj.org/luaj/3.0/api/org/luaj/vm2/lib/OsLib.html http://luaj.org/luaj/3.0/api/org/luaj/vm2/lib/OsLib.html

http://luaj.org/luaj/3.0/api/org/luaj/vm2/lib/IoLib.html http://luaj.org/luaj/3.0/api/org/luaj/vm2/lib/IoLib.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM