チーズパン
チーズパン

Reputation: 2778

Unable to pass string from read_line to Command arg

I have the following rust routine which executes fine:

use std::process::Command;
fn main() {
    let out = Command::new("iwconfig")
        .arg("wlan0")
        .output()
        .expect("Failed to execute 'iwconfig'");

    println!("status:\n {}", out.status);
    println!("stdout:\n {}", String::from_utf8_lossy(&out.stdout));
    println!("stderr:\n {}", String::from_utf8_lossy(&out.stderr));
}

It simply prints the result of the iwconfig command in linux with the wlan0 adapter as target. The output is fine for this program.

My goal enable the user to enter an adapter manually. If I execute the following program and enter wlan0 manually when prompted:

use std::process::Command;

fn main() {
    let mut adapter_input = String::new();
    println!("{}", "Enter adapter name:");
    io::stdin().read_line(&mut adapter_input).unwrap();

    let out = Command::new("iwconfig")
        .arg(adapter_input)
        .output()
        .expect("Failed to execute 'iwconfig'");

    println!("status:\n {}", out.status);
    println!("stdout:\n {}", String::from_utf8_lossy(&out.stdout));
    println!("stderr:\n {}", String::from_utf8_lossy(&out.stderr));
}

I get the following output:

wlan0 status: exit code: 237 stdout: stderr: wlan0 No such device

What am I doing wrong? Why is the adapter found when hardcoded but not found when entered manually?

Upvotes: 0

Views: 40

Answers (0)

Related Questions