【发布时间】:2017-01-01 03:12:11
【问题描述】:
我正在尝试通过 Java 在我的 Ubuntu 机器上运行 bash 脚本。 bash 脚本将 2 个输入作为我作为数组传递的参数 但是,它似乎没有将array[0]和array[1]的值传递给bash脚本?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.omg.CORBA.portable.InputStream;
public class readBashScript {
public static void readBashScript() {
try {
String[] array = {"ys1","R"};
Process proc = Runtime.getRuntime().exec("var/www/red/marsh_webreset.sh /",array);
BufferedReader read = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
-
我在使用 Runtime.exec 时遇到了类似的问题。我认为我的解决方案是将参数直接包含在程序中以执行 String
-
推荐的方法是使用
ProcessBuilder,尽管使用shellshock这似乎是一段非常危险的代码。 -
当您直接在程序中说时,您是指在 bash 脚本中吗?因为我会实时听取用户的意见
-
不,我的意思是在“var/www/red/marsh_webreset.sh /”之后