gpt4 book ai didi

java - 无法从java向bash脚本发送参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:44 24 4
gpt4 key购买 nike

我正在尝试通过 Java 在我的 Ubuntu 机器上运行 bash 脚本。 bash 脚本将 2 个输入作为我作为数组传递的参数但是,它似乎没有将 array[0] 和 array[1] 的值传递给 bash 脚本?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.omg.CORBA.portable.InputStream;

public class readBashScript {

public static void readBashScript() {
try {

String[] array = {"ys1","R"};

Process proc = Runtime.getRuntime().exec("var/www/red/marsh_webreset.sh /",array);
BufferedReader read = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
try {
proc.waitFor();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
while (read.ready()) {
System.out.println(read.readLine());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

最佳答案

查看一些文档。

传递给 exec 方法的第二个参数是:

envp -- array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.

我建议看看 thisthis .

如果要传递环境变量,可以将它们添加为数组,但必须采用“key=value”格式。

即:

$ ONE=1 TWO=2 shell.sh

然后您可以在 shell 脚本中回显这些变量。

$ echo $ONE

关于java - 无法从java向bash脚本发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com