我正在 JavaFX 场景构建器应用程序中工作,在某些时候我的代码流执行如下:
在我的 Java 类中,有一个名为 script1 的 bash 脚本
来自MyClass.java
exec(./Script1)
在 script1 是另一个名为 script2 的脚本
called ./script2
script2 是另一个名为 script3 的脚本
在 脚本3
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),我需要显示 BUILD SUCESS 字符串以及 $upgrade_file_path
以及我的 javafx 标签中的退出代码。
或者我可以将此退出代码、路径和状态保存在我的 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.
关于java - 将 bash 脚本的返回代码和状态获取到 Java 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651545/