我有一個shell腳本讀取執行從Java,Shell腳本Shell腳本已經閱讀操作
#!/bin/bash
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year
我使用的Java API
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/junk/leaptest.sh");
final Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("Program terminated!");
在Java執行這個shell腳本的輸入控制檯我可以看到輸出
類型要檢查(4位)的年,然後按[ENTER]:
現在如何將值傳遞給我的腳本中的shell腳本實際的問題是如何在varialble「年」可以讀
我已編輯的代碼爲每建議,但沒有按」 t工作在那裏我們糾正它
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "/junk/leaptest.sh");
final Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
/*
* OutputStream os = process.getOutputStream(); PrintWriter pw = new
* PrintWriter(os);
*/
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
// pw.println("8999");
bw.write("2012");
}
System.out.println("Program terminated!");
感謝Alexr解析輸出意味着什麼?如何獲得此 – anish