当前位置:编程学习 > JAVA >>

java调用shell返回值问题

shell中调用另一程序有返回值,此时java调用此shell无法获得程序的返回值 --------------------编程问答-------------------- 没写过shell,不过shell也能想方法一样返回值吗,你可以贴出一个程序研究一下 --------------------编程问答-------------------- 直接终端中调用此shell 可以输出结果,使用java调用此shell无法获得输出值
shell脚本部分代码

#!/bin/sh

dsVal=`/info/IBM/InformationServer/Server/DSEngine/bin/dsjob -user xx -password xx 
-server TESTDS:31539 -jobinfo dstage1 SQ_CWWJ__T_CWWJ_GLFY_SY`

echo dsVal

exit 0

--------------------编程问答-------------------- up up up  --------------------编程问答-------------------- java里需要从流里去读取shell的输出,并不是shell可以直接返回

 InputStream is = process.getInputStream ( ) ; 
        byte[] buffer = new byte[1024] ; 
        while ( is.read(buffer) != -1 ) { 
        result = new String (buffer) ; 
        } 
        is.close ( ) ; 
--------------------编程问答-------------------- 是从流中读取的,就是的不到输出值啊


 
#!/bin/sh
 
dsVal=`/info/IBM/InformationServer/Server/DSEngine/bin/dsjob -user xx -password xx 
-server TESTDS:31539 -jobinfo dstage1 SQ_CWWJ__T_CWWJ_GLFY_SY`
 
echo $dsVal

echo "hello world!"
 
exit 0
 


可以从流中换取到“hello world” 但是得不到dsVal的变量的值
--------------------编程问答--------------------
 Process pid = null;
 pid = Runtime.getRuntime().exec(shellCommand);
--------------------编程问答-------------------- 的确采用Runtime,Process。问题是inputStream获得不了值?直接运行shell文件终端可看到,而用java调用则无法获得

继续up! --------------------编程问答-------------------- 我遇到的问题 与此贴类似 http://bbs.csdn.net/topics/280053880 
烦请知道的朋友指点下

thanks! --------------------编程问答-------------------- up up up! --------------------编程问答--------------------
String[] cmds = {"/bin/sh", "-c" , "sssss.sh"};

补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,