英文:
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
您还不一定需要通过 cmd
和 cd
到达 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 <string>
, separate them by the command separator &&
.
So instead you can use something like this:
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();
}
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("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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论