Skip to content

micrometreuk/rust

Repository files navigation

Running Shell Commands and Capturing Output in Rust

Rust code example that demonstrates how to execute a shell command and capture its output:

Rust

use std::process::Command;

fn main() {
    let output = Command::new("ls")
        .arg("-la")
        .output()
        .expect("failed to execute process");

    if output.status.success() {
        let stdout = String::from_utf8(output.stdout).expect("failed to convert output to string");
        println!("{}", stdout);
    } else {
        let stderr = String::from_utf8(output.stderr).expect("failed to convert error output to string");
        eprintln!("Error: {}", stderr);
    }
}

Use code with caution.

Explanation:

1. Import the Command module:

  • This module provides functions to spawn child processes.

2. Create a Command instance:

  • Command::new("ls"): Initializes a new command with the specified program name.
  • .arg("-la"): Adds an argument to the command.

3. Execute the command and capture output:

  • .output(): Executes the command and returns an Output struct containing the standard output, standard error, and exit status.
  • .expect("failed to execute process"): Handles potential errors during execution.

4. Check the exit status:

  • If the status.success() method returns true, the command executed successfully.

5. Process the output:

  • String::from_utf8(output.stdout).expect("failed to convert output to string"): Converts the byte vector representing the standard output to a string.
  • Print the output to the console.

6. Handle errors:

  • If the command fails, the stderr field of the Output struct contains the error message.
  • Convert the error message to a string and print it to the error stream.

Key Points:

  • Error Handling: Always handle potential errors using expect or a more robust error handling mechanism like Result.

  • Output Parsing: The output is captured as a byte vector.

  • Security Considerations: Be cautious when executing arbitrary shell commands. Sanitize input and consider using safer alternatives if possible.

  • Platform-Specific Behavior: The exact behavior of shell commands might vary across different operating systems.