导航:首页 > 编程语言 > shell返回值给java

shell返回值给java

发布时间:2023-05-06 13:26:56

java执行带参数的shell脚本并返回值

文件名确实不对
.sh文件才是linux下的批处理文件,它不认bat的
另外要保证.sh中调用的其他函数在当前目录下能正常运行

⑵ shell脚本是否能给java程序传值。

当然是可以,你可能是把windows下与linux下的调用混淆了, windows下分隔是; 而linux下是:
下面是个例子:
/usr/java/jdk1.6.0_31/bin/java -Dfile.encoding=UTF-8 -cp /usr/local/bin/ReplyParse.jar://usr/local/bin/lib/commons-httpclient-3.1.jar com.test.main "$arg1" "$arg2"

如果还有问题,请把报错告诉大家。

⑶ shell 怎么向java传递参数

碰到空格时,Windows 和 Linux 都是用双引号把参数括起来。

⑷ 使用java连接linux,执行shell命令返回值有乱码,怎么解决

packagecom.pasier.xxx.util;

importjava.io.IOException;
importjava.io.InputStream;
importjava.nio.charset.Charset;

importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;

importch.ethz.ssh2.ChannelCondition;
importch.ethz.ssh2.Connection;
importch.ethz.ssh2.Session;
importch.ethz.ssh2.StreamGobbler;

publicclassRmtShellExecutor{

privatestaticfinalLoggerLOG=LoggerFactory.getLogger(RmtShellExecutor.class);

privateConnectionconn;
privateStringip;
privateStringusr;
privateStringpsword;
privateStringcharset=Charset.defaultCharset().toString();

privatestaticfinalintTIME_OUT=1000*5*60;

publicRmtShellExecutor(Stringip,Stringusr,Stringps){
this.ip=ip;
this.usr=usr;
this.psword=ps;
}

privatebooleanlogin()throwsIOException{
conn=newConnection(ip);
conn.connect();
returnconn.authenticateWithPassword(usr,psword);
}

publicStringexec(Stringcmds)throwsIOException{
InputStreamstdOut=null;
InputStreamstdErr=null;
StringoutStr="";
StringoutErr="";
intret=-1;

try{
if(login()){
Sessionsession=conn.openSession();
session.execCommand(cmds);
stdOut=newStreamGobbler(session.getStdout());
outStr=processStream(stdOut,charset);
LOG.info("caijl:[INFO]outStr="+outStr);
stdErr=newStreamGobbler(session.getStderr());
outErr=processStream(stdErr,charset);
LOG.info("caijl:[INFO]outErr="+outErr);
session.waitForCondition(ChannelCondition.EXIT_STATUS,TIME_OUT);
ret=session.getExitStatus();

}else{
LOG.error("caijl:[INFO]ssh2loginfailure:"+ip);
thrownewIOException("SSH2_ERR");
}

}finally{
if(conn!=null){
conn.close();
}
if(stdOut!=null)
stdOut.close();
if(stdErr!=null)
stdErr.close();
}

returnoutStr;
}

privateStringprocessStream(InputStreamin,Stringcharset)throwsIOException{
byte[]buf=newbyte[1024];
StringBuildersb=newStringBuilder();
while(in.read(buf)!=-1){
sb.append(newString(buf,charset));
}
returnsb.toString();
}

publicstaticvoidmain(String[]args){

Stringusr="root";
Stringpassword="12345";
StringserverIP="11.22.33.xx";
StringshPath="/root/ab.sh";

RmtShellExecutorexe=newRmtShellExecutor(serverIP,usr,password);

StringoutInf;

try{
outInf=exe.exec("sh"+shPath+"xn");
System.out.println("outInf="+outInf);
}catch(IOExceptione){
e.printStackTrace();
}
}

}

⑸ shell 里面调用 java 怎么取到java地返回值呀

shell:var=`java Demo`
Demo.java:System.out.print("赋给shell变量的值")

最简单的方法了。

⑹ java调用shell无法返回参数。请教。

用java调用shell时没有shell环境变量,你的BPbin在哪个路径下?操作系统可能找不到的。
所以
1、你可以将脚本改成:
#!/bin/sh
db=`BPbin 2>&1`
echo $db
看我说得对不对
2、将BPbin的绝对路径写上去
我说的是可能性,你先按我说的操作,然后视结果看看是什么情况

阅读全文

与shell返回值给java相关的资料

热点内容
程序员那么爱心 浏览:300
字符a经过md5加密 浏览:413
绿色的小蝴蝶是个什么app 浏览:11
python编程输入数字输出年月日英文 浏览:622
程序员枪手 浏览:743
gm28服务器怎么设置 浏览:538
饿了么网站源码 浏览:328
天选程序员真的有用吗 浏览:914
微信登录服务器什么意思 浏览:349
溯源码粘碎图 浏览:133
qq绑定邮箱pop服务器地址 浏览:721
卡罗拉空调压缩机价格 浏览:889
华润it程序员 浏览:552
51单片机c语言秒表 浏览:271
php一周前的时间 浏览:851
windows文件夹输入列表 浏览:917
php做网页聊天系统 浏览:888
鼠标光学传感器读取单片机 浏览:165
食品批号的app是什么 浏览:194
文件夹复原快捷键 浏览:391