如何使用Rust语言执行std::process::Command,并将其插入终端 fn main(){ 让output=Command::new(“/bin/bash”) .args(&[“-c”、“docker”、“build”、“-t”、“postgres:latest”、“-”、“,”dockers/PostgreSql“])) .output() .expect(“未能执行流程”); println!(“{:?}”,输出); } 上面的代码运行良好,但仅在docker脚本完全运行后才打印输出,但我希望看到Linux终端中的所有命令输出,并希望看到输出 我尝试了文档中给出的所有组合,并多次阅读,不明白如何将stdout重定向到终端窗口

如何使用Rust语言执行std::process::Command,并将其插入终端 fn main(){ 让output=Command::new(“/bin/bash”) .args(&[“-c”、“docker”、“build”、“-t”、“postgres:latest”、“-”、“,”dockers/PostgreSql“])) .output() .expect(“未能执行流程”); println!(“{:?}”,输出); } 上面的代码运行良好,但仅在docker脚本完全运行后才打印输出,但我希望看到Linux终端中的所有命令输出,并希望看到输出 我尝试了文档中给出的所有组合,并多次阅读,不明白如何将stdout重定向到终端窗口,rust,Rust,根据stdout,其默认行为取决于启动子流程的方式: 与“繁殖”或“状态”一起使用时默认为“继承”,与“输出”一起使用时默认为“管道” 因此,当您调用output()时,stdout是通过管道传输的。管道是什么意思?这意味着子进程的输出将被定向到父进程(本例中是我们的rust程序)std::process::Command很好地将其作为字符串提供给我们: fn main() { let output = Command::new("/bin/bash")

根据stdout,其默认行为取决于启动子流程的方式:

与“繁殖”或“状态”一起使用时默认为“继承”,与“输出”一起使用时默认为“管道”

因此,当您调用
output()
时,stdout是通过管道传输的。管道是什么意思?这意味着子进程的输出将被定向到父进程(本例中是我们的rust程序)
std::process::Command
很好地将其作为字符串提供给我们:


fn main() {
    let output = Command::new("/bin/bash")
                .args(&["-c", "docker","build", "-t", "postgres:latest", "-", "<>", "dockers/PostgreSql"])
                .output()
                .expect("failed to execute process");

    println!("{:?}", output);
}
现在我们已经了解了stdout当前的去向,如果您希望让控制台获得流式输出,请使用
spawn()
调用该进程:

请注意,在后面的示例中,我在一个字符串中传递了完整的
echo hello world
命令。这是因为
bash-c
将其arg按空间分割并运行它。如果您在控制台中通过
bash
shell执行docker命令,您会说:

use std::process::Command;

fn main() {

    let output = Command::new("/bin/bash")
                .args(&["-c", "echo hello world"])
                .spawn()
                .expect("failed to execute process");


    println!("{:?}", output);
}
上面的引号告诉终端将第三个arg放在一起,而不是按空格分割。在我们的rust数组中,等效的方法是在单个字符串中传递完整的命令(当然,假设您希望通过
bash-c
调用它)。

根据stdout,根据您启动子进程的方式,它具有默认行为:

与“繁殖”或“状态”一起使用时默认为“继承”,与“输出”一起使用时默认为“管道”

因此,当您调用
output()
时,stdout是通过管道传输的。管道是什么意思?这意味着子进程的输出将被定向到父进程(本例中是我们的rust程序)
std::process::Command
很好地将其作为字符串提供给我们:


fn main() {
    let output = Command::new("/bin/bash")
                .args(&["-c", "docker","build", "-t", "postgres:latest", "-", "<>", "dockers/PostgreSql"])
                .output()
                .expect("failed to execute process");

    println!("{:?}", output);
}
现在我们已经了解了stdout当前的去向,如果您希望让控制台获得流式输出,请使用
spawn()
调用该进程:

请注意,在后面的示例中,我在一个字符串中传递了完整的
echo hello world
命令。这是因为
bash-c
将其arg按空间分割并运行它。如果您在控制台中通过
bash
shell执行docker命令,您会说:

use std::process::Command;

fn main() {

    let output = Command::new("/bin/bash")
                .args(&["-c", "echo hello world"])
                .spawn()
                .expect("failed to execute process");


    println!("{:?}", output);
}

上面的引号告诉终端将第三个arg放在一起,而不是按空格分割。在我们的rust数组中,等效的方法是只在单个字符串中传递完整的命令(当然,假设您希望通过
bash-c
调用它)。

我很幸运地得到了另一个答案,但我不得不对它进行一些修改。对我来说,我 需要添加
wait
方法:

bash -c "docker run ..."
使用std:{io,process::Command};
fn main()->io::Result{
让mut o=Command::new(“rustc”).arg(“-V”).spawn()?;
o、 等待()?;
好(())
}
否则,父程序将在子程序之前结束


我很幸运得到了另一个答案,但我不得不稍微修改一下。对我来说,我 需要添加
wait
方法:

bash -c "docker run ..."
使用std:{io,process::Command};
fn main()->io::Result{
让mut o=Command::new(“rustc”).arg(“-V”).spawn()?;
o、 等待()?;
好(())
}
否则,父程序将在子程序之前结束


这与(可能重复)的方式非常相似。这与(可能重复)的方式非常相似。此设置对我有效
fn run_command(){let_output=command::new(“/bin/bash”).arg(“-c”).arg(“docker build-t postgres:latest-
实际上,在调试过程中,我更改了cargo中的包名,但忘记了在executable中运行新名称,所以我整个晚上都没有看到任何更改,我正在更改和构建,没有任何更改,所以我认为文档很糟糕,文档已经足够了,我的错误还在挣扎,这个设置对我有效
fn run_command(){let_output=command::new(“/bin/bash”).arg(“-c”).arg(“docker build-t postgres:latest-
实际上在调试期间,我更改了cargo中的包名,但是忘了在可执行文件中运行新名称,所以我整晚都没有看到任何更改,我正在更改和构建,也没有更改,所以认为文档很糟糕,文档很充分,我的错误让我挣扎,