如何在Java中执行Shell脚本

huangapple go评论78阅读模式
英文:

How to execute a shell script in Java

问题

String script = "ksh /xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh param1 param2 20200901 459 121";
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("sh", "-c", script);
processBuilder.redirectErrorStream(true);

// start() will be in try-catch
process = processBuilder.start();

int exitVal = process.waitFor();

if(exitVal == 0){
   //code
}
List<String> command = Arrays.asList("ksh", "/xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh", "param1", "param2", "20200901", "459", "121");
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);

// start() will be in try-catch
process = processBuilder.start();

int exitVal = process.waitFor();

if(exitVal == 0){
   //code
}
ProcessBuilder processBuilder = new ProcessBuilder("Notepad.exe", "C:/Dev/Test.txt");
processBuilder.redirectErrorStream(true);
process = processBuilder.start();

Linux执行脚本的部分已进行适当修改。

英文:

I have to run a shell script from Java in a Linux box
for eg,
Below is the script path and all parameters needed by the script.

&quot;ksh /xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh param1 param2 20200901 459 121&quot;

Below is the code,

String script = &quot;ksh /xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh param1 param2 20200901 459 121&quot;;
ProcessBuilder processBuilder = new ProcessBuilder();						
processBuilder.command(script);
processBuilder.redirectErrorStream(true);
//start() will be in try catch
process = processBuilder.start();

int exitVal = process.waitFor();

if(exitVal == 0){
   //code
}

Do i need to pass the script as a List<String> by adding each as a separate String in the List ?
"ksh" , "/xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh" , "param1" "param2" "20200901" "459" "121"

On my Windows machine, i am opening a Notepad as below and it works fine,

ProcessBuilder processBuilder = new ProcessBuilder(&quot;Notepad.exe&quot;, &quot;C:/Dev/Test.txt&quot;);
processBuilder.redirectErrorStream(true);
process = processBuilder.start();

Will the script get executed on the Linux box properly or any more changes are required in the code ??

答案1

得分: 1

你应该能够使用Runtime.exec()ProcessBuilder来调用shell运行脚本,但整个过程充满了陷阱。

我建议阅读这篇文章:

https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html

该文章解释了许多这些陷阱,以及如何避免它们。特别是在Linux上,您几乎肯定需要从shell调用中获取stdout和stderr输出 - 即使脚本不产生任何输出,这一点有时也成立。您还需要小心处理参数中的空格和特殊字符 - 如果有的话 - 在您的脚本中。Java用于命令行的解析器盲目地在空格处分割命令行。

英文:

You should be able to use Runtime.exec() or ProcessBuilder to invoke a shell to run a script, but the whole process is full of pitfalls.

I would suggest reading this article:

https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html

which explains many of these pitfalls, and how to avoid them. In particular, on Linux you'll almost certainly need to consume stdout and stderr from your shell invocation -- that's sometimes true even if the script doesn't produce any output. You'll also need to be careful how you handle spaces and special characters -- if there are any -- in the arguments to your script. The parser that Java uses for the command line blindly breaks a command line at whitespace.

答案2

得分: 0

看看这个 Runtime.getRunTime().exec

英文:

Take a look at this Runtime.getRunTime().exec

huangapple
  • 本文由 发表于 2020年9月4日 17:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63738277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定