在Linux中从Linux执行shell脚本(使用perl和其他shell脚本的shell脚本)

时间:2022-12-06 08:57:46

I am trying to execute a shell script (that makes use of a perl script and other shell scripts) from java program, however I am not successful, here is what I tried:

我试图从java程序执行一个shell脚本(使用perl脚本和其他shell脚本),但是我没有成功,这是我尝试过的:

On the Linux server, in a folder test1/testJava the following scripts are available:

在Linux服务器上,在test1 / testJava文件夹中提供以下脚本:

  1. decode24.sh (the main script I call from java program, this script makes use of the other scripts listed below)
  2. decode24.sh(我从java程序调用的主脚本,这个脚本使用下面列出的其他脚本)

  3. fram.sh
  4. shadow.pl
  5. light.sh

Here is what I try from java:

这是我从java尝试:

try {
        ConnBean cb = new ConnBean("xx.yyy.zz.p", "user","passme");
        ssh = SSHExec.getInstance(cb);
        CustomTask ct1 = new ExecShellScript("/test1/testJava", "./decode24.sh", "arg1");
        ssh.connect();
        net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
 }catch......

Result after execution:

执行后的结果:

error message:

./decode.sh[17]: shadow.pl: not found [No such file or directory]
./decode.sh[21]: fram.sh: not found [No such file or directory]

2 个解决方案

#1


The error comes from your decode.sh script. It cannot find the shadow.pl and fram.sh scripts that it executes. Probably because the CWD or path is not set to the script dir when you run the script.

该错误来自您的decode.sh脚本。它找不到它执行的shadow.pl和fram.sh脚本。可能是因为运行脚本时CWD或路径未设置为脚本目录。

The most robust way to solve it is to change your decode.sh script to use absolute paths to the shadow.pl and fram.sh scripts. That way you can execute decode.sh from any directory.

最有效的解决方法是更改​​decode.sh脚本以使用shadow.pl和fram.sh脚本的绝对路径。这样你就可以从任何目录执行decode.sh。

If you use bash, check out this question for a neat way to resolve the absolute directory to the scripts to avoid hard coding the path.

如果您使用bash,请查看此问题,以便将绝对目录解析为脚本,以避免对路径进行硬编码。

#2


Let decode.sh find out the directory it is in (see bash solution) and use the path for calling the others.

让decode.sh找出它所在的目录(参见bash解决方案)并使用该路径调用其他目录。

#1


The error comes from your decode.sh script. It cannot find the shadow.pl and fram.sh scripts that it executes. Probably because the CWD or path is not set to the script dir when you run the script.

该错误来自您的decode.sh脚本。它找不到它执行的shadow.pl和fram.sh脚本。可能是因为运行脚本时CWD或路径未设置为脚本目录。

The most robust way to solve it is to change your decode.sh script to use absolute paths to the shadow.pl and fram.sh scripts. That way you can execute decode.sh from any directory.

最有效的解决方法是更改​​decode.sh脚本以使用shadow.pl和fram.sh脚本的绝对路径。这样你就可以从任何目录执行decode.sh。

If you use bash, check out this question for a neat way to resolve the absolute directory to the scripts to avoid hard coding the path.

如果您使用bash,请查看此问题,以便将绝对目录解析为脚本,以避免对路径进行硬编码。

#2


Let decode.sh find out the directory it is in (see bash solution) and use the path for calling the others.

让decode.sh找出它所在的目录(参见bash解决方案)并使用该路径调用其他目录。