我想從java訪問UNIX shell但輸出不同,我不知道爲什麼。無法訪問java進程構建器的unix shell輸出
public class Test {
public static void main(String[] args) throws IOException,InterruptedException {
List<String> arg = new ArrayList<>();
arg.add("echo");
arg.add("$PATH");
ProcessBuilder pb = new ProcessBuilder(arg);
Process p = pb.start();
BufferedReader br;
if (p.waitFor() == 0) {
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
} else {
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
}
}
輸出:
$ PATH
然後我試圖讀取~/.bashrc
文件進行閱讀的路徑。
public class Test {
public static void main(String[] args) throws IOException,InterruptedException {
List<String> arg = new ArrayList<>();
arg.add("cat");
arg.add("<");
arg.add("~/.bashrc");
ProcessBuilder pb = new ProcessBuilder(arg);
Process p = pb.start();
BufferedReader br;
if (p.waitFor() == 0) {
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
} else {
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
}
}
輸出:
貓: '<':沒有這樣的文件或目錄
貓: '〜/ .bashrc中':沒有這樣的文件或目錄
OK,然後一個問題。爲什麼輸出爲''echo「,」$ PATH「'爲$ PATH? –
出於同樣的原因。 – rom1v