簡體   English   中英

從bash腳本獲取返回代碼和狀態到Java文件

[英]Get return code and status from bash script into Java file

我正在使用JavaFX Scene Builder App,在某些時候我的代碼流執行如下所示:

從我的Java類開始,一個名為From MyClass.java的bash腳本script1

exec(./Script1)

在script1中,另一個名為script2的腳本

called ./script2

script2另一個名為script3的腳本

在script3中

if [ ! "$upgrade_file_path" = "" ]; then
    echo "BUILD SUCCESS"
    echo "upgrade.cpio.gz : "$upgrade_file_path
    //LINE-1
else
    echo "FAILED TO CREATE upgrade.cpio.gz"
fi

我需要的 :

LINE-1:我可以從此處將一些退出代碼返回到我的java文件(MyClass.java)中 ,我需要在$upgrade_file_path java.xfx標簽中顯示BUILD SUCESS字符串以及$upgrade_file_path和退出代碼。 或者我可以將此退出代碼,路徑和狀態保存在MyClass.java文件中的字符串中?

更新:

我正在使用外部jar連接SSH。 我想做的是從Windows計算機連接linux計算機,並為此實現了sshexec.jar https://code.google.com/p/sshxcute/downloads/list

下面的代碼完成了連接和執行bash腳本的工作

        ConnBean cb = new ConnBean(buildServerURL, buildServerUsername,buildServerPassword);
        // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
        ssh = SSHExec.getInstance(cb);          
        //Connect to server
        ssh.connect();
        CustomTask sampleTask = new ExecCommand("/usr/init/checkout.sh");
        //Execution of main taks
        Result rs = ssh.exec(sampleTask);

為了從Java執行shell命令,我們需要使用一些庫,在您使用SSHExec的情況下,在此jar中,您可以擁有從shell腳本返回的結果/退出代碼。

if [ ! "$upgrade_file_path" = "" ]; then
    echo "BUILD SUCCESS"
    echo "upgrade.cpio.gz : "$upgrade_file_path
    //Here you can just add something like:
    exit 0;
else
    echo "FAILED TO CREATE upgrade.cpio.gz"
    exit 1;
fi

當上述腳本執行時,將從此處拋出退出代碼,您可以在Java應用程序中使用以下退出代碼:

    ConnBean cb = new ConnBean(buildServerURL, buildServerUsername,buildServerPassword);
    // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
    ssh = SSHExec.getInstance(cb);          
    //Connect to server
    ssh.connect();
    CustomTask sampleTask = new ExecCommand("/usr/init/checkout.sh");
    //Execution of main taks
    Result rs = ssh.exec(sampleTask);
    int exitCode = rs.rc; //rc stands for result code, and this rc will have what shell returned.rc is not a function but a int variable of Result class.
    if(exitCode!=0){
      //Error message
}else 
     //Success message.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM