std::process::Command 执行没有错误,但没有任何操作。

huangapple go评论71阅读模式
英文:

std::process::Command executes without error but nothing happens

问题

use std::{process::Command};

let steamcmd_dir = String::from("C:/Users/user/Desktop/steamcmd");
let content = String::from("steamcmd +login anonymous");
let mut command = Command::new("cmd");

command.arg("/C");
command.arg("cd");
command.arg("/C");
command.arg(steamcmd_dir);
command.arg("/C");
command.arg(content);

command.output().unwrap();
英文:

I'm trying to cd into a directory that contains steamcmd.exe and run a command. When I run this code no error occurs and nothing actually happens.

use std::{process::Command};

let steamcmd_dir = String::from("C:/Users/user/Desktop/steamcmd");
let content = String::from("steamcmd +login anonymous");
let mut command = Command::new("cmd");

command.arg("/C");
command.arg("cd");
command.arg("/C");
command.arg(steamcmd_dir);
command.arg("/C");
command.arg(content);

command.output().unwrap();

答案1

得分: 3

Command::output() 文档解释:

执行命令作为子进程,等待其完成并收集其所有输出。

SteamCMD 的工作方式类似于数据库 CLI 客户端,您可以交互地使用它,也可以使用其命令行标志执行一次性命令。但是,您提供给 SteamCMD 的标志使其运行在交互模式下,不会在完成后退出,所以您需要附加 +quit 以便在完成后 SteamCMD 退出,如下所示:

steamcmd +login anonymous +quit

您还不一定需要通过 cmdcd 到达 SteamCMD 的位置,如果这对您更方便,您可以直接引用可执行文件:

let output = Command::new("C:/Users/user/Desktop/steamcmd/steamcmd.exe")
    .args(["+login", "anonymous", "+quit"])
    .output()
    .unwrap();
英文:

Command::output() documentation explains:

> Executes the command as a child process, waiting for it to finish and collecting all of its output.

SteamCMD works similarly to e.g a database CLI client in the sense that you can use it both interactively as well as for one-off commands using its command line flags. However the flags you supplied to SteamCMD makes it run in interactive mode and not exit at all, so you need to append +quit for SteamCMD to exit when it is finished, like so:

steamcmd +login anonymous +quit

You also don't necessarily need to go through cmd and cd to where SteamCMD is located, you can reference the executable directly if that's desirable to you:

let output = Command::new("C:/Users/user/Desktop/steamcmd/steamcmd.exe")
    .args(["+login", "anonymous", "+quit"])
    .output()
    .unwrap();

答案2

得分: 2

从关于cmd的文档中:

要对 <string> 使用多个命令,可以使用命令分隔符 && 进行分隔。

因此,您可以使用类似以下的内容:

use std::process::Command;

fn main() {
    let steamcmd_dir = String::from("C:/Users/user/Desktop/steamcmd");
    let content = String::from("steamcmd +login anonymous");
    let mut command = Command::new("cmd")
        .arg("/C")
        .arg([format!("cd {}", steamcmd_dir), content].join("&&"));

    command.output().unwrap();
}

然而,您也可以通过使用std::env::set_current_dir来更改当前进程的工作目录,然后像Svenskunganka的答案中所示直接运行steamcmd,从而完全避免使用cmd,如下所示:

fn main() {
    let steamcmd_dir = String::from("C:/Users/user/Desktop/steamcmd");
    std::env::set_current_dir(steamcmd_dir).unwrap();
    let mut command = Command::new("steamcmd")
        .args(["+login", "anonymous", "+quit"]);

    command.output().unwrap();
}
英文:

From the docs on cmd:
> To use multiple commands for &lt;string&gt;, separate them by the command separator &amp;&amp;.

So instead you can use something like this:

use std::process::Command;

fn main() {
    let steamcmd_dir = String::from(&quot;C:/Users/user/Desktop/steamcmd&quot;);
    let content = String::from(&quot;steamcmd +login anonymous&quot;);
    let mut command = Command::new(&quot;cmd&quot;)
        .arg(&quot;/C&quot;)
        .arg([format!(&quot;cd {}&quot;, steamcmd_dir), content].join(&quot;&amp;&amp;&quot;));

    command.output().unwrap();
}

However you can also avoid using cmd alltogether by using std::env::set_current_dir to change the current processes working directory and then running steamcwd directly like in Svenskunganka's answer:

fn main() {
    let steamcmd_dir = String::from(&quot;C:/Users/user/Desktop/steamcmd&quot;);
    std::env::set_current_dir(steamcmd_dir).unwrap();
    let mut command = Command::new(&quot;steamcmd&quot;)
        .args([&quot;+login&quot;, &quot;anonymous&quot;, &quot;+quit&quot;]);

    command.output().unwrap();
}

huangapple
  • 本文由 发表于 2023年2月27日 03:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定