我正在尝试获取一个扩展命令来与runtime.exec一起使用,但是大括号被解释为文字而不是被扩展。这就是我正在尝试做的事情:
String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "sh", "-c", command } );
不幸的是,这在我的当前目录中提供了一个名为“Foldername{1,2,3}”的文件夹,而不是“Foldername1”、“Foldername2”和“Foldername3”。有谁知道如何防止大括号被解释为文字?
最佳答案
您正在尝试使用 Bash 通配符。它们在 Bash shell 中被解释。您直接运行 mkdir,因此没有 shell 来解释 {}
。您需要指定 shell 的路径
String command = "mkdir -p Foldername{1,2,3}/InnerFolder";
Runtime.getRuntime().exec( new String[] { "/bin/bash", "-c", command } );
Source .
关于java - 使用 Java 的 runtime.exec 进行 Shell (bash) 大括号扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486649/