当我在终端中运行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
有谁知道命令无法正常运行的原因?为什么要查找文件或目录?
答案 0 :(得分:4)
Command::new
将命令的名称仅运行;可以使用.arg()
添加参数。
match Command::new("tput").arg("cols").output() {
// …
}