java简单执行shell脚本_从Java执行Shell脚本,具有读取操作的Shell脚本

我有一个读取输入的Shell脚本

#!/bin/bash

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

read year

echo $year

我正在使用JAVA APi执行此Shell脚本

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控制台中,我可以看到输出

键入要检查的年份(4位数字),然后按[ENTER]:

现在,如何将值传递到我脚本中的Shell脚本中的实际问题是如何读取变量“ year”

我已经按照建议编辑了代码,但在我们更正的地方无法正常工作

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!");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值