简体   繁体   中英

Unable to execute `tput` command with std::io::process::Command

When I run tput cols in my terminal, it prints out the number of columns just fine. But when I run the following Rust program:

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),
    }
}

I get the following error:

Error running command: no such file or directory

Does anyone know why the command doesn't run correctly? Why is it looking for a file or directory?

Command::new takes the name of the command to run only; arguments can be added using .arg() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM