java調用shell腳本並傳參


java調用shell腳本並傳參
private void invokeShell(String projectDirectory, String ipaOutputDirectory, String version, String appName) {
try {
//第一個參數是sh命令,第 二個參數是腳本,從第三個參數天始,是我們要傳到腳本里的參數。
ProcessBuilder pb = new ProcessBuilder("/bin/sh",
OnlineBuildController.class.getResource("/").getFile() + "/ipa-build.sh",
projectDirectory,
"-o", ipaOutputDirectory,
"-v", version,
"-i", appName,
"-n");
Process p = pb.start();
process.waitFor();
try {
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = null;
while ((line = input.readLine()) != null){
logger.info("info:" + line);//打印輸出內容
}
if(null != input){
input.close();
}
if(null != ir){
ir.close();
}
int extValue = p.waitFor();
logger.info("extValue:" + extValue);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}

java代碼調用shell腳本

public void importDateTohive() {
try {
String shpath = "/data/hadoop/percisettask/2_merge_userlog.sh";
Process ps = Runtime.getRuntime().exec(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);
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("數據刷新成功");
}

上述代碼中,下面這段就是調用shell執行的代碼。
String shpath = "/data/hadoop/percisettask/2_merge_userlog.sh";
Process ps = Runtime.getRuntime().exec(shpath);
ps.waitFor();

注意:shpath最好寫絕對路徑。
1人點贊
java

java執行bat腳本和shell腳本並傳入參數
轉載pengjunlee 最后發布於2019-05-27 18:15:02 閱讀數 28173 收藏
展開
之前想着在windows下和linux下調用一些python Scrapy的接口,發現路徑問題,傳參數問題都挺麻煩,遂改為在bat文件和shell中具體寫方法,然后執行他們就好了
1.執行bat腳本
(1)傳入參數
bat處理文件中可引用的參數為%0%9,%0是指批處理文件的本身,也可以說是一個外部命令;%1%9是批處理參數,也稱形參,例如:新建一個文件test_argv.bat,文件內容如下:
@echo off

echo param[0] = %0
echo param[1] = %1
echo param[2] = %2
echo param[3] = %3
echo param[4] = %4
echo param[5] = %5
echo ...
pause
調用時只需要在執行bat文件后加上參數即可,記得參數間有空格
test_argv.bat 1 game test what
此時輸出:
param[0] = test_argv.bat
param[1] = 1
param[2] = game
param[3] = test
param[4] = what
param[5] =

請按任意鍵繼續…
(2)調用
最簡單的調用
Runtime.getRuntime().exec("D:\aaa\remoteDesktop\remoteConnection.bat");
這樣調用是不會有回顯的,如果你需要看到返回結果,就需要這樣:
try {
// 執行ping命令
Process process = Runtime.getRuntime().exec("cmd /c e:&dir");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
注意到了嗎,這次的exec函數不是簡單的選擇bat文件的路徑,多了“cmd /c” 這個前綴有以下使用方式:
cmd /c dir 是執行完dir命令后關閉命令窗口。
cmd /k dir 是執行完dir命令后不關閉命令窗口。
cmd /c start dir 會打開一個新窗口后執行dir指令,原窗口會關閉。
cmd /k start dir 會打開一個新窗口后執行dir指令,原窗口不會關閉。
注意:第二個調用方式在bat的輸出內容過長時,會卡死!!!T_T
網上的解釋說“因為本地進程輸入輸出緩存有限,你不快點讀取的話Process就掛在那了。” 所以需要開一個進程去不斷的取數據,具體實現方式見 :https://blog.csdn.net/aerchi/article/details/7669215 。這個我沒有檢測,因為我發現,如果bat輸出的內容過長時,使用第一種方式,不會卡死,若是想看到輸出 可以加前綴cmd /k start,若是想把輸出存起來 就加上后綴 >>1.txt,就好了,畢竟還要開線程太繁瑣。
2.執行shell文件
(1)傳入參數
shell腳本傳入參數與bat基本一致,只不過形參變成了$1,,$2,$3…..
例如,腳本test.sh的內容如下:
name=$1
echo "the ${name} are great man!"
執行./test.sh Xiao Ming命令,可以看到自己編寫腳本的結果
the Xiao Ming are great man!
(2)調用
linux環境果然友好得多,封裝好了以下代碼,傳入shell文件的路徑就好了
public static String linuxShellexec(String shellPath) {
String result="";
try {
Process ps = Runtime.getRuntime().exec(shellPath);
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");
}
result = sb.toString();
}
catch (Exception e) {
e.printStackTrace();
result="linux下運行完畢";
}
return result;
}
參考文章
https://www.cnblogs.com/happyPawpaw/p/3740903.html
https://blog.csdn.net/zyf_balance/article/details/51692065
https://blog.csdn.net/aerchi/article/details/7669215
https://blog.csdn.net/a1010256340/article/details/76187353
https://www.cnblogs.com/abel-hefei/p/7284256.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM