我想使用带有PS4环境变量集的Java在调试模式下运行我的bash脚本。
ProcessBuilder pb = new ProcessBuilder("/bin/bash" ,"-x", "/junk/leaptest.sh");
Map<String, String> env = pb.environment();
env.put("PS4", "${BASH_SOURCE}:::${LINENO}:::COV:");
final Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
PrintWriter pw = new PrintWriter(process.getOutputStream());
String line;
while ((line = br.readLine()) != null) {
System.out.println("line no--->>" + line);
pw.println("2000");
pw.flush();
}
我得到的输出因为它没有在调试模式下运行且我的PS4 env没有设置
当我使用Runtime命令
时,同样的事情起作用String[] cmd = new String[3];
cmd[0] = "/bin/bash"; // should exist on all POSIX systems
cmd[1] = "-xv";
cmd[2] = args[0];
String[] env = new String[1];
env [0] = "PS4=${BASH_SOURCE}:::${LINENO}::: COV:";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + cmd[1]);
Process proc = rt.exec(cmd,env);
答案 0 :(得分:1)
当bash
与-x
一起运行时,它会将跟踪信息写入标准错误,而不是标准输出。您需要使用以下内容捕获它:
PrintWriter pwe = new PrintWriter(process.getErrorStream());