gpt4 book ai didi

rust - 无法使用 std::io::process::Command 执行 `tput` 命令

转载 作者:行者123 更新时间:2023-11-29 08:05:36 31 4
gpt4 key购买 nike

当我在终端中运行 tput cols 时,它会打印出列数。但是当我运行以下 Rust 程序时:

use std::io::process::{Command, ProcessOutput};

fn main() {
let cmd = Command::new("tput cols");
match cmd.output() {
Ok(ProcessOutput { error: _, output: out, status: exit }) => {
if exit.success() {
println!("{}" , out);
match String::from_utf8(out) {
Ok(res) => println!("{}" , res),
Err(why) => println!("error converting to utf8: {}" , why),
}
} else {
println!("Didn't exit succesfully")
}
}
Err(why) => println!("Error running command: {}" , why.desc),
}
}

我收到以下错误:

Error running command: no such file or directory

有谁知道为什么命令不能正确运行?为什么要查找文件或目录?

最佳答案

Command::new仅采用要运行的命令的名称;可以使用 .arg() 添加参数.

match Command::new("tput").arg("cols").output() {
// …
}

关于rust - 无法使用 std::io::process::Command 执行 `tput` 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673049/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com