https://github.com/brandonxlf/rust-system
Crate to easily run system/shell commands across different platforms, similar to the C system command.
https://github.com/brandonxlf/rust-system
rust shell-commands
Last synced: 5 months ago
JSON representation
Crate to easily run system/shell commands across different platforms, similar to the C system command.
- Host: GitHub
- URL: https://github.com/brandonxlf/rust-system
- Owner: BrandonXLF
- License: mit
- Created: 2024-03-17T21:28:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-18T18:49:40.000Z (almost 2 years ago)
- Last Synced: 2025-06-02T06:07:40.802Z (7 months ago)
- Topics: rust, shell-commands
- Language: Rust
- Homepage: https://crates.io/crates/system
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# system
Cross-platform crate to easily run shell commands, similar to the C `system` function.
## Usage
### `system` and `system_output`
For simple use cases where you just need the result of a system command, the `system` and `system_output` functions can be used.
`system` inherits the stdout, stderr, and stdin from the parent process whereas `system_output` captures stdout and stderr and does not inherit an stdin.
An example of using `system`,
```rust
use system::system;
fn main() {
// Prints "Hello, world!"
system("echo Hello, world!").expect("Failed to run command.");
}
```
An example of using `system_output`,
```rust
use system::system_output;
fn main() {
let out = system_output("echo Hello, world!").expect("Failed to run command.");
let stdout = String::from_utf8_lossy(&out.stdout);
#[cfg(target_os = "windows")]
assert_eq!(stdout, "Hello, world!\r\n");
#[cfg(not(target_os = "windows"))]
assert_eq!(stdout, "Hello, world!\n");
}
```
### `std::process::Command::system`
For more complex uses cases where the underlying `Command` has to be modified before running the command, the `system::System` trait is implemented for `Command`.
The trait adds the function `Command::system` to create `Command`s that execute shell commands.
For example,
```rust
use std::process::Command;
use system::System;
fn test() {
let out = Command::system("echo Hello, world!")
.output()
.expect("Failed to run command.");
let stdout = String::from_utf8_lossy(&out.stdout);
#[cfg(target_os = "windows")]
assert_eq!(stdout, "Hello, world!\r\n");
#[cfg(not(target_os = "windows"))]
assert_eq!(stdout, "Hello, world!\n");
}
```