求助!!Java怎么获取shell脚本的返回值哇..试了几种方法,都不可以
Java代码:
public static void main(String[] args) throws Exception {
try {
String shpath = "learn\\sh_test\\sh.sh";
Process ps = Runtime.getRuntime().exec("cmd /c " + shpath);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();
System.out.println(result);
System.out.println("exit value " + ps.exitValue());
} catch (Exception e) {
e.printStackTrace();
}
}shell脚本:
#!bin/bash -
function myfunc()
{
myresult='some value'
}
myfunc
echo $myresult
read t但是这个方法只是把shell脚本进行了执行,没有返回值。...
~~~真心求助。
~问题解决之后我把这个更新一下