执行从Java,看了Shell脚本运行shell脚本 [英] Executing Shell Scripts from Java, Shell scripts having read operation

查看:156
本文介绍了执行从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控制台我可以看到输出


  

键入要检查(4位)年份,然后按[Enter键]:


现在在如何将值传递给我的脚本中的Shell脚本如何varialble年可以读取的实际问题。


我已经编辑了code根据该建议,但不工作,我们纠正

 的ProcessBuilder PB =新的ProcessBuilder(/斌/ bash的,-c,/junk/leaptest.sh);
最后一道工序流程= pb.start();
InputStream为= process.getInputStream();
InputStreamReader的ISR =新的InputStreamReader(是);
BR的BufferedReader =新的BufferedReader(ISR);
串线;
/ *
 * OutputStream的OS = process.getOutputStream(); PrintWriter的PW =新
 *为PrintWriter(OS);
 * /BufferedWriter将体重=新的BufferedWriter(新OutputStreamWriter(process.getOutputStream()));
而((行= br.readLine())!= NULL){
    的System.out.println(线);
    // pw.println(8999);
    bw.write(2012);
}
的System.out.println(程序终止!);


解决方案

要通过从执行脚本的脚本中使用命令行参数的Java程序的值。如果你想发送的信息,从脚本到java程序后面打印脚本中的值,读剧本的标准输出Java程序并解析它。

您真的几乎没有。现在,你正在阅读脚本输出(入while循环),但你只是打印。解析输出,并做你需要做什么用。

I have a Shell Scripts that read the Input

#!/bin/bash
echo "Type the year that you want to check (4 digits), followed by [ENTER]:"
read year
echo $year  

I'm executing this shell scripts using 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!");

In the Java Console I can see the Output

Type the year that you want to check (4 digits), followed by [ENTER]:

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脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆