Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从java索引插入shell脚本_Java_Shell - Fatal编程技术网

如何从java索引插入shell脚本

如何从java索引插入shell脚本,java,shell,Java,Shell,我正在使用Java运行一个sh文件 然而,问题是如何在为Eclipse执行sh文件时将Java的变量放入sh变量中 我正在使用eclipse 这是我的密码 test.sh #!/bin/sh docker run -it --name $1 ubuntu:16.04 exit 0 在爪哇 public static void makeContainer() throws Exception { //String makecontainer = "/usr/local/bin/docke

我正在使用Java运行一个sh文件

然而,问题是如何在为Eclipse执行sh文件时将Java的变量放入sh变量中

我正在使用eclipse

这是我的密码

test.sh
#!/bin/sh
docker run -it --name $1 ubuntu:16.04
exit 0
在爪哇

public static void makeContainer() throws Exception {
    //String makecontainer = "/usr/local/bin/docker run --name "+fileName+" ubuntu:16.04";

    String[] command = { "/bin/sh","/Users/keomgong1/Desktop/test.sh", fileName };
    Process process = Runtime.getRuntime().exec(command);

    process.getInputStream();
    InputStream is = process.getInputStream();
    BufferedReader bf = new BufferedReader(new InputStreamReader(is));

}
我想要$1作为文件名,但它没有按预期运行

文件名是java字符串索引

怎么了


我能做什么?

脚本名后的第一个参数是程序名(
$0
),而不是第一个位置参数(
$1

您可以为
$0
添加一个虚拟文件名,然后您的下一个参数将是
$1

String[] command = { "/bin/sh","/Users/keomgong1/Desktop/test.sh", "_", fileName };
但是,最好直接运行脚本,而不是使用
/bin/sh

String[] command = { "/Users/keomgong1/Desktop/test.sh", fileName };
这需要
test.sh
有一个shebang(第一行是
#!/bin/sh
)和执行权限(
chmod+x/Users/keomgong1/Desktop/test.sh