導航:首頁 > 編程語言 > shelljava返回值

shelljava返回值

發布時間:2022-11-01 07:28:33

Ⅰ shell里判斷返回值的問題

你不要輸出結果,那就將結果重定向至 /dev/null 即可,具體代碼就是:

你的命令後面加上 >/dev/null 就不會有輸出結果了。舉個簡單的例子:

ping -c 192.168.1.1 這個命令,用於判斷到這個IP是否暢通,如果你在後面不加上重定向。

那麼每次都會輸出結果,也就是你現在的情況。所以下面代碼我就在ping的後面加上了重定向

代碼,讓結果屏蔽,僅獲取返回值$?,那屏幕上就不會顯示ping的結果了,只會顯示下面判斷結果,懂了么?

#!/bin/bash
ping-c2192.168.1.1>/dev/null
if[$?-eq0];then
echo192.168.1.1isokey!
else
echo192.168.1.1isfailed!
fi

Ⅱ shell 裡面調用 java 怎麼取到java地返回值呀

shell:var=`java Demo`
Demo.java:System.out.print("賦給shell變數的值")

最簡單的方法了。

Ⅲ 使用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執行的程序怎麼取得返回值

shell中,$? 就是上一個執行命令或函數的返回碼, 例如 執行命令xxxxx echo $? 就輸出了執行命令xxxxx的返回碼,當然也可以把$?保存到一個變數中,用於後續的計算或者其他判斷邏輯。

Ⅳ java執行帶參數的shell腳本並返回值

文件名確實不對
.sh文件才是linux下的批處理文件,它不認bat的
另外要保證.sh中調用的其他函數在當前目錄下能正常運行

Ⅵ Java解析執行shell命令返回的結果

String s = 那串結果;
String useful = s.substring(s.indexOf("/"));
String[] every = useful.split(" /");
得到的數組就是每個的信息,然後再解析(但要注意,第一個的開頭有一個斜線/)

Ⅶ java調用shell腳本,並得到shell腳本的返回值

文件名確實不對
.sh文件才是linux下的批處理文件,它不認bat的
另外要保證.sh中調用的其他函數在當前目錄下能正常運行

Ⅷ shell腳本調用java返回的字元串含有shell變數如何解析

先運行java程序,然後用echo $?獲取程序的運行結果,如果是字元串,可以使用
result=$(java -jar xxx.jar)來獲取,當然這只是示例,$()中的命令使可以隨便修改的

Ⅸ java 執行shell 腳本 返回 find的內容~~求助

你先列印pathshell這個變數,看這個變數是不是正確的,如果不正確,當然無法定位到find命令了。
你後面的代碼是沒有問題的,只能出在這個地方。
還有你的find命令沒有參數傳入啊,命令參數之類的還是是需要的。

閱讀全文

與shelljava返回值相關的資料

熱點內容
微盟程序員老婆 瀏覽:928
intellij創建java 瀏覽:110
java連接odbc 瀏覽:38
啟動修復無法修復電腦命令提示符 瀏覽:359
手機編程是什麼 瀏覽:97
山東移動程序員 瀏覽:163
蘇州java程序員培訓學校 瀏覽:475
單片機液晶驅動 瀏覽:854
魔拆app里能拆到什麼 瀏覽:130
新預演算法的立法理念 瀏覽:144
wdcpphp的路徑 瀏覽:134
單片機p0口電阻 瀏覽:926
瀏覽器中調簡訊文件夾 瀏覽:594
五菱宏光空調壓縮機 瀏覽:68
為什麼app佔用幾百兆 瀏覽:680
自動解壓失敗叫我聯系客服 瀏覽:484
易語言新手源碼 瀏覽:456
oa伺服器必須有固定ip地址 瀏覽:45
傳奇源碼分析是什麼 瀏覽:269
解放壓縮機支架 瀏覽:255