导航:首页 > 编程语言 > 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相关的资料

热点内容
前端如何认证服务器 浏览:554
linux切换db2用户命令 浏览:306
相片如何用电解压 浏览:905
硕士程序员去学校当老师 浏览:120
pythonstr提取到字典 浏览:818
程序员那么可爱有人看上陆漓了 浏览:876
php正则提取图片 浏览:103
pythonlinuxdjango 浏览:562
php中文返回乱码 浏览:89
宿舍装的电信怎么加密 浏览:745
为什么压缩文件解压后变少了 浏览:426
现在安卓充电器普遍是什么型号 浏览:714
9日均线36均线主图指标源码 浏览:349
程序员阿里文化完整版 浏览:98
早间新闻在哪个app上面可以看 浏览:954
工作啦app注册的信息怎么删去 浏览:378
滚动转子式制冷压缩机 浏览:873
美国编程用什么软件 浏览:571
图片加密防盗用 浏览:616
dbscan算法python源码 浏览:849