执行“echo”使用 Java ProcessBuilder 不会插入变量(输出字符串“$PATH”)
我想回显 PATH 变量,目的是从 Java ProcessBuilder 获得与在终端中运行 echo $PATH
相同的输出。然而,当它执行时,输出实际上是 $PATH
而不是 PATH 变量的值。我想知道 ProcessBuilder 是否正在转义 $ 并且有什么技巧可以防止这种情况吗?
下面是我正在讨论的输出字符串“$PATH”的代码示例:
List<String> processBuilderCommand = ImmutableList.of("echo","$PATH");
ProcessBuilder processBuilder = new ProcessBuilder(processBuilderCommand).redirectErrorStream(true);
final Process process = processBuilder.start();
String commandOutput = CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return process.getInputStream();
}
}, Charset.defaultCharset()));
System.out.println(commandOutput);
一些额外的上下文:
我正在尝试模拟在我的单元测试之一中找不到的 sort
命令。我正在使用 this hack/trick 来更改我的 PATH 和通过检查 processBuilder.environment() 的结果,果然传递给进程的 PATH 变量不应该允许查找排序(我已经尝试过空字符串以及随机路径)。我想看看 shell 是否做了一些有趣的事情并修复了我试图破坏的备份 PATH。
I want to echo the PATH variable, with the goal to get the same output from a Java ProcessBuilder as running echo $PATH
in the terminal. However, when it executes the output is actually $PATH
instead of the value of the PATH variable. I wonder if ProcessBuilder is escaping the $ and is there a trick to prevent this?
Here is a code sample of what I am talking about that outputs the string "$PATH":
List<String> processBuilderCommand = ImmutableList.of("echo","$PATH");
ProcessBuilder processBuilder = new ProcessBuilder(processBuilderCommand).redirectErrorStream(true);
final Process process = processBuilder.start();
String commandOutput = CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return process.getInputStream();
}
}, Charset.defaultCharset()));
System.out.println(commandOutput);
Some extra context:
I am trying to simulate the sort
command not being found for one of my unit tests. I am using this hack/trick to change my PATH and by inspecting the result of processBuilder.environment()
and sure enough the PATH variable being passed to the process shouldn't allow finding sort (I've tried the empty string as well as a random path). I'd like to see if the shell is doing anything funny and fixing back up PATH which I am trying to destroy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$PATH
是bash
(和其他 shell)中使用的语法,用于引用环境变量PATH
。由于您使用 ProcessBuilder 执行的是echo
,而不是bash
,因此它不打印环境变量的内容也就不足为奇了。您应该从 Java 获取环境变量的内容,并将其作为参数提供给外部进程,或者执行一个能够正确解释
$PATH
语法的程序(例如如bash
)。正如下面的评论所指出的,
确实打印了 PATH 环境变量的内容。
$PATH
is the syntax used inbash
(and other shells) to refer to the environment variablePATH
. Since it'secho
you execute using the ProcessBuilder, and notbash
it's not very surprising that it doesn't print the content of the environment variable.You should either get hold of the content of the environment variable from Java, and give it as argument to the external process, or, execute a program which is capable of interpreting the
$PATH
syntax properly, (such asbash
).As pointed out in your comment below,
indeed prints the content of the PATH environment variable.