在java中运行bash shell脚本

时间:2021-09-07 11:25:11

I want to run a shell script from my program below but it doesn't seem to do anything. I've run the same command directly in the linux terminal and it works fine so I'm guessing it's my java code. As you can see, I was first writing the command to the shell script using a PrintWriter but I expect that this would not effect the running of the shell script itself. Any help would be appreciated!

我想从下面的程序中运行shell脚本,但它似乎什么都不做。我已经在linux终端上直接运行了相同的命令,它运行得很好,所以我猜这是我的java代码。如您所见,我首先使用PrintWriter编写shell脚本的命令,但是我希望这不会影响shell脚本本身的运行。如有任何帮助,我们将不胜感激!

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String nfdump = "nfdump -o csv -r /home/shane/Documents/nfdump/nfcapd.201211211526>blank.txt";

    try {
        FileWriter fw = new FileWriter("/home/shane/Documents/script.sh");

        PrintWriter pw = new PrintWriter(fw);

        pw.println("#!/bin/bash");
        pw.println(nfdump);

        pw.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Process proc = null;

    try {
        proc = Runtime.getRuntime().exec("sh /home/shane/Documents/script.sh");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

3 个解决方案

#1


26  

You should use the returned Process to get the result.

您应该使用返回的进程来获取结果。

Runtime#exec executes the command as a separate process and returns an object of type Process. You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process.html#getOutputStream() on the returned Process object to inspect the output of the executed command.

运行时#exec将命令作为单独的进程执行,并返回类型process的对象。您应该调用Process#waitFor,以便您的程序等待到新的过程完成。然后,您可以在返回的Process对象上调用Process.html#getOutputStream()来检查执行命令的输出。

An alternative way of creating a process is to use ProcessBuilder.

创建流程的另一种方法是使用ProcessBuilder。

Process p = new ProcessBuilder("myCommand", "myArg").start();

With a ProcessBuilder, you list the arguments of the command as separate arguments.

使用ProcessBuilder时,可以将命令的参数列表为单独的参数。

See Difference between ProcessBuilder and Runtime.exec() and ProcessBuilder vs Runtime.exec() to learn more about the differences between Runtime#exec and ProcessBuilder#start.

请参阅ProcessBuilder和Runtime.exec()和ProcessBuilder与Runtime.exec()之间的差异,以了解更多关于运行时#exec和ProcessBuilder#start之间的差异。

#2


4  

Try this, it will work.

试试这个,会有用的。

String[] cmd = new String[]{"/bin/sh", "path/to/script.sh"};
Process pr = Runtime.getRuntime().exec(cmd);

#3


3  

When you execute a script from Java it spawns a new shell where the PATH environment variable is not set.

当您从Java执行一个脚本时,它会在路径环境变量未设置的地方生成一个新的shell。

Setting the PATH env variable using the below code should run your script.

使用下面的代码设置PATH env变量应该运行脚本。

String[] env = {"PATH=/bin:/usr/bin/"};
String cmd = "you complete shell command";  //e.g test.sh -dparam1 -oout.txt
Process process = Runtime.getRuntime().exec(cmd, env);

#1


26  

You should use the returned Process to get the result.

您应该使用返回的进程来获取结果。

Runtime#exec executes the command as a separate process and returns an object of type Process. You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process.html#getOutputStream() on the returned Process object to inspect the output of the executed command.

运行时#exec将命令作为单独的进程执行,并返回类型process的对象。您应该调用Process#waitFor,以便您的程序等待到新的过程完成。然后,您可以在返回的Process对象上调用Process.html#getOutputStream()来检查执行命令的输出。

An alternative way of creating a process is to use ProcessBuilder.

创建流程的另一种方法是使用ProcessBuilder。

Process p = new ProcessBuilder("myCommand", "myArg").start();

With a ProcessBuilder, you list the arguments of the command as separate arguments.

使用ProcessBuilder时,可以将命令的参数列表为单独的参数。

See Difference between ProcessBuilder and Runtime.exec() and ProcessBuilder vs Runtime.exec() to learn more about the differences between Runtime#exec and ProcessBuilder#start.

请参阅ProcessBuilder和Runtime.exec()和ProcessBuilder与Runtime.exec()之间的差异,以了解更多关于运行时#exec和ProcessBuilder#start之间的差异。

#2


4  

Try this, it will work.

试试这个,会有用的。

String[] cmd = new String[]{"/bin/sh", "path/to/script.sh"};
Process pr = Runtime.getRuntime().exec(cmd);

#3


3  

When you execute a script from Java it spawns a new shell where the PATH environment variable is not set.

当您从Java执行一个脚本时,它会在路径环境变量未设置的地方生成一个新的shell。

Setting the PATH env variable using the below code should run your script.

使用下面的代码设置PATH env变量应该运行脚本。

String[] env = {"PATH=/bin:/usr/bin/"};
String cmd = "you complete shell command";  //e.g test.sh -dparam1 -oout.txt
Process process = Runtime.getRuntime().exec(cmd, env);