执行“echo”使用 Java ProcessBuilder 不会插入变量(输出字符串“$PATH”)

发布于 2025-01-07 15:02:36 字数 1158 浏览 0 评论 0原文

我想回显 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2025-01-14 15:02:36

$PATHbash(和其他 shell)中使用的语法,用于引用环境变量 PATH。由于您使用 ProcessBuilder 执行的是 echo,而不是 bash,因此它不打印环境变量的内容也就不足为奇了。

您应该从 Java 获取环境变量的内容,并将其作为参数提供给外部进程,或者执行一个能够正确解释 $PATH 语法的程序(例如如bash)。


正如下面的评论所指出的,

[...]  ImmutableList.of("/bin/bash","-c","echo $PATH")  [...]

确实打印了 PATH 环境变量的内容。

$PATH is the syntax used in bash (and other shells) to refer to the environment variable PATH. Since it's echo you execute using the ProcessBuilder, and not bash 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 as bash).


As pointed out in your comment below,

[...]  ImmutableList.of("/bin/bash","-c","echo $PATH")  [...]

indeed prints the content of the PATH environment variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文