本文介绍了执行从Java,看了Shell脚本运行shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有看过一个输入shell脚本
#!/斌/庆典
回声键入要检查(4位)年份,然后按[ENTER]:
今年读
回声$一年
我执行使用的Java API这个shell脚本
的ProcessBuilder PB =新的ProcessBuilder(/斌/ bash的,/junk/leaptest.sh);
最后一道工序流程= pb.start();InputStream为= process.getInputStream();
InputStreamReader的ISR =新的InputStreamReader(是);
BR的BufferedReader =新的BufferedReader(ISR);
串线;
而((行= br.readLine())!= NULL){
的System.out.println(线);
}
的System.out.println(程序终止!);
在Java控制台我可以看到输出
Now the Actual Problem in How to pass the values to the Shell Scripts in my scripts how the varialble "year" can be read
I have edited the code as per the suggestion but doesn't work where we correct it
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!");
解决方案
To pass values from java program that executes script to the script use command line arguments. If you want to send information back from script to java program print the value in script, read the script's STDOUT in java program and parse it.
You really almost there. Now you are reading the script output (into while loop) but you are just printing it. Parse the output and do what you need with it.
这篇关于执行从Java,看了Shell脚本运行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!