是否可以使用 Rhino 执行外部 bash 脚本并将另一个文件设置为输入? 例如我需要用以下内容重写 bash 脚本(exec.sh):
somescript.sh <fileInput.txt
我试过很多方法都没有成功:
读取 fileInput.txt 作为输入流并传递给 shell:
var inputStream = new java.io.InputStream(fileInput.txt);
runCommand( "somescript.sh", inputStream);写作
"somescript.sh <fileInput.txt"
添加 bash 脚本并调用 runCommand():message = new FileUtils.writeStringToFile(helpfulScript, "somescript.sh
runCommand("bash", helpfulScript.getCanonicalPath());
抱歉纯粹的亮点,并感谢您提出任何想法的建议。
最佳答案
您需要将输入流作为对象的 input
属性传递。
var inputStream = new java.io.FileInputStream("fileInput.txt");
runCommand("somescript.sh", { input: inputStream });
如果 input
不是 InputStream
它将被转换为字符串并直接发送到命令。同样,您可以添加 output
和/或 err
属性来捕获命令的标准输出和标准错误 (documentation here)。
关于javascript - 如何使用 Mozilla Rhino 中文件的重定向输入运行 bash 脚本的 Command()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18361933/